Hot answers tagged

28 votes
Accepted

Import a shapefile to postgis with ogr2ogr gives: Unable to open datasource

As you discovered by trial and error, there were few nagging issues you needed to fix, the last of which was resolved using ogr2ogr's -nlt GEOMETRY* argument. * Note the recommendation in @...
user avatar
  • 6,140
24 votes
Accepted

Uploading Geopackage contents to PostgreSQL

This tutorial shows how to import geopackges into postgres with ogr2ogr like: ogr2ogr -f PostgreSQL "PG:dbname=gadm" DNK_adm.gpkg Note importing into PostgreSQL like this will convert the case of ...
user avatar
  • 1,002
22 votes
Accepted

Piping data to ogr2ogr

OGR has its own idiom for stdin, /vsistdin/. Use that as ogr2ogr's first argument (the dst_datasource_name) and you can pipe curl's output to it: curl "https://raw.githubusercontent.com/nvkelso/...
user avatar
  • 8,538
19 votes

Loading shapefile to a specific table in postgis using ogr2ogr

To be explicit about nickves' answer: ogr2ogr -append -f "PostgreSQL" PG:"dbname=db" shapefile.shp -nln mytable
user avatar
19 votes
Accepted

ogr2ogr merge multiple shapefiles: What is the purpose of -nln tag?

For GDAL there are datastores which contain layers. Some datastores, like the database ones or GML, can hold several layers but some others like shapefiles can only contain one layer. You can test ...
user avatar
  • 52.5k
18 votes
Accepted

ogr2ogr error importing shapefile into PostGIS : "numeric field overflow"

For making the conversion to success read the manual page http://www.gdal.org/drv_pg.html from section Layer Creation Options PRECISION: This may be "YES" to force new fields created on this layer ...
user avatar
  • 52.5k
16 votes
Accepted

Creating gpkg with many layers using ogr2ogr

You are overwriting dst.gpkg every time you run ogr2ogr. From the geopackage documentation: For adding new layers into existing geopackage run ogr2ogr with -update. So do: ogr2ogr -f GPKG dst....
user avatar
  • 52.8k
15 votes

How to export thousands of features from PostGIS to a single GeoJSON file?

There is, yes, using a combination of row_to_json, array_to_json, array_agg and ST_AsGeoJSON. I realize, on rereading your question, that you asked for ogr2ogr approach, but seeing as your source is ...
user avatar
  • 13.2k
15 votes

Repairing broken GDAL and PROJ.4 on Ubuntu?

In https://gis.stackexchange.com/questions/158457/gdal2tiles-how-to-make-filenames-of-tiles-to-represent-xyz-coordinates, somebody mentions doing: sudo ln -s /usr/lib/libproj.so.0 /usr/lib/libproj.so ...
user avatar
  • 381
15 votes
Accepted

How do I create/write a Shapefile with UTF-8 encoded attributes with ogr2ogr?

How can I get ogr2ogr to write a UTF-8 encoded *.dbf file when exporting to Shapefile? Similar to How to encode shapefiles from LATIN1 to UTF-8?, this is possible with -lco ENCODING=UTF-8. So for my ...
user avatar
  • 1,370
15 votes
Accepted

How to assign a schema to ogr2ogr?

SCHEMA is a "layer creation option", specified with the -lco parameter. This should do the trick (split into multiple lines here for readability): call ogr2ogr -overwrite -skipfailures -f ...
user avatar
  • 1,758
15 votes

How to clip very large shapefile in QGIS using ogr2ogr?

On windows environnement, if you installed Qgis with OSGeoW setup you have access to a shell which can run directly gdal/ogr. to run a clic processing use this command ogr2ogr -clipsrc clip_polygon....
user avatar
14 votes
Accepted

ogr2ogr clip all shapefiles in a directory

Modifying the referenced answer, in the Windows command line: for /R %f in (*.shp) do ogr2ogr "%~dpnf_clipped.shp" -clipsrc clipper.shp "%f" You just add _clipped to the output. Note, this will also ...
user avatar
14 votes
Accepted

Specify geom field name with ogr2ogr: gdb to PostgreSQL

In the help in ogr2ogr --long-usage, there is layer creation option: -lco NAME=VALUE: Layer creation option (format specific) You can use the driver specific option GEOMETRY_NAME=geom to Set ...
user avatar
  • 10.3k
13 votes
Accepted

Converting csv to kml/geojson using ogr2ogr or equivalent command line tools?

According to the ogr2ogr csv documentation and also this answer, you need to specify which fields contain the geometry in a VRT file: <OGRVRTDataSource> <OGRVRTLayer name="test"> ...
user avatar
  • 6,738
13 votes
Accepted

select by location using ogr2ogr sqlite

At the following link all SQL functions that test spatial relationships are described: http://www.gaia-gis.it/spatialite-2.4.0/spatialite-sql-2.4.html#p12. The following spatial relationships can be ...
user avatar
  • 896
12 votes
Accepted

Moving SpatiaLite Table into PostgreSQL using ogr2ogr?

And, after a bit more reading the ogr2ogr help I found the answer more easily than I expected. ogr2ogr -f "PostgreSQL" PG:"dbname=db" spatialitedb -sql "SELECT * FROM table" -dialect spatialite -nln ...
user avatar
12 votes

Converting csv to kml/geojson using ogr2ogr or equivalent command line tools?

Starting with GDAL 2.1, it is possible to directly specify the potential names of the columns that can contain X/longitude and Y/latitude with the X_POSSIBLE_NAMES and Y_POSSIBLE_NAMES open option....
user avatar
12 votes
Accepted

Resolving relation does not exist in PostGIS?

This is a case sensitivity/quoting issue. "PointsForGpxExport" and PointsForGpxExport are not the same table name. PostgreSQL, per the SQL standard, case-folds unquoted identifiers. (It case-folds ...
user avatar
12 votes
Accepted

Bulk csv to shapefile (using ogr2ogr)

A modern-enough GDAL can convert CSV files directly into shapefiles. For the test.csv file in the documentation, http://www.gdal.org/drv_csv.html - you can do: ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:...
user avatar
  • 52.1k
11 votes

Piping data to ogr2ogr

You can use URLs as paths to OGR data sources, so you don't need curl or pipes. ogr2ogr -f KML countries.kml https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/...
user avatar
  • 39.9k
11 votes
Accepted

Repairing broken GDAL and PROJ.4 on Ubuntu?

After several wrong ways, including multiple reinstall of libproj0 package and even installing QGIS from sources different from the Ubuntu Software Center default (I think it's from the official QGIS ...
user avatar
  • 1,487
11 votes
Accepted

og2ogr character encoding problem

Type -lco before the ENCODING=UTF-8 part, a.k.a -lco ENCODING=UTF-8 and it should work (it did for me and my strange Polish characters).
user avatar
  • 126
11 votes
Accepted

ogr2ogr from file to PostgreSQL 12 table fails

I have the same issue (this morning) and started to search for some solutions and the only thing I found is that is a GDAL PG12 problem, which was solved in a commit of GDAL in July 2019. This means ...
user avatar
  • 533
11 votes
Accepted

Creating point on surface in QGIS

In QGIS 3.10 this option is not available using Vector > Geometry Tools > Centroids. However if you search for "Point on surface" in the Processing Toolbox (Ctrl+Shift+T) using QGIS ...
user avatar
  • 425
10 votes
Accepted

How to rename ogr2ogr tables on export to Spatialite?

From the ogr2ogr docs, use the -nln name option to assign an alternate name to the new layer.
user avatar
  • 39.9k
10 votes

Execute ogr2ogr from python

subprocess.call and subprocess.check_call both take lists with strings as input and separate each argument with a blank space. Your command would be: import os, subprocess base_path = "some/file/...
user avatar
  • 9,450
10 votes
Accepted

ogr2ogr difference between -a_srs and -s_srs

From the ogr2ogr page: -a_srs srs_def: Assign an output SRS -t_srs srs_def: Reproject/transform to this SRS on output -s_srs srs_def: Override source SRS So we can see they have very different ...
user avatar
  • 75.1k
10 votes
Accepted

ogr2ogr keep geometry when converting to CSV?

For the coordinates, see Getting list of coordinates for points in layer using QGIS? or Reproject CSV file to different projecton using ogr2ogr -lco GEOMETRY=AS_XY is what you need. For the error ...
user avatar
  • 75.1k
10 votes
Accepted

Action to directly export selected features as GPX

If it were a single feature to export you could use an action with canvas scope (i.e., interacting with the canvas), but since you may export several features, an action with layer scope fits better. ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible