Hot answers tagged

35

Grab a copy of the ogr2ogr Python port, which is distributed with the GDAL source code download or can be found here: http://svn.osgeo.org/gdal/trunk/gdal/swig/python/samples/ogr2ogr.py Once you import that into your code, you can use it like this: import ogr2ogr def main(): #note: main is expecting sys.argv, where the first argument is the script name ...


35

With ogr2ogr (GDAL >= 1.10 with SpatiaLite support): ogr2ogr output.shp input.shp -dialect sqlite -sql "SELECT ST_Union(geometry), dissolve_field FROM input GROUP BY dissolve_field"


31

You have to force the SHP geometry type (because the geometry type of GeoJSON Geometry Collection is not supported in SHPs) and use the -skipfailures option: ogr2ogr -nlt POINT -skipfailures points.shp geojsonfile.json OGRGeoJSON ogr2ogr -nlt LINESTRING -skipfailures linestrings.shp geojsonfile.json OGRGeoJSON ogr2ogr -nlt POLYGON -skipfailures polygons....


31

You miss a minus sign before where and the select is not necessary, so it should be: ogr2ogr -where ID="1" outfile.shp infile.shp or if you have to do more complex query on your input data: ogr2ogr -sql "SELECT * FROM infile WHERE ID='1'" outfile.shp infile.shp If ID is a field of Integer type, substitute ID='1' with ID=1. Notes: -f "ESRI Shapefile" is ...


25

Using Homebrew you should: brew install gdal --with-postgresql or with older versions of gdal: brew install gdal --with-postgres if you have already installed gdal with brew before but without postgresql support, just brew uninstall gdal


24

Try adding appropriate host and port arguments. And BTW, when I import a large GDB file, I also add these flags: -overwrite (erases any mess you may have already inserted) and -progress (displays a dot or number for every 10,000 or 10% records added): --config PG_USE_COPY YES (greatly improves speed) So the command (which should be a one-liner but i ...


21

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 @LeeHachadoorian's comment that -nlt PROMOTE_TO_MULTI be used as a default solution, rather than nlt GEOMETRY, as the former promotes best practice in addition to ...


20

Assuming you want to reproject a shapefile, one way within QGIS is to load the file, right-click on the layer, select Save As…, and then the following window appears: If you click browse beside CRS you can choose a new projection to save your file in. EDIT: To reproject all shapfiles in the one folder, something like this could work: set "str1=_projected"...


20

ogrinfo -so -al yourshapefile.shp This will give you geometry type, number of features/shapes, bounding box corners, projection information, and the name of each attribute file as well as the datatype stored in those attributes.


19

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/natural-earth-vector/master/geojson/ne_50m_admin_0_countries.geojson" | ogr2ogr -f "KML" countries.kml /vsistdin/


19

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 with for example GeoPackage driver what happens if you do not use the -nln switch with a datastore that can contain many layers. ogr2ogr -f gpkg merged.gpkg a....


17

With small scripting it would be doable. With something like following you should be able to add column to a shapefile in all shapefiles in a folder, and merge them to merged.shp file for %f in (*.shp) do ( ogrinfo %f -sql "ALTER TABLE %f ADD COLUMN filename character(15)" ogrinfo %f -sql "UPDATE TABLE %f filename = '%f'" ogr2ogr -update -append ...


17

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


16

Use the -nlt option. In this case you want: -nlt MULTILINESTRING There is also PROMOTE_TO_MULTI (GDAL 1.10 and later), which chooses either MULTILINESTRING or MULTIPOLYGON depending on the input layer. The use case for this is "doing a mass conversion of shapefiles that [mix] different types of geometries".


16

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 to try and represent the width and precision information, if available using NUMERIC(width,precision) or CHAR(width) types. If "NO" then the types FLOAT8, ...


15

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 Postgis, I thought you might appreciate a pure Postgres/Postgis approach. I have used this approach with Google Maps, so it works well if you need to create ...


15

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 the field names in your GeoPackage data to lowercase in the PostgreSQL table. To maintain case you would need to use the lco option like: ogr2ogr -f PostgreSQL "...


14

SpatialLite has no way of converting multi-geometries to single-parts itself. There are some 'CastTo' functions but they are for special cases (where your multigeometry contains a single geometry - it won't fan-out). I have seen a reference to a function in the SpatialLite GUI but never found it (perhaps you need to compile from the latest source code. I'...


14

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 Which absolutely fixed the same error (ERROR 6: Unable to load PROJ.4 library (libproj.so)) for me. On the x86_64 architecture you may need to use this ...


13

So this python script will take a json input file as detailed above and write properly formatted geojson to the output file. run the script in terminal by doing python scriptname.py input_file.json output_file.json #! usr/bin/env python from sys import argv from os.path import exists import simplejson as json script, in_file, out_file = argv data = ...


13

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"> <SrcDataSource>test.csv</SrcDataSource> <GeometryType>wkbPoint</GeometryType> <LayerSRS>WGS84</LayerSRS> ...


12

You can convert multipoint layer to single points with menu Vector -> Geometry tools -> Multipart to Singleparts and import the new layer into PostGIS.


12

You are looking for the -progress switch ogr2ogr -progress ... You can see the other options by doing ogr2ogr --help Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-select field_list] [-where restricted_where] [-progress] [-sql <sql statement>] [-dialect dialect] [-preserve_fid] [-...


12

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 new_table EDIT: As suggested by user30184 in the comments a cleaner, simpler method is: ogr2ogr -f "PostgreSQL" PG:"dbname=db" spatialitedb ...


11

For unix bash: #!/bin/bash for FILE in *.mif # cycles through all files in directory (case-sensitive!) do echo "converting file: $FILE..." FILENEW=`echo $FILE | sed "s/.mif/_new.shp/"` # replaces old filename ogr2ogr \ -f "ESRI Shapefile" \ "$FILENEW" "$FILE" done exit If you're working in a *nix-based OS (ie Linux or OS X), there are ...


11

Using GDAL >= 1.10.0 and its OGR Virtual Format, we can write a VRT file named, for example, merge.vrt (see Example: Union layer (GDAL >= 1.10.0)): <OGRVRTDataSource> <OGRVRTUnionLayer name="unionLayer"> <OGRVRTLayer name="source1"> <SrcDataSource>source1.shp</SrcDataSource> </OGRVRTLayer> ...


11

http://www.bostongis.com/PrinterFriendly.aspx?content_name=ogr_cheatsheet use the flag -nln to specify the name of your target table


11

The trick is to get a list of layers in the KML that you can than iterate over individually. A linux solution (bash) could look like... for layer in "$(ogrinfo -ro -so -q file.kml | cut -d ' ' -f 2)" do ogr2ogr -f "GeoJSON" "file_${layer}.json" file.kml "${layer}" done You might also have an issue of spaces ' ' in the kml layer name. Those can be ...


11

As 102100 and 3857 are mathematically identical, I think you can safely ignore the errors and just continue with overriding the source srs using the -s_srs flag. You can try adding the definition for 102100 in your $GDAL_DATA$ but it's not really recommended. If you really want to do it though, here's the method and the accompanying warning from Frank ...


11

The correct syntax is: ogr2ogr -f GeoJSON geojson.json Areas.kml You can also use togeojson (Online Maps moving forward, from KML to GeoJSON) togeojson Areas.kml > geojson.json


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