GithubHelp home page GithubHelp logo

gdal-cheat-sheet's Introduction

Cheat sheet for GDAL/OGR command-line tools

Vector operations

Get vector information

ogrinfo -so input.shp

Print vector extent

ogrinfo input.shp layername | grep Extent

List vector drivers

ogr2ogr --formats

Convert between vector formats

ogr2ogr -f "GeoJSON" output.json input.shp

Clip vectors by bounding box

ogr2ogr -f "ESRI Shapefile" output.shp input.shp -clipsrc x_min y_min x_max y_max

Clip one vector by another

ogr2ogr -clipsrc clipping_polygon.shp output.shp input.shp

Reproject vector:

ogr2ogr output.shp -t_srs "EPSG:4326" input.shp

Merge vectors:

ogr2ogr merged.shp input1.shp
ogr2ogr -update -append merged.shp input2.shp -nln merged

Extract from a vector file based on query

To extract features with STATENAME 'New York','New Hampshire', etc. from states.shp

ogr2ogr -where 'STATENAME like "New%"' states_subset.shp states.shp

To extract type 'pond' from water.shp

ogr2ogr -where "type = pond" ponds.shp water.shp

Raster operations

Get raster information

gdalinfo input.tif

List raster drivers

gdal_translate --formats

Convert between raster formats

gdal_translate -of "GeoTIFF" input.grd output.tif

Reproject raster:

gdalwarp -t_srs "EPSG:102003" input.tif output.tif

Merge rasters

gdal_merge.py -o merged.tif input1.tif input2.tif

Create a hillshade from a DEM

gdaldem hillshade -of PNG input.tif hillshade.png

Change light direction:

gdaldem hillshade -of PNG -az 135 input.tif hillshade_az135.png 

Apply color ramp to a DEM
First, create a color-ramp.txt file:
(Height, Red, Green, Blue)

	0 110 220 110
	900 240 250 160
	1300 230 220 170
	1900 220 220 220
	2500 250 250 250

Then apply those colors to a DEM:

gdaldem color-relief input.tif color_ramp.txt color-relief.tif

Create slope-shading from a DEM
First, make a slope raster from DEM:

	gdaldem slope input.tif slope.tif 

Second, create a color-slope.txt file:
(Slope angle, Red, Green, Blue)

0 255 255 255
90 0 0 0  

Finally, color the slope raster based on angles in color-slope.txt:

gdaldem color-relief slope.tif color-slope.txt slopeshade.tif

Resample raster

gdalwarp -ts 3672 2520 -r cubicspline st_helens_dem.tif 2x_st_helens_dem.tif

Burn vector into raster

gdal_rasterize -b 1 -i -burn -32678 -l layername input.shp input.tif

Create contours from DEM

gdal_contour -a elev -i 50 input_dem.tif output_contours.shp

Other

Convert KML to CSV (WKT)
First list layers in the KML file

ogrinfo -so input.kml

Convert the desired KML layer to CSV

ogr2ogr -f CSV output.csv input.kml -sql "select *,OGR_GEOM_WKT from some_kml_layer"

CSV points to SHP
Given input.csv

lon,lat,value
-81,32,13
-81,32,14
-81,32,15

Make a .dbf table from input.csv

ogr2ogr -f "ESRI Shapefile" output_dir/output.dbf input.csv

Use a text editor to create a .vrt file in the same directory

<OGRVRTDataSource>
  <OGRVRTLayer name="output">
    <SrcDataSource relativeToVRT="1">/output_dir</SrcDataSource>
    <SrcLayer>input</SrcLayer>
    <GeometryType>wkbPoint</GeometryType>
    <LayerSRS>WGS84</LayerSRS>
    <GeometryField encoding="PointFromColumns" x="lon" y="lat"/>
  </OGRVRTLayer>
</OGRVRTDataSource>

Create shapefile based on parameters listed in the .vrt

ogr2ogr -f "ESRI Shapefile" /output_dir parameters.vrt

Sources

http://live.osgeo.org/en/quickstart/gdal_quickstart.html

https://github.com/nvkelso/geo-how-to/wiki/OGR-to-reproject,-modify-Shapefiles

ftp://ftp.remotesensing.org/gdal/presentations/OpenSource_Weds_Andre_CUGOS.pdf

http://developmentseed.org/blog/2009/jul/30/using-open-source-tools-make-elevation-maps-afghanistan-and-pakistan/

http://linfiniti.com/2010/12/a-workflow-for-creating-beautiful-relief-shaded-dems-using-gdal/

http://nautilus.baruch.sc.edu/twiki_dmcc/bin/view/Main/OGR_example

gdal-cheat-sheet's People

Contributors

dwtkns avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.