<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: Times New Roman; font-size: 12pt; color: #000000'><meta http-equiv="content-type" content="text/html; charset=utf-8"><span class="Apple-style-span" style="font-family: Verdana, Geneva, Helvetica, Arial, sans-serif; font-size: 13px; ">Hi all,&nbsp;<br><br>I'm a newbie in gdal developement, working with python on a method to clip polygon and multi-polygon shapefiles.&nbsp;<br><br>To perform this, I first create the Geometry of my clipper, then I set a spatial filter on my layer and loop for the features.&nbsp;<br><br>If I have a feature of type polygon, I calculate the intersection using the methode Geometry.Intersection and add the result to the final geometry to return.&nbsp;<br><br>If I have a feature of type multi-polygon, I loop into it and add the intersections between my clipper and the polygons within the multi-polygon feature.&nbsp;<br><br>The method works but It seems that it does not always return a correct intersection. See the attached file :&nbsp;<br><br><img src="http://osgeo-org.1803224.n2.nabble.com/file/n6410817/intersection.png" border="0"><br><br>Normally, I should not have not filled (blank spaces) areas. I was wondering if it's a problem of the spatial filter, i removed it, but it still returns exactly the same result.&nbsp;<br><br>For polygon shapefiles it perfectly returns a correct result. But when it's up to shapefiles containing multi-polygons, it always return that empty areas.&nbsp;<br><br>I think the problem is may be due to multi-polygons containing multi-polygons !&nbsp;<br><br>Thank you for help. If you have any suggestions , they would be really welcome !! I can't find the solution to my problem ... :'(&nbsp;<br><br>Here is my code :&nbsp;<br><br><br>def clipShp(self, infile, outfile):&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; # Creating the output Shapefile (out datasource, layer and feature)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; #--------------------------------------------------------&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; driver = ogr.GetDriverByName("ESRI Shapefile")&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; outDS = driver.CreateDataSource(outfile)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; if outDS is None:&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("clipShp ERROR : Unable to create output DataSource.")&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return False&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; outLayer = outDS.CreateLayer(outfile, geom_type=ogr.wkbMultiPolygon)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; if outLayer is None:&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("clipShp ERROR : Unable to create output Layer")&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return False&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; outFeature = ogr.Feature(outLayer.GetLayerDefn())&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; if outFeature is None:&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("clipShp ERROR : Unable to create output Feature")&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return False&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; # Reading the filtred geometry&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; #--------------------------------------------------------&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; inGeom = self.<b>clipGeometry(infile)</b><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; outFeature.SetGeometryDirectly(inGeom)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; outLayer.CreateFeature(outFeature)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; # Clean up&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; #--------------------------------------------------------&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; outFeature.Destroy()&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; outDS.Destroy()&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; return True &nbsp;&nbsp;<br><br><br>Here is the code of the method&nbsp;<b>clipGeometry</b>&nbsp;:&nbsp;<br><br><br>def clipGeometry(self,fileDS):&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; geom = None&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; # Read the input file, create the datasource and layer&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; # (This method works only for shapefiles that contains one Layer. )&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; #--------------------------------------------------------&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; datasource = ogr.Open(fileDS,False)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; layer = datasource.GetLayer(0)&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if layer is None:&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("ERROR : Failed to load layer from datasource")&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return None&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; # Create the geometry of the clipper (variable clipGeom) &nbsp;which is of polygon type&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; # REMARK : We must create rings first and then add them to the polygon geometry&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; #--------------------------------------------------------&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; clipRing = ogr.Geometry(ogr.wkbLinearRing)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; clipRing.AddPoint_2D( float(self.xmin), float(self.ymin) )&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; # Create a rectangle here...&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; clipGeom = ogr.Geometry(ogr.wkbPolygon)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; clipGeom.AddGeometry(clipRing)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; # Set in the SpatialFilter (rectangular)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; # than, loop on this set of features&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; #--------------------------------------------------------&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; #layer.SetSpatialFilterRect(self.xmin,self.ymin,self.xmax,self.ymax)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; layer.SetSpatialFilter(clipGeom)&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; feature = layer.GetNextFeature()&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; if feature is None:&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return None&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; while feature is not None:&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; srcGeom = feature.GetGeometryRef()&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if srcGeom is not None:&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; geomType = srcGeom.GetGeometryType()&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if geom is None:&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; geom = ogr.Geometry(ogr.wkbMultiPolygon)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; geom.AssignSpatialReference(srcGeom.GetSpatialReference())&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if geomType == ogr.wkbPolygon:&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Calculate the intersection Geometry&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #--------------------------------------------------------&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clippedpolygon = srcGeom.Intersection(clipGeom)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; geom.AddGeometry( clippedpolygon )&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif geomType == ogr.wkbMultiPolygon:&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for iGeom in range( srcGeom.GetGeometryCount()):&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Calculate the intersection Geometry&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #--------------------------------------------------------&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clippedpolygon = srcGeom.GetGeometryRef(iGeom).Intersection(clipGeom)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; geom.AddGeometry(clippedpolygon)&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("ERROR : Geometry not of polygon type." )&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; datasource.Destroy()&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return None&nbsp;<br>&nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; feature = layer.GetNextFeature()&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; datasource.Destroy()&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; return geom&nbsp;</span></div></body></html>