Geographic Information Systems Asked by notmenotme on December 14, 2020
I found a Python port of ogr2ogr which can be found here
This port allows me to re-project vectors from a Python script instead of having to use the command line:
import pyogr2ogr, tempfile
with fiona.open("B:/Canada-d8-100m/Canada-d8-100m.geojson", "r") as before:
print(before.crs)
> {'init': 'epsg:3347'}
pyogr2ogr.main(["","-t_srs", "EPSG:4326", "-f", "GeoJSON", "B:/proj_test.geojson", "B:/Canada-d8-100m/Canada-d8-100m.geojson"])
with fiona.open("B:/proj_test.geojson", "r") as after:
print(after.crs)
> {'init': 'epsg:4326'}
I would like to do the same thing as above, but I want to be able to read/write the output GeoJSON to/from memory instead of to/from the disk.
How can I do this?
You can do it purely with Fiona
import fiona
from fiona import transform
with fiona.open('B:/Canada-d8-100m/Canada-d8-100m.geojson') as input:
# The output has the same schema
output_schema = input.schema.copy()
# write a new shapefile
with fiona.open('B:/proj_test.geojson', 'w', 'GeoJSON', output_schema, crs={'init': 'epsg:4326'}) as output:
for elem in input:
if not elem['geometry']:
geom = None
else:
geom = transform.transform_geom(
input.crs, 'EPSG:4326', elem['geometry'])
output.write({
'properties': elem['properties'],
'geometry': geom
})
With Geopandas
import geopandas as gpd
geojson_input = gpd.read_file("B:/Canada-d8-100m/Canada-d8-100m.geojson")
geojson_output = geojson_input.to_crs({'init': 'epsg:4326'}) # Had some issues here due to empty geometries...
geojson_output.to_file("B:/proj_test.geojson", driver="GeoJSON")
Correct answer by ThomasG77 on December 14, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP