Skip to content

Add support for conversion GPKG -> CSV #40

Open
@schoeller

Description

Dear all,

thanks for this project.
I see a need to convert from GPKG->CSV file format starting out from #39. I have written a first script to compile from attached sample file buildings from a GPKG-package into CSV format. Thus one should be able to edit a GPKG-file through GIS and later serve as model input.

Best regards

Sebastian

"""
Sources:
Polyline geometry with attribute measuredHeight to be converted into CSV format as per documentation
csv2building_polygons(file name, floor height=3)
Module: shallow_water.data_manager
Convert CSV files of the form:
easting,northing,id,floors
422664.22,870785.46,2,0
422672.48,870780.14,2,0
422668.17,870772.62,2,0
422660.35,870777.17,2,0
422664.22,870785.46,2,0
422661.30,871215.06,3,1
422667.50,871215.70,3,1
422668.30,871204.86,3,1
422662.21,871204.33,3,1
422661.30,871215.06,3,1

https://stackoverflow.com/questions/69859170/how-acess-get-extract-the-values-of-polygon-in-a-geometry-column-of-geopandas-da
https://stackoverflow.com/questions/56709561/how-to-smartly-loop-over-all-points-in-a-geodataframe-and-look-at-nearest-neig
https://stackoverflow.com/questions/44513738/pandas-create-empty-dataframe-with-only-column-names
https://stackoverflow.com/questions/16923281/writing-a-pandas-dataframe-to-csv-file#16923367
"""

import pandas as pd
import geopandas as gpd

def coord_lister(geom):
    coords = list(geom.exterior.coords)
    return (coords)

def write_csv_files(row):
    dir_path = './'
    file_name = dir_path + '_building_' + str(row.id) + '.csv'
    coords = coord_lister(row.geometry)
    df = pd.DataFrame(columns=['easting','northing','id','floors'])
    for xy in coords:
        df.loc[len(df)] = [xy[0], xy[1], int(row.id), int(row.floors)]
    df['id'] = df['id'].astype(int)
    df['floors'] = df['floors'].astype(int)
    df.to_csv(file_name, sep=',', header=True, index=None)
    return(file_name)

if __name__ == "__main__":
    gdf = gpd.read_file("buildings_mod.gpkg")
    for index,row in gdf.iterrows():
        file_name = write_csv_files(row)
        print("Writing file " + str(file_name))
    print("Files have been written\n")

buildings_mod.zip

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions