Difference between revisions of "Vector Format Conversion Using ogr2ogr"
Ellis Symons (talk | contribs) |
Ellis Symons (talk | contribs) |
||
(9 intermediate revisions by the same user not shown) | |||
Line 16: | Line 16: | ||
==Bulk Format Conversion== | ==Bulk Format Conversion== | ||
<Br> | <Br> | ||
− | '''Convert All Files Within Folder to GPKG'''<br> | + | '''Convert All Files Within Folder to a Single GPKG'''<br> |
<pre>ogr2ogr.exe -f GPKG all_layers.gpkg ./path/to/dir</pre> | <pre>ogr2ogr.exe -f GPKG all_layers.gpkg ./path/to/dir</pre> | ||
− | '''Convert All Files Within Folder to GPKG with Wildcard'''<br> | + | '''Convert All Files Within Folder to a Single GPKG with Wildcard'''<br> |
<pre>ogr2ogr.exe -f GPKG all_layers.gpkg ./path/to/dir/*.shp</pre> | <pre>ogr2ogr.exe -f GPKG all_layers.gpkg ./path/to/dir/*.shp</pre> | ||
'''Convert All Files From a GPKG to a SHP'''<br> | '''Convert All Files From a GPKG to a SHP'''<br> | ||
Line 27: | Line 27: | ||
'''Convert Single Layer From GPKG''' - ''Uses an SQL statement to select all features from a given layer''<Br> | '''Convert Single Layer From GPKG''' - ''Uses an SQL statement to select all features from a given layer''<Br> | ||
<pre>ogr2ogr.exe -f "ESRI Shapefile" -sql "SELECT * FROM '2d_code_M01_R'" 2d_code_M01_R.shp all_layers.gpkg</pre> | <pre>ogr2ogr.exe -f "ESRI Shapefile" -sql "SELECT * FROM '2d_code_M01_R'" 2d_code_M01_R.shp all_layers.gpkg</pre> | ||
+ | '''Convert Single Layer From GPKG and convert the layer type'''<br> | ||
+ | :* ''<tt>-nlt <geometry type></tt>'': assigns the geometry type of the output layer - typical values [POLYGON | LINESTRING | POINT]. See <b><u>[https://gdal.org/programs/ogr2ogr.html#ogr2ogr gdal ogr2ogr documention]</u></b> for other options. | ||
+ | <pre>ogr2ogr.exe -f "ESRI Shapefile" -sql "SELECT * FROM '2d_code_M01_R'" -nlt POLYGON 2d_code_M01_R.shp all_layers.gpkg</pre> | ||
+ | '''Convert Single Layer From GPKG and convert the layer type and convert multipart geometries to singlepart'''<br> | ||
+ | :* ''<tt>-explodecollections</tt>'': splits multipart geometries into individual features. | ||
+ | <pre>ogr2ogr.exe -f "ESRI Shapefile" -sql "SELECT * FROM '2d_code_M01_R'" -nlt POLYGON -explodecollections 2d_code_M01_R.shp all_layers.gpkg</pre> | ||
'''Convert Single SHP Into a New GPKG Where Layer Name is Different From Database Name'''<br> | '''Convert Single SHP Into a New GPKG Where Layer Name is Different From Database Name'''<br> | ||
:* ''<tt>-nln <layername></tt>'': assigns the name of the layer within database | :* ''<tt>-nln <layername></tt>'': assigns the name of the layer within database | ||
Line 39: | Line 45: | ||
:* ''<tt>-where <clause></tt>'': "-where" can be used to specify a clause to only select a given geometry type | :* ''<tt>-where <clause></tt>'': "-where" can be used to specify a clause to only select a given geometry type | ||
<pre> | <pre> | ||
− | ogr2ogr.exe -f "ESRI Shapefile" -where | + | ogr2ogr.exe -f "ESRI Shapefile" -where "OGR_GEOMETRY='Point'" 2d_zsh_P.shp 2d_zsh.mif |
− | ogr2ogr.exe -f "ESRI Shapefile" -where | + | ogr2ogr.exe -f "ESRI Shapefile" -where "OGR_GEOMETRY='LineString'" 2d_zsh_L.shp 2d_zsh.mif |
− | ogr2ogr.exe -f "ESRI Shapefile" -where | + | ogr2ogr.exe -f "ESRI Shapefile" -where "OGR_GEOMETRY='Polygon'" 2d_zsh_R.shp 2d_zsh.mif |
</pre> | </pre> | ||
'''MIF With Multiple Geometry Types To a Single GPKG'''<Br> | '''MIF With Multiple Geometry Types To a Single GPKG'''<Br> | ||
− | :* ''<tt>-nlt <geometry_type></tt>'': Defines geometry type for output layer. Useful when converting to formats that implement strict checks for geometry types (e.g. Don't allow "Singlepart" geometry and "Multipart" geometry in the same layer. | + | :* ''<tt>-nlt <geometry_type></tt>'': Defines the geometry type for the output layer. Useful when converting to formats that implement strict checks for geometry types (e.g. Don't allow "Singlepart" geometry and "Multipart" geometry in the same layer. |
<pre> | <pre> | ||
− | ogr2ogr.exe -f GPKG -nln 2d_zsh_P -where | + | ogr2ogr.exe -f GPKG -nln 2d_zsh_P -where "OGR_GEOMETRY='Point'" -nlt POINT 2d_zsh.gpkg 2d_zsh.mif |
− | ogr2ogr.exe -f GPKG -nln 2d_zsh_L -where | + | ogr2ogr.exe -f GPKG -nln 2d_zsh_L -where "OGR_GEOMETRY='LineString'" -nlt LINESTRING -append 2d_zsh.gpkg 2d_zsh.mif |
− | ogr2ogr.exe -f GPKG -nln 2d_zsh_R -where | + | ogr2ogr.exe -f GPKG -nln 2d_zsh_R -where "OGR_GEOMETRY='Polygon'" -nlt POLYGON -append 2d_zsh.gpkg 2d_zsh.mif |
</pre> | </pre> | ||
+ | <br> | ||
+ | {{Tips Navigation | ||
+ | |uplink=[[GDAL_Tips_and_Tricks#GDAL_Programs| Back to GDAL Tips and Tricks]] | ||
+ | }} |
Latest revision as of 21:27, 7 July 2022
This page lists useful batch file examples for using ogr2ogr.exe. For complete documentation on this tool please visit gdal.org/programs/ogr2ogr
Basic Format Conversion
Format conversion can be done using "-f <format_name>" then specifying the "<destination_filename> <source_filename>".
SHP to GPKG
ogr2ogr.exe -f GPKG 2d_code_M01_R.gpkg 2d_code_M01_R.shp
MIF to SHP - assuming only one geometry type exists in the MIF file
ogr2ogr.exe -f "ESRI Shapefile" 2d_code_M01_R.shp 2d_code_M01.mif
SHP to MIF - TAB and MIF use the same command just with different extensions on the destination file
ogr2ogr.exe -f "Mapinfo File" 2d_code_M01_R.mif 2d_code_M01.shp
MIF to SHP Force Overwrite an Existing File
- -overwrite: overwrite the output file if it exists
ogr2ogr.exe -f "ESRI Shapefile" -overwrite 2d_code_M01_R.shp 2d_code_M01.mif
Bulk Format Conversion
Convert All Files Within Folder to a Single GPKG
ogr2ogr.exe -f GPKG all_layers.gpkg ./path/to/dir
Convert All Files Within Folder to a Single GPKG with Wildcard
ogr2ogr.exe -f GPKG all_layers.gpkg ./path/to/dir/*.shp
Convert All Files From a GPKG to a SHP
ogr2ogr.exe -f GPKG ./path/to/outdir all_layers.gpkg
Convert Single Layer To/From GPKG
Convert Single Layer From GPKG - Uses an SQL statement to select all features from a given layer
ogr2ogr.exe -f "ESRI Shapefile" -sql "SELECT * FROM '2d_code_M01_R'" 2d_code_M01_R.shp all_layers.gpkg
Convert Single Layer From GPKG and convert the layer type
- -nlt <geometry type>: assigns the geometry type of the output layer - typical values [POLYGON | LINESTRING | POINT]. See gdal ogr2ogr documention for other options.
ogr2ogr.exe -f "ESRI Shapefile" -sql "SELECT * FROM '2d_code_M01_R'" -nlt POLYGON 2d_code_M01_R.shp all_layers.gpkg
Convert Single Layer From GPKG and convert the layer type and convert multipart geometries to singlepart
- -explodecollections: splits multipart geometries into individual features.
ogr2ogr.exe -f "ESRI Shapefile" -sql "SELECT * FROM '2d_code_M01_R'" -nlt POLYGON -explodecollections 2d_code_M01_R.shp all_layers.gpkg
Convert Single SHP Into a New GPKG Where Layer Name is Different From Database Name
- -nln <layername>: assigns the name of the layer within database
ogr2ogr.exe -f GPKG -nln 2d_code_M01_R new_database.gpkg 2d_code_M01_R.shp
Convert Single SHP Into an Existing GPKG
- -append: appends the layer into an existing database
ogr2ogr.exe -f GPKG -nln 2d_code_M01_R -append existing_database.gpkg 2d_code_M01_R.shp
Converting MIF File With Multiple Geometry Types
MIF With Multiple Geometry Types To SHP
- -where <clause>: "-where" can be used to specify a clause to only select a given geometry type
ogr2ogr.exe -f "ESRI Shapefile" -where "OGR_GEOMETRY='Point'" 2d_zsh_P.shp 2d_zsh.mif ogr2ogr.exe -f "ESRI Shapefile" -where "OGR_GEOMETRY='LineString'" 2d_zsh_L.shp 2d_zsh.mif ogr2ogr.exe -f "ESRI Shapefile" -where "OGR_GEOMETRY='Polygon'" 2d_zsh_R.shp 2d_zsh.mif
MIF With Multiple Geometry Types To a Single GPKG
- -nlt <geometry_type>: Defines the geometry type for the output layer. Useful when converting to formats that implement strict checks for geometry types (e.g. Don't allow "Singlepart" geometry and "Multipart" geometry in the same layer.
ogr2ogr.exe -f GPKG -nln 2d_zsh_P -where "OGR_GEOMETRY='Point'" -nlt POINT 2d_zsh.gpkg 2d_zsh.mif ogr2ogr.exe -f GPKG -nln 2d_zsh_L -where "OGR_GEOMETRY='LineString'" -nlt LINESTRING -append 2d_zsh.gpkg 2d_zsh.mif ogr2ogr.exe -f GPKG -nln 2d_zsh_R -where "OGR_GEOMETRY='Polygon'" -nlt POLYGON -append 2d_zsh.gpkg 2d_zsh.mif
Up |
---|
Back to GDAL Tips and Tricks |