Skip to content

Instantly share code, notes, and snippets.

@daliposc
Last active December 3, 2019 09:11
Show Gist options
  • Save daliposc/fc131152c0db1d9cebd36da951976420 to your computer and use it in GitHub Desktop.
Save daliposc/fc131152c0db1d9cebd36da951976420 to your computer and use it in GitHub Desktop.
ogr2ogr cookbook. a very small cookbook (so far).

Batch command, all GeoJSON in folder —> PostGIS

FOR %%f IN (*.geojson) DO (
	ogr2ogr -f PostgreSQL PG:"dbname={dbname} user={user} password={pass}" %%f
)

Batch command, Numbered files —> PostGIS

Run as: to_postgis.bat [base_filename] [file_extenstion] [num_files]. Assumes first file is "[base_filename]_0.[file_extension]".

SET arg1=%1
SET arg2=%2
SET arg3=%3
REM Format string, remove trailing space. See: https://www.dostips.com/DtTipsStringManipulation.php
SET file=%arg1: =%
SET ext=%arg2: =%
SET num=%arg3: =%
ogr2ogr -f PostgreSQL PG:"dbname={dbname} user={user} password={pass}" %file%_0.%ext% -nln %file%
FOR /l %%i IN (1,1,%num%) DO (
	-update -append ogr2ogr -f PostgreSQL PG:"dbname={dbname} user={user} password={pass}" %file%_%%i.%ext% -nln %file%
)

GTFS stops.txt —> PostGIS

ogr2ogr CSV reference: https://gdal.org/drivers/vector/csv.html

ogr2ogr -f PostgreSQL PG:"dbname={dbname} user={user} password={pass}" CSV:stops.txt -s_srs EPSG:4326 -t_srs EPSG:2913 -oo X_POSSIBLE_NAMES=*_lon -oo Y_POSSIBLE_NAMES=*_lat -oo AUTODETECT_TYPE=YES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment