0

I have the Lat Lon information of the boundaries of 43 shapefiles. Now I want to combine all these 43 polygons into a single shapefile. How can I do that?

What I did is I converted every polygon to a single shapefile and trying to run this code to combine them to a single shapefile.

import os
import ogr

outputMergefn = 'Merge.shp'
directory = r'G:\ARSI\LAndsat data\Field\Hartford\12022018\shapefiles\\'
#filestartswith = 'C'
FileEndsWith = '.shp'
drivername = 'ESRI Shapefile'
geometrytype = ogr.wkbMultiPoint
ptdriver = ogr.GetDriverByName('ESRI Shapefile')

if os.path.exists(outputMergefn):
    ptdriver.DeleteDataSource(outputMergefn)
out_ds = ptdriver.CreateDataSource(outputMergefn)
out_layer = out_ds.CreateLayer(outputMergefn, geom_type = geometrytype)



filelist = os.listdir(directory)
for file in filelist:
    if file.endswith(FileEndsWith):
        print(file)
        ds = ogr.Open(directory + file)
        if ds is None:
            print("This is None")
        lyr = ds.GetLayer()
        for feat in lyr:
            out_feat = ogr.Feature(out_layer.GetLayerDefn())
            out_feat.SetGeometry(feat.GetGeometryRef().Clone())
            out_layer.CreateFeature(out_feat)
            out_layer.SyncToDisk()

But, the merged shapefile does not show any information in QGIS, looks like its empty. Is there any way I can convert the Lat Lon information of multiple polygons to a single shapefile?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.