User Tools

Site Tools


wiki:intersect-python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

wiki:intersect-python [2017/12/05 22:53] (current)
Line 1: Line 1:
 +====== DESCRIPTION ======
 +Command line to intersect two shape files.\\
 +The results is a new shape file storing only polygons that intersect each other.\\
 +The new shape file can overwrite an existing shape file.\\ ​
 +The script use the Qgis libraries which depends on GEOS libraries.\\
 +Pay attention to have the GEOS libraries installed ( geos-config --version ) and setting in the\\
 +script the right QgsApplication.setPrefixPath("/​path",​ True) where qgis is installed.\\
 +The script can be considered and extension of the ogr2ogr for the missing intersection flag.\\
 +\\
 +Download the file intersect.py\\
 +    chmod 777 intersect.py
 +    sudo mv intersect.py /​usr/​local/​bin/​
 +
 +    intersect.py src_shapefile1 ​ src_shapefile2 dst_shapefile
 +\\
 +<code bash| intersect.py>​
 +#​!/​usr/​bin/​python
 +
 +# version 1  ​
 +# August 20 2013
 +#​******************************************************************************
 +#  intersect.py
 +#
 +#  Repository: www.spatial-eclogy.net
 +#  Purpose: ​ Command line to intersect two shape files.
 +#            The results is a new shape file storing only polygons that intersect each other.
 +#            The new shape file can overwrite an existing shape file. 
 +#            The script use the Qgis libraries which depends on GEOS libraries.
 +#            Pay attention to have the GEOS libraries installed ( geos-config --version ) and setting in the
 +#            script the right QgsApplication.setPrefixPath("/​path",​ True) where qgis is installed.
 +#            The script can be considered and extension of the ogr2ogr for the missing intersection flag.
 +#  Author: ​  ​Giuseppe Amatulli, giuseppe.amatulli@gmail.com
 +#​******************************************************************************
 +#  Copyright (c) 2013, Giuseppe Amatulli, giuseppe.amatulli@gmail.com
 +
 +#  Permission is hereby granted, free of charge, to any person obtaining a
 +#  copy of this software and associated documentation files (the "​Software"​),​
 +#  to deal in the Software without restriction,​ including without limitation
 +#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 +#  and/or sell copies of the Software, and to permit persons to whom the
 +#  Software is furnished to do so, subject to the following conditions:
 +
 +#  The above copyright notice and this permission notice shall be included
 +#  in all copies or substantial portions of the Software.
 +
 +#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 +#  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,​
 +#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 +#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 +#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 +#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 +#  DEALINGS IN THE SOFTWARE.
 +#​******************************************************************************
 +# intersect.py src_shapefile1 ​ src_shapefile2 dst_shapefile
 +################################################################​
 +
 +import sys
 +import os
 +import os.path
 +from qgis import *
 +from qgis.core import *
 +QgsApplication.setPrefixPath("/​usr",​ True)  # in Ubuntu 12.04 repository installation /​usr/​bin/​qgis ​
 +QgsApplication.initQgis()
 +from qgis.analysis import * 
 +
 +def Usage():
 +    print('​Usage:​ intersect.py src_shapefile1 ​ src_shapefile2 dst_shapefile'​)
 +    sys.exit( 1 )
 +
 +src_shpA = None
 +src_shpB = None
 +dst_shp ​ = None
 +
 +if len(sys.argv) != 4:
 +    sys.exit(Usage())
 +
 +i = 1
 +while i < len(sys.argv):​
 +    arg = sys.argv[i]
 +    if src_shpA is None:
 +        src_shpA = arg
 +    elif src_shpB is None:
 +        src_shpB = arg
 +    elif dst_shp is None:
 +        dst_shp = arg
 +    else:
 +        Usage()
 +    i = i + 1
 +
 +if  src_shpA is None:
 +    Usage()
 + 
 +if  src_shpA is None:
 +    Usage()
 +
 +if  dst_shp is None:
 +    Usage()
 +
 +shpA = QgsVectorLayer( ​ src_shpA ​ , "​layerA",​ "​ogr"​)
 +if not shpA.isValid():​
 +    print "Layer failed to load! Check the",​src_shpA,​ "​path" ​
 +    sys.exit(1)
 +
 +shpB = QgsVectorLayer( src_shpB ​ , "​layerB",​ "​ogr"​)
 +if not shpB.isValid():​
 +    print "Layer failed to load! Check the",​src_shpB,"​path" ​
 +    sys.exit(1)
 +
 +print "​Intersecting",​src_shpA,"​with",​src_shpB,"​to create",​dst_shp
 +QgsOverlayAnalyzer().intersection( shpA , shpB  , dst_shp )
 +QgsApplication.exitQgis()
 +
 +
 +</​code>​
 +
 +
  
wiki/intersect-python.txt ยท Last modified: 2017/12/05 22:53 (external edit)