Skip to content

Instantly share code, notes, and snippets.

@borja-munoz
Created May 2, 2021 11:01
Show Gist options
  • Save borja-munoz/2a0cc94d43e0188803055b858b41adfd to your computer and use it in GitHub Desktop.
Save borja-munoz/2a0cc94d43e0188803055b858b41adfd to your computer and use it in GitHub Desktop.
Read geospatial file with Fiona and write CSV file with EWKB geometries using Shapely
import csv
import logging
import fiona
from shapely import geos, wkb
from shapely.geometry import shape
output_file = file_name + ".processing.csv"
with fiona.open(file_name, "r") as source:
# Write the CSV file row by row
with open(output_file, "w") as file:
writer = csv.writer(file, delimiter=",", lineterminator="\n")
firstRow = True
for f in source:
try:
if firstRow:
writer.writerow(
['geom'] +
list(f["properties"].keys())
)
firstRow = False
writer.writerow(
[wkb.dumps(shape(f["geometry"]), hex=True)] +
list(f["properties"].values())
)
except Exception:
logging.exception("Error processing feature %s:", f["id"])
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment