35 lines
1.2 KiB
Python
35 lines
1.2 KiB
Python
"""
|
|
Converts .csv files to .shape files
|
|
"""
|
|
import click
|
|
import fiona
|
|
import pandas as pd
|
|
from fiona.crs import from_epsg
|
|
from shapely.geometry import Point, mapping
|
|
|
|
from utils import setup_logging
|
|
|
|
logger = setup_logging()
|
|
|
|
|
|
@click.command()
|
|
@click.option("--input-csv", required=True, help=".csv file to convert")
|
|
@click.option("--output-shp", required=True, help="where to store .shp file")
|
|
def sites_csv_to_shp(input_csv, output_shp):
|
|
"""
|
|
Converts our dataframe of sites to .shp to load in QGis
|
|
:param input_csv:
|
|
:param output_shp:
|
|
:return:
|
|
"""
|
|
logger.info("Converting %s to %s", input_csv, output_shp)
|
|
df_sites = pd.read_csv(input_csv, index_col=[0])
|
|
logger.info(os.environ.get("GDAL_DATA", None))
|
|
schema = {"geometry": "Point", "properties": {"beach": "str", "site_id": "str"}}
|
|
with fiona.open(output_shp, "w", crs=from_epsg(4326), driver="ESRI Shapefile", schema=schema) as output:
|
|
for index, row in df_sites.iterrows():
|
|
point = Point(row["x_200_lon"], row["x_200_lat"])
|
|
prop = {"beach": row["beach"], "site_id": index}
|
|
output.write({"geometry": mapping(point), "properties": prop})
|
|
logger.info("Done!")
|