I want to create a shapefile using the GDAL library. And I want this Shapefile, when created, to have a grid of square polygons. And where each polygon cell is 10 degrees by 10 degrees, ultimately I want this grid to cover the entire world. As well, I also want the shapefile to contain a decimal column 'LAND', which should be populated with 0.0 for all rows/cells.
Having confusion here because I've never used GDAL before and just sifting through the documents is confusing me. I think I understand how to create a shapefile something like the following:
from osgeo import ogr
from osgeo import osr
import gdal
# specifying that I want to work with a shapefile
DriverName = 'My Shapefile'
driver = ogr.GetDriverByName(DriverName)
Filename = 'points.shp'
Just a little lost here. Not sure exactly how this would be done through GDAL. I know if I am creating the world, the extent of the world is -180 deg to 180 deg longitude and -90 deg to 90 deg for latitude. And that I should create a nested loop somewhere to loop the outer of longitudes and the inner of latitudes, stepping a value of 10 each time for spatial resolution for the grid.
It's just the whole-set up and the library functionally is confusing not sure what way to go about all this.
The general structure of the code I'd think would be something like: - Import GDAL and other modules - create a spatial reference - Create an empty shapefile - create a layer in the shapefile and assign it the spatial reference - create a set of point geometries as a ring - add those point geometries in an order to a polygon geometry - create a feature and add the polygon Geometry to it - put the feature in the layer
I examine the GDAL library website and found this: link: https://pcjericks.github.io/py-gdalogr-cookbook/
and then more specifically a section that said "Create a New Shapefile and Add Data" link: https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#create-a-new-shapefile-and-add-data
I looked at the following but they did not help so much: link: Creating square grid polygon shapefile with Python?
and
link: Creating grid polygons from coordinates using R or Python
I am trying to piece all this information together in order to solve my task but it is proving difficult.
Please help how would I create a shapefile being a grid of 10x10degree polygons?
<-, except in function definitions. And no, it's not a dumb question at all. – Rodrigo Apr 26 '18 at 16:23Polygons(listaP) composed of one or morePolygon(p) (one, in this case), that will later be assembled together in the last 3 functions. If you look at theslotNamesof any shapfile, you'll get closer to understand how they work. – Rodrigo Apr 26 '18 at 16:26rgdalis derived from the same GDAL you're using). – Rodrigo Apr 26 '18 at 16:48vector()creates an empty array,flush.console()is just to ensure the print of the previous message (to flush it to the console, instead of wait for the end of the loop). – Rodrigo Apr 26 '18 at 16:49poly = org.Geometry(ogr.wkbPolygon)only once, before the two loops), and create/add rings only in the inner for. – Rodrigo Apr 26 '18 at 17:45poly, which is aorg.Geometry(ogr.wkbPolygon). Now you need to look how to save it to a file. That will probably be a single command. – Rodrigo Apr 26 '18 at 18:32