Geoscience Reference
In-Depth Information
def main():
try:
from osgeo import ogr
else:
import ogr
import sys
import os
import numpy as np
import argparse
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
parser = argparse.ArgumentParser()
parser.add_argument("-d","--datasource", help="Relative
pathname of datasource", required=True, type="string")
parser.add_argument("-c","--color", help="Display
color", dest="color", required=True, type="string")
args = parser.parse_args()
#here we assign the pars'ed arguments to variable names
datasource = args.datasource
color = args.color
#call the function plotShp
plotShp(datasource, color)
It is now necessary to define the plotVector function that will include the
necessary code to plot the Shapefile. Initially, we use OGR to access the individual
feature layers from the Shapefile. The reader is referred back to Fig. 14.2 as it illus-
trates the hierarchical structure of the OGR data model. In this example, the original
datasource is opened, then the number of layers is retrieved and finally the layer is
retrieved from the datasource.
def plotVector(datasource, color):
#here we extract the first feature layer from the Shapefile
ds = ogr.Open(datasource)
number_layers = ds.GetLayerCount()
layer = ds.GetLayer(0)
In the following code block, the spatial extent or bounding box of the layer is
obtained using the GetExtent method from the OGR API. It returns two sets of coor-
dinates that define the lower left and upper right of the bounding box that encompasses
the vector features. This set of coordinates is used to define the limits of the figure.
We also prepare the figure for plotting (a 1
×
1 grid defined by 111) and set limits
based on the layers extent.
 
 
Search WWH ::




Custom Search