Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating GeoJson from csv: How do I set geometry properties?

I have a csv file that I want to convert to GeoJson.

lon,lat,val,id
-97.1589432,25.9642008,0,2690
-97.1682294,25.9761856,0,2691
-97.1775156,25.9881704,0,2692

I run the following ogr2ogr command

ogr2ogr -f GEOJson geo_result.json source.csv

and I get this result

{
"type": "FeatureCollection",

"features": [
{ "type": "Feature", "properties": { "lon": "-97.1589432", "lat": "25.9642008", "val": "0", "id": "2690" }, "geometry": null },
{ "type": "Feature", "properties": { "lon": "-97.1682294", "lat": "25.9761856", "val": "0", "id": "2691" }, "geometry": null },
{ "type": "Feature", "properties": { "lon": "-97.1775156", "lat": "25.9881704", "val": "0", "id": "2692" }, "geometry": null }
]
}

Why is geometry null? How can I tell ogr2ogr which values are lat & lon?

like image 642
GrantE Avatar asked Dec 07 '25 02:12

GrantE


1 Answers

You need to wrap the CSV file in a VRT file which specifies the meaning of the columns.

<OGRVRTDataSource>
    <OGRVRTLayer name="source">
        <SrcDataSource>source.csv</SrcDataSource>
        <GeometryType>wkbPoint</GeometryType>
        <LayerSRS>WGS84</LayerSRS>
        <GeometryField encoding="PointFromColumns" x="lon" y="lat"/>
    </OGRVRTLayer>
</OGRVRTDataSource>

And then use the VRT file as the input:

ogr2ogr -f GEOJson geo_result.json source.vrt

QGIS (which uses OGR) has an interface which cleverly guesses the columns, and that works very well. So if you have to use your conversion only once it might be worth trying it with a GUI like QGIS.

See the CSV driver page from OGR for more details: http://www.gdal.org/ogr/drv_csv.html

like image 154
Rutger Kassies Avatar answered Dec 08 '25 16:12

Rutger Kassies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!