-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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