
ogr2ogr by GDAL project is a command line tool for GIS data processing to “Convert simple features data between different file formats”. It can performs various operations like
- Converting data between different formats like shapefiles to GeoJSON, GeoJSON to shapefiles etc.
- Transforming data from one projection to another projection
- Doing spatial selection on data
- Exporting shapefiles, geojson etc. to SQL and vice versa
Lets start using ogr2ogr tool to transform our data. First, we will simply type ogr2ogr in command prompt to see what it displays. It will show various parameters that can be used to process our data.
1 |
ogr2ogr |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-select field_list] [-where restricted_where|@filename] [-progress] [-sql <sql statement>|@filename] [-dialect dialect] [-preserve_fid] [-fid FID] [-limit nb_features] [-spat xmin ymin xmax ymax] [-spat_srs srs_def] [-geomfield field] [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def] [-ct string] [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...] dst_datasource_name src_datasource_name [-lco NAME=VALUE] [-nln name] [-nlt type|PROMOTE_TO_MULTI|CONVERT_TO_LINEAR|CONVERT_TO_CURVE] [-dim XY|XYZ|XYM|XYZM|layer_dim] [layer [layer ...]] Advanced options : [-gt n] [-ds_transaction] [[-oo NAME=VALUE] ...] [[-doo NAME=VALUE] ...] [-clipsrc [xmin ymin xmax ymax]|WKT|datasource|spat_extent] [-clipsrcsql sql_statement] [-clipsrclayer layer] [-clipsrcwhere expression] [-clipdst [xmin ymin xmax ymax]|WKT|datasource] [-clipdstsql sql_statement] [-clipdstlayer layer] [-clipdstwhere expression] [-wrapdateline][-datelineoffset val] [[-simplify tolerance] | [-segmentize max_dist]] [-makevalid] [-addfields] [-unsetFid] [-relaxedFieldNameMatch] [-forceNullable] [-unsetDefault] [-fieldTypeToString All|(type1[,type2]*)] [-unsetFieldWidth] [-mapFieldType srctype|All=dsttype[,srctype2=dsttype2]*] [-fieldmap identity | index1[,index2]*] [-splitlistfields] [-maxsubfields val] [-explodecollections] [-zfield field_name] [-gcp ungeoref_x ungeoref_y georef_x georef_y [elevation]]* [-order n | -tps] [-nomd] [-mo "META-TAG=VALUE"]* [-noNativeData] Note: ogr2ogr --long-usage for full help. FAILURE: no target datasource provided |
For starters, we will use ogr2ogr –long-usage to get extra information. This command will display information on how to use this tool, what are various supported formats and information on arguments that can be used with ogr2ogr
1 |
ogr2ogr --long-usage |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-select field_list] [-where restricted_where|@filename] [-progress] [-sql <sql statement>|@filename] [-dialect dialect] [-preserve_fid] [-fid FID] [-limit nb_features] [-spat xmin ymin xmax ymax] [-spat_srs srs_def] [-geomfield field] [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def] [-ct string] [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...] dst_datasource_name src_datasource_name [-lco NAME=VALUE] [-nln name] [-nlt type|PROMOTE_TO_MULTI|CONVERT_TO_LINEAR|CONVERT_TO_CURVE] [-dim XY|XYZ|XYM|XYZM|layer_dim] [layer [layer ...]] Advanced options : [-gt n] [-ds_transaction] [[-oo NAME=VALUE] ...] [[-doo NAME=VALUE] ...] [-clipsrc [xmin ymin xmax ymax]|WKT|datasource|spat_extent] [-clipsrcsql sql_statement] [-clipsrclayer layer] [-clipsrcwhere expression] [-clipdst [xmin ymin xmax ymax]|WKT|datasource] [-clipdstsql sql_statement] [-clipdstlayer layer] [-clipdstwhere expression] [-wrapdateline][-datelineoffset val] [[-simplify tolerance] | [-segmentize max_dist]] [-makevalid] [-addfields] [-unsetFid] [-relaxedFieldNameMatch] [-forceNullable] [-unsetDefault] [-fieldTypeToString All|(type1[,type2]*)] [-unsetFieldWidth] [-mapFieldType srctype|All=dsttype[,srctype2=dsttype2]*] [-fieldmap identity | index1[,index2]*] [-splitlistfields] [-maxsubfields val] [-explodecollections] [-zfield field_name] [-gcp ungeoref_x ungeoref_y georef_x georef_y [elevation]]* [-order n | -tps] [-nomd] [-mo "META-TAG=VALUE"]* [-noNativeData] -f format_name: output file format name, possible values are: -f "AmigoCloud" -f "BAG" -f "BNA" -f "Carto" -f "Cloudant" -f "CouchDB" -f "CSV" -f "DGN" -f "DXF" -f "Elasticsearch" -f "ESRI Shapefile" -f "FITS" -f "FlatGeobuf" -f "Geoconcept" -f "GeoJSON" -f "GeoJSONSeq" -f "GeoRSS" -f "GML" -f "GPKG" -f "GPSBabel" -f "GPSTrackMaker" -f "GPX" -f "Interlis 1" -f "Interlis 2" -f "JML" -f "KML" -f "LIBKML" -f "MapInfo File" -f "MapML" -f "MBTiles" -f "Memory" -f "MSSQLSpatial" -f "MVT" -f "MySQL" -f "netCDF" -f "NGW" -f "ODBC" -f "ODS" -f "OGR_GMT" -f "PCIDSK" -f "PDF" -f "PDS4" -f "PGDUMP" -f "PostgreSQL" -f "S57" -f "Selafin" -f "SQLite" -f "TIGER" -f "VDV" -f "VICAR" -f "WAsP" -f "XLSX" -append: Append to existing layer instead of creating new if it exists -overwrite: delete the output layer and recreate it empty -update: Open existing output datasource in update mode -progress: Display progress on terminal. Only works if input layers have the "fast feature count" capability -select field_list: Comma-delimited list of fields from input layer to copy to the new layer (defaults to all) -where restricted_where: Attribute query (like SQL WHERE) -wrapdateline: split geometries crossing the dateline meridian (long. = +/- 180deg) -datelineoffset: offset from dateline in degrees (default long. = +/- 10deg, geometries within 170deg to -170deg will be split) -sql statement: Execute given SQL statement and save result. -dialect value: select a dialect, usually OGRSQL to avoid native sql. -skipfailures: skip features or layers that fail to convert -gt n: group n features per transaction (default 20000). n can be set to unlimited -spat xmin ymin xmax ymax: spatial query extents -simplify tolerance: distance tolerance for simplification. -segmentize max_dist: maximum distance between 2 nodes. Used to create intermediate points -dsco NAME=VALUE: Dataset creation option (format specific) -lco NAME=VALUE: Layer creation option (format specific) -oo NAME=VALUE: Input dataset open option (format specific) -doo NAME=VALUE: Destination dataset open option (format specific) -nln name: Assign an alternate name to the new layer -nlt type: Force a geometry type for new layer. One of NONE, GEOMETRY, POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT, MULTIPOLYGON, or MULTILINESTRING, or PROMOTE_TO_MULTI or CONVERT_TO_LINEAR. Add "25D" for 3D layers. Default is type of source layer. -dim dimension: Force the coordinate dimension to the specified value. -fieldTypeToString type1,...: Converts fields of specified types to fields of type string in the new layer. Valid types are : Integer, Integer64, Real, String, Date, Time, DateTime, Binary, IntegerList, Integer64List, RealList, StringList. Special value All will convert all fields to strings. -fieldmap index1,index2,...: Specifies the list of field indexes to be copied from the source to the destination. The (n)th value specified in the list is the index of the field in the target layer definition in which the n(th) field of the source layer must be copied. Index count starts at zero. There must be exactly as many values in the list as the count of the fields in the source layer. We can use the 'identity' setting to specify that the fields should be transferred by using the same order. This setting should be used along with the append setting. -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 Srs_def can be a full WKT definition (hard to escape properly), or a well known definition (i.e. EPSG:4326) or a file with a WKT definition. |
We will start with basic commands like to convert Shapefile to GeoJSON. We will use follwing command to do this. Argument -f is used to specify format_name for our output data. In our case, it is GeoJSON. output.geojson is output file and input.shp is input file. Please kep in mind the order of file name. First is your output file name and second is your input file name
1 |
ogr2ogr -f GeoJSON output.geojson input.shp |
To tranform the data from one projection to another projection system, use following command. Arguments -s_srs is the current projection of your input data and -t_srs is the target projection system to which you want to transform your data. output_3857.shp is outpu file name and input.shp is input file name
1 |
ogr2ogr -s_srs EPSG:3035 -t_srs EPSG:3857 -f 'ESRI Shapefile' output_3857.shp input.shp |
To clip you data use following command is used. Argument -spat is used to specify spatial extent which will clip your data. It will return those features which intersects with extent.
1 2 3 |
ogr2ogr -spat \ 4381293.12851205188781023 3093477.68962127156555653, 4391081.58145586121827364 3096387.60672014206647873 \ <strong>-</strong>f 'ESRI Shapefile' output_clip<strong>.</strong>shp input<strong>.</strong>shp |
To import your shape file to postgis, use following command. Argument -f PosgreSQL specify the output data format, in our case it is postgis table. Argument -nln specify the schem and table name where shape file will be imported. PG:”host=localhost user=postgres dbname=postgres password=admin” is used to specify the database details
1 2 |
ogr2ogr -f PostgreSQL PG:"host=localhost user=postgres dbname=postgres password=admin" \ input<strong>.</strong>shp <strong>-</strong>nln public<strong>.</strong>input |
To know more about ogr2ogr tool, click on following link.