I'm trying to perform a simple buffer on GeoJSON file using Python script. I am able to do it on a shapefile using Python script and on GeoJSON using QGIS fix distance buffer tool but I can't figure out how to do it on GeoJSON using Python script. I read many posts on it but none of them include working solely on GeoJSON. I also know that it is possible to convert GeoJSON to .shp but I need to work only with GeoJSON files.
My code that works for .shp file:
from osgeo import ogr
ds = ogr.Open(r'C:\vector files\tryyy.shp', 1)
in_lyr = ds.GetLayer()
out_lyr = ds.CreateLayer('new_buff44',in_lyr.GetSpatialRef(),ogr.wkbPolygon)
out_lyr.CreateFields(in_lyr.schema)
out_defn = out_lyr.GetLayerDefn()
out_feat = ogr.Feature(out_defn)
bufferDist = -0.00015075
for in_feat in in_lyr:
geom = in_feat.geometry()
out_feat.SetGeometry(geom)
for in_feat in in_lyr:
geom = in_feat.geometry().Buffer(bufferDist)
out_feat.SetGeometry(geom)
for i in range(in_feat.GetFieldCount()):
value = in_feat.GetField(i)
out_feat.SetField(i, value)
out_lyr.CreateFeature(out_feat)
del ds
print "finish"
When I change the file from shp to geojson in the org.Open
line I get:
NoneType' object has no attribute 'CreateFields'
I thought that if I read vector file as OGR datasource object then it doesn't matter which format is the original vector file.
Can someone help me understand what I should change so that the code will work on GeoJSON files?
in_lyr
has a spatial ref