Hot answers tagged

35

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....


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"


32

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 ...


22

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 ...


21

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.


21

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

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


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....


19

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 "...


17

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 = ...


17

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, ...


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".


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

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 ...


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'...


13

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] [-...


13

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 ...


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> ...


13

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 clip the clipping file, and work recursively, so it will also clip the shapefiles in sub-folders. If you don't want it recursive: for %f in (*.shp) do ...


13

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 name of geometry column in new table as follows: ogr2ogr -lco GEOMETRY_NAME=geom -append -f "PostgreSQL" PG:"dbname=database host=localhost user=username ...


13

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.shp output.shp input.shp replace output and input with your files name and their path. If you need more details about OSGEOW have a look to this page


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

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 ...


12

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


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 ...


12

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 case ogr2ogr \ -f 'Esri Shapefile' \ $OUTPUT_PATH \ -t_srs $OUT_SRS \ "PG:dbname=${DB_NAME} user=${DB_USERNAME} password=${DB_PASSWORD} schemas=...


12

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 used within a query ST_Equals - ST_Disjoint - ST_Touches - ST_Within - ST_Overlaps - ST_Crosses - ST_Intersects - ST_Contains - ST_Relate For this type of ...


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> ...


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