[gdal-dev] Improve clipping of shapefiles

Paul Meems bontepaarden at gmail.com
Mon Sep 11 02:09:12 PDT 2017


I have a large shapefile with over 2.8 million shapes (fishnet) and I have
a border file with only 1 multipolygon.

I'm trying to clip the fishnet with the border.
Using code is takes about 5 min. using command line it takes even longer.

This is my command:
ogr2ogr fishnetClipped.shp fishnet.shp -clipsrc border.shp

And this is my C# code:
using (var dsFishnet = Ogr.Open(fishnetFilename, 0))
{
    // Select first layer:
    using (var layerFishnet = dsFishnet.GetLayerByIndex(0))
    {
        // Open border:
        using (var dsBorder = Ogr.Open(borderFilename, 0))
        {
            // Select first layer:
            using (var layerBorder = dsBorder.GetLayerByIndex(0))
            {
                // Get ESRI driver:
                using (var driver = Ogr.GetDriverByName("ESRI Shapefile"))
                {
                    // Create new shapefile:
                    using (var dsClipped =
driver.CreateDataSource(outputFilename, new string[] { }))
                    {
                        // Create new layer:
                        var layerName =
Path.GetFileNameWithoutExtension(outputFilename);
                        using (var layerClipped =
dsClipped.CreateLayer(layerName, layerFishnet.GetSpatialRef(),
layerFishnet.GetGeomType(), new string[] { }))
                        {
                            if (layerClipped == null)
                            {
                                throw new Exception("Layer creation
failed.");
                            }

                            // Clip the layer:
                            layerFishnet.Clip(layerBorder, layerClipped,
null, null, null);
                        }
                    }
                }
            }
        }
    }
}

I'm using GDAL 2.1.3

Can I somehow improve the speed?
I read something about putting one of the layers in memory or using a
spatial filter.
But I can't find an example of how to do this.

Please advise.

Thanks,
Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20170911/a0381b2f/attachment.html>


More information about the gdal-dev mailing list