Geoscience Reference
In-Depth Information
Bear in mind that too many (required) options can make for complex command
line utilities.
Oncewe have defined our command line arguments, we need towrite the functions
to create the vector datasource and generate the pairs of coordinates that form the
sampling frame. In the first instance, we write a function to create the output vector
file whose format is determined by the command line argument output_format ,
which the user defines. In the code snippet, two formats are included: ESRI Shapefile
and a Spatialite database.
For the purposes of the example, we set the Spatial Reference to WGS84
(EPSG:4326), but this can be modified to handle another reference system or can
be read directly from another spatial dataset. We define the datasource name as
xypoints.shp and xypoints.db for the ESRI Shapefile and Spatialite file
respectively. Note that for Spatialite, we define the output format as SQLite. Both
have a layer called xysample .
We create three attributes (plot_id, x_coord and y_coord), which are populated
automatically during the creation of the sampling frame.
def createVector(output_format):
#by default we set EPSG:4326
spatialRef = ogr.osr.SpatialReference()
if output_format =='Shp':
driver = ogr.GetDriverByName('ESRI Shapefile')
output_ds =
driver.
CreateDataSource('xypoints.shp')
layer = output_ds.CreateLayer('xysample',
geom_type=ogr.wkbPoint)
elif output_format =='SQLite':
driver = ogr.GetDriverByName('SQLite')
output_ds =
driver.
CreateDataSource('./xypoint.db')
layer = output_ds.CreateLayer('xysample',
geom_type=ogr.wkbPoint)
layerDefinition = layer.GetLayerDefn()
#create three attributes to add to each point (index, x,
y)
index = ogr.FieldDefn("plot_id")
x_coord = ogr.FieldDefn("x")
y_coord = ogr.FieldDefn("y")
layer.CreateField(index)
layer.CreateField(x_coord)
layer.CreateField(y_coord)
return output_ds, layer, layerDefinition
After having defined the vector layer using the OGRmethods, we can nowdevelop
the algorithm to generate the coordinate pairs to form the sampling frame for either
 
Search WWH ::




Custom Search