Skip to content

Instantly share code, notes, and snippets.

@Blake-Loveland
Forked from borja-munoz/createCSVwithEWKB.py
Created August 11, 2023 13:02
Show Gist options
  • Select an option

  • Save Blake-Loveland/2eca6995930c4d4fea902297604c77c5 to your computer and use it in GitHub Desktop.

Select an option

Save Blake-Loveland/2eca6995930c4d4fea902297604c77c5 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