Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active June 18, 2022 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/1c76c8fd4f6ee08d9da88dfdb911cfcb to your computer and use it in GitHub Desktop.
Save ThomasG77/1c76c8fd4f6ee08d9da88dfdb911cfcb to your computer and use it in GitHub Desktop.
Multiple approaches to solve question at https://lists.osgeo.org/pipermail/gdal-dev/2022-June/055949.html
import os
from osgeo import gdal
# gdal.UseExceptions()
# gdal.SetConfigOption('CPL_DEBUG', 'ON')
# Create a test CSV
file = "test.csv"
with open("test.csv", "w") as csv:
csv.write("latitude,longitude\n")
csv.write("61,-150")
# GDAL approach using gdal.VectorTranslate
ds = gdal.OpenEx(
file, open_options=["X_POSSIBLE_NAMES=longitude", "Y_POSSIBLE_NAMES=latitude"]
)
gdal.VectorTranslate(
"out.gpkg", ds, options='-f GPKG -a_srs "EPSG:4326" -nln layernameout'
)
# Geopandas approach
import geopandas
gdf = geopandas.read_file(file, x_possible_names="longitude", y_possible_names="latitude")
gdf.to_file("dataframe1.gpkg", driver="GPKG", crs='epsg:4326')
# Fiona approach
import fiona
cols_lon_lat = ["longitude", "latitude"]
with fiona.open(file) as infile:
properties = dict(
[[i, "str"] for i in infile.schema.get("properties") if i not in cols_lon_lat]
)
schema = {"geometry": "Point", "properties": properties}
with fiona.open(
"test_geopackage.gpkg",
"w",
driver="GPKG",
crs="EPSG:4326",
schema=schema,
layer="layername",
) as gpkg:
for _, row in infile.items():
gpkg.write(
{
"properties": {
k: v
for k, v in row.get("properties").items()
if k not in cols_lon_lat
},
"geometry": {
"type": "Point",
"coordinates": [
float(row.get("properties").get(col))
for col in cols_lon_lat
],
},
}
)
@snowman2
Copy link

Thanks for the examples, they are a good reference. I believe this should work too:

gdf = geopandas.read_file(file, x_possible_names="longitude", y_possible_names="latitude")
gdf.to_file("dataframe.gpkg", driver="GPKG")

See: geopandas/geopandas#2221

@ThomasG77
Copy link
Author

ThomasG77 commented Jun 18, 2022

Thanks. Updated the gist. Do not forget the crs e.g

gdf.to_file("dataframe1.gpkg", driver="GPKG", crs='epsg:4326')

otherwise ogrinfo -so -al dataframe.gpkg output the following

INFO: Open of `dataframe.gpkg'
      using driver `GPKG' successful.

Layer name: dataframe
Geometry: Point
Feature Count: 1
Extent: (-150.000000, 61.000000) - (-150.000000, 61.000000)
Layer SRS WKT:
(unknown)
FID Column = fid
Geometry Column = geom
latitude: Real (0.0)
longitude: Real (0.0)

instead of

INFO: Open of `dataframe.gpkg'
      using driver `GPKG' successful.

Layer name: dataframe
Geometry: Point
Feature Count: 1
Extent: (-150.000000, 61.000000) - (-150.000000, 61.000000)
Layer SRS WKT:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
FID Column = fid
Geometry Column = geom
latitude: Real (0.0)
longitude: Real (0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment