{"id":21415096,"url":"https://github.com/marcelovilla/gdal-py-snippets","last_synced_at":"2026-05-07T20:10:57.065Z","repository":{"id":124859715,"uuid":"144775050","full_name":"marcelovilla/gdal-py-snippets","owner":"marcelovilla","description":"Small different-purpose programs using gdal-python bindings (and a fair amount of NumPy)","archived":false,"fork":false,"pushed_at":"2019-06-26T01:17:27.000Z","size":2478,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-23T05:29:35.355Z","etag":null,"topics":["gdal","gdal-python","numpy","ogr","pandas","python","raster","shapefile"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marcelovilla.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-14T21:43:12.000Z","updated_at":"2024-05-16T16:38:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"ed60e81e-f198-4d1d-a7b2-64f93a762aac","html_url":"https://github.com/marcelovilla/gdal-py-snippets","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelovilla%2Fgdal-py-snippets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelovilla%2Fgdal-py-snippets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelovilla%2Fgdal-py-snippets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelovilla%2Fgdal-py-snippets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcelovilla","download_url":"https://codeload.github.com/marcelovilla/gdal-py-snippets/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243911720,"owners_count":20367741,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gdal","gdal-python","numpy","ogr","pandas","python","raster","shapefile"],"created_at":"2024-11-22T18:44:09.724Z","updated_at":"2026-05-07T20:10:52.043Z","avatar_url":"https://github.com/marcelovilla.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gdal-py-snippets\nThis repository presents a wide range of different-purpose small examples using the gdal-python bindings (both `gdal` and `ogr`). It uses a fair amount of `numpy` as well, along with other third party packages such as `pandas`.\n\nThe programs presented in this repository were developed using Python 3.6.5 and the following packages versions:\n* `gdal` version: 2.3.3\n* `numpy` version: 1.14.3\n* `pandas` version: 0.24.2\n\n\n### Data\nAll the snippets in this repository use open data. Here is a table with the description of each dataset used\u003csup\u003e*\u003c/sup\u003e and its sources:\n\n| Dataset                   | Description                                             | Type   | Extension | Source             |\n|---------------------------|---------------------------------------------------------|--------|-----------|--------------------|\n| COL_msk_alt               | Elevation for Colombia (country mask)                   | Raster | .vrt      | [DIVA_GIS][1]      |\n| wc2.0_10m_prec_01         | Average January global precipitation from 1970 to 2000. | Raster | .tif      | [WorldClim][2]     |\n| COL_rails                 | Colombian railroads                                     | Vector | .shp      | [DIVA_GIS][1]      |\n| ne_110m_admin_0_countries | Administrative boundaries (countries) of the world.     | Vector | .shp      | [Natural Earth][3] |\n\n\u003csup\u003e*I do not own any of the datasets here presented.\u003c/sup\u003e\n\n\n### Snippets\nHere is a brief explanation for each snippet:\n\n#### 1. [attribute_table_to_csv][4]\nSaves a shapefile's attribute table to a given csv file using `ogr` and `pandas`. The program iterates through each feature in the shapefile and then iterates through each one of its fields, storing the values in a `DataFrame`. The `DataFrame` is then converted to a csv file.\n\n#### 2. [compute_proximity][5]\nComputes proximity (euclidean distance) in pixels from each cell to a target value using the `gdal.ComputeProximity()` function. The input raster is the output from [rasterize][9].\n\n#### 3. [euclidean_distance][6]\nComputes the euclidean distance (in pixels) from each cell to a target value. Similar to [compute_proximity][5] but instead of using `gdal`'s built-in function an own implementation of the distance computation is presented. In this case, the target values are the rasterized lines produced in [rasterize][9]. This snippet uses `numpy` to vectorize the distance computation (following [Pythagoras' theorem][7]). It creates a distance matrix to each target value (*i.e.* each value different from NoData). Then, the minimum distance value for each cell is retrieved and a single distance matrix is returned.\n\n#### 4. [extract_lowest_cell_values][8]\nExtracts an arbitrary percentage (5% in this case) of the lowest cell values in the raster.\n\n#### 5. [get_fields_info][9]\nLoops through each field in a shapefile and prints information about them.\n\n#### 6. [get_pixel_coordinates][10]\nCreates two grids with the corresponding x and y coordinates of each pixel center in a raster.\n\n#### 7. [get_pixel_values][11]\nGets an array of pixel values correspondent to a set of points within a raster's extent. Points are presented as a tuple of 1D arrays with x and y coordinates. It converts coordinates to array indices and retrieve the values by indexing the data array.\n\n#### 7. [raster_up_sample][12]\u003csup\u003e*\u003c/sup\u003e\nResamples (up-samples) a raster to a higher resolution. In order to do this it reads the original `GeoTransform` and changes the pixel width and pixel height to be proportionally smaller. Then it reads the data from the original dataset specifying `buf_xsize` and `buf_ysize` so the array where the data is going to be stored fits the new dimensions. When specifying a bigger buffer than the original raster dimensions in the `ReadAsArray()` method, original values are repeated to fit the new dimensions.\n\n\u003csup\u003e*This snippet was slightly adapted from one example presented by Chris Garrard in [Geoprocessing with Python's][13] 9th chapter.\u003c/sup\u003e\n\n#### 8. [rasterize][14]\nRasterizes a shapefile. It uses `ogr` to open a shapefile and get its extent. Then it creates an empty raster with the same extent as the shapefile and an arbitrary value for the pixel resolution. It uses the `gdal.RasterizeLayer()` function to burn the raster´s band (*i.e.* first and only band) with a 1 where the shapefile presents a feature. The rest is set as NoData.\n\n[1]: https://www.diva-gis.org/gdata\n[2]: http://worldclim.org/version2\n[3]: https://www.naturalearthdata.com/downloads/110m-cultural-vectors/110m-admin-0-countries/\n[4]: https://github.com/marcelovilla9/gdal-py-snippets/blob/master/scripts/attribute_table_to_csv.py\n[5]: https://github.com/marcelovilla9/gdal-py-snippets/blob/master/scripts/compute_proximity.py\n[6]: https://github.com/marcelovilla9/gdal-py-snippets/blob/master/scripts/euclidean_distance.py\n[7]: https://en.wikipedia.org/wiki/Pythagorean_theorem\n[8]: https://github.com/marcelovilla9/gdal-py-snippets/blob/master/scripts/extract_lowest_cell_values.py\n[9]: https://github.com/marcelovilla9/gdal-py-snippets/blob/master/scripts/get_fields_info.py\n[10]: https://github.com/marcelovilla9/gdal-py-snippets/blob/master/scripts/get_pixel_coordinates.py\n[11]: https://github.com/marcelovilla9/gdal-py-snippets/blob/master/scripts/get_pixel_values.py\n[12]: https://github.com/marcelovilla9/gdal-py-snippets/blob/master/scripts/raster_up_sample.py\n[13]: https://www.manning.com/books/geoprocessing-with-python\n[14]: https://github.com/marcelovilla9/gdal-py-snippets/blob/master/scripts/rasterize.py\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcelovilla%2Fgdal-py-snippets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcelovilla%2Fgdal-py-snippets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcelovilla%2Fgdal-py-snippets/lists"}