-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Doc: First draft of a GDAL cookbook #13810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
geographika
wants to merge
1
commit into
OSGeo:master
Choose a base branch
from
geographika:cookbook
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| .. _cookbook: | ||
|
|
||
| ================================================================================ | ||
| GDAL Command-line Cookbook | ||
| ================================================================================ | ||
|
|
||
|
|
||
| .. contents:: | ||
| :depth: 3 | ||
|
|
||
| Introduction | ||
| ------------ | ||
|
|
||
| Welcome to the GDAL Users Cookbook. This guide provides practical examples for working with raster and vector data using the GDAL command-line tools. | ||
| It demonstrates common tasks such as raster analysis, vector geometry operations, and combining raster and vector workflows. | ||
| Examples includes code snippets in both Bash and PowerShell, or links to relevant examples elsewhere in the GDAL documentation. | ||
|
|
||
| General | ||
| ------- | ||
|
|
||
| Get the GDAL version | ||
| ++++++++++++++++++++ | ||
|
|
||
| .. tabs:: | ||
|
|
||
| .. code-tab:: bash | ||
|
|
||
| ogr2ogr --version | ||
| gdalinfo --version | ||
|
|
||
| .. code-tab:: bash gdal CLI | ||
|
|
||
| gdal --version | ||
|
|
||
| Raster | ||
| ------ | ||
|
|
||
| Resize a Raster | ||
| +++++++++++++++ | ||
|
|
||
| See :ref:`gdal_raster_resize_example_resize`. | ||
|
|
||
| Vector | ||
| ------ | ||
|
|
||
| Get the geometry field name for a dataset | ||
| +++++++++++++++++++++++++++++++++++++++++ | ||
|
|
||
| After running the commands below look for ``Geometry Column = `` in the output. | ||
| For example, if you see ``Geometry Column = geom``, then the geometry field name is ``geom``. | ||
|
|
||
| .. tabs:: | ||
|
|
||
| .. code-tab:: bash | ||
|
|
||
| # list layers | ||
| ogrinfo -so edges.gpkg | ||
| # list details for a specific layer | ||
| ogrinfo -so edges.gpkg Edges | ||
|
|
||
| .. code-tab:: bash gdal CLI | ||
|
|
||
| gdal vector info edges.gpkg | ||
|
|
||
| Buffer geometries | ||
| +++++++++++++++++ | ||
|
|
||
| You can use the :ref:`gdal_vector_buffer` command; see :ref:`gdal_vector_buffer_examples` for usage examples. | ||
| Alternatively, the SQLite dialect can be used, as shown below. | ||
|
|
||
| Buffer geometries using an attribute | ||
| ++++++++++++++++++++++++++++++++++++ | ||
|
|
||
| This example uses a ``lines.gpkg`` dataset containing a single layer named ``lines``, | ||
| with a geometry field named ``geom`` and an integer attribute named ``width``. The value | ||
| of this attribute is used as the buffer distance for each feature. | ||
|
|
||
| .. note:: | ||
|
|
||
| When creating derived geometries using SQL, avoid using ``SELECT *``. | ||
| Including the original geometry field will result in multiple geometry | ||
| columns in the output. Instead, explicitly list the required attributes | ||
| and return a single geometry column. | ||
|
|
||
| .. tabs:: | ||
|
|
||
| .. code-tab:: bash | ||
|
|
||
| ogr2ogr buffered-lines.gpkg lines.gpkg \ | ||
| -dialect SQLITE \ | ||
| -sql "SELECT fid, ST_Buffer(geom, width) AS geom FROM lines" \ | ||
| -overwrite \ | ||
| -nlt POLYGON -nln BufferedLines | ||
|
|
||
| .. code-tab:: bash gdal CLI | ||
|
|
||
| gdal vector pipeline \ | ||
| ! read lines.gpkg \ | ||
| ! sql "SELECT fid, ST_Buffer(geom, width) AS geom FROM lines" \ | ||
| ! set-geom-type --geometry-type Polygon \ | ||
| ! write buffered-lines.gpkg --output-layer=BufferedLines --overwrite --overwrite-layer | ||
|
|
||
| Combined Raster and Vector Workflows | ||
| ------------------------------------ | ||
|
|
||
| Burn a vector dataset into a raster | ||
| +++++++++++++++++++++++++++++++++++ | ||
|
|
||
| See :ref:`gdal_vector_rasterize_example_burn`. | ||
|
|
||
| Extract pixel values from a raster and apply them to a point dataset | ||
| ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
|
||
| Example dataset: ``points.gpkg`` contains a single layer called ``points`` with a geometry column named ``geometry``. | ||
|
|
||
| .. tabs:: | ||
|
|
||
| .. code-tab:: bash | ||
|
|
||
| export OGR_SQLITE_ALLOW_EXTERNAL_ACCESS="YES" | ||
| gdal vector sql points.gpkg \ | ||
| points-dem.gpkg \ | ||
| --sql "SELECT *, gdal_get_pixel_value('./dem.tif', 1, 'georef', ST_X(geometry), ST_Y(geometry)) as pixel FROM points" \ | ||
| --dialect SQLITE \ | ||
| --overwrite | ||
|
|
||
| .. code-tab:: ps1 | ||
|
|
||
| $env:OGR_SQLITE_ALLOW_EXTERNAL_ACCESS="YES" | ||
| gdal vector sql points.gpkg ` | ||
| points-dem.gpkg ` | ||
| --sql "SELECT *, gdal_get_pixel_value('./dem.tif', 1, 'georef', ST_X(geometry), ST_Y(geometry)) as pixel FROM points" ` | ||
| --dialect SQLITE ` | ||
| --overwrite | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Off topic for this PR, but we should probably improve
gdal raster pixel-infoto have a less convoluted way of doing this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was quite please with myself to get that example working 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll tackle that