source: branches/0.5/win/PostNAS-0.5/bin/mkgraticule.py @ 23

Revision 23, 7.1 KB checked in by astrid.emde, 14 years ago (diff)
Line 
1#!/usr/bin/env python
2###############################################################################
3# $Id: mkgraticule.py 18216 2009-12-08 19:13:48Z rouault $
4#
5# Project:  OGR Python samples
6# Purpose:  Produce a graticule (grid) dataset.
7# Author:   Frank Warmerdam, warmerdam@pobox.com
8#
9###############################################################################
10# Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
11#
12# Permission is hereby granted, free of charge, to any person obtaining a
13# copy of this software and associated documentation files (the "Software"),
14# to deal in the Software without restriction, including without limitation
15# the rights to use, copy, modify, merge, publish, distribute, sublicense,
16# and/or sell copies of the Software, and to permit persons to whom the
17# Software is furnished to do so, subject to the following conditions:
18#
19# The above copyright notice and this permission notice shall be included
20# in all copies or substantial portions of the Software.
21#
22# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28# DEALINGS IN THE SOFTWARE.
29###############################################################################
30
31try:
32    from osgeo import osr
33    from osgeo import ogr
34except ImportError:
35    import osr
36    import ogr
37
38import string
39import sys
40
41#############################################################################
42def float_range(*args):
43    start = 0.0
44    step = 1.0
45    if (len(args) == 1):
46        (stop,) = args
47    elif (len(args) == 2):
48        (start, stop) = args
49    elif (len(args) == 3):
50        (start, stop, step) = args
51    else:
52        raise TypeError("float_range needs 1-3 float arguments")
53
54    the_range = []
55    steps = (stop-start)/step
56    if steps != int(steps):
57        steps = steps + 1.0
58    for i in range(int(steps)):
59        the_range.append(i*step+start)
60
61    return the_range
62
63
64#############################################################################
65def Usage():
66    print ('Usage: mkgraticule [-connected] [-s stepsize] [-substep substepsize]')
67    print ('         [-t_srs srs] [-range xmin ymin xmax ymax] outfile')
68    print('')
69    sys.exit(1)
70
71#############################################################################
72# Argument processing.
73
74t_srs = None
75stepsize = 5.0
76substepsize = 5.0
77connected = 0
78outfile = None
79
80xmin = -180
81xmax = 180
82ymin = -90
83ymax = 90
84
85i = 1
86while i < len(sys.argv):
87    if sys.argv[i] == '-connected':
88        connected = 1
89    elif sys.argv[i] == '-t_srs':
90        i = i + 1
91        t_srs = sys.argv[i]
92    elif sys.argv[i] == '-s':
93        i = i + 1
94        stepsize = float(sys.argv[i])
95    elif sys.argv[i] == '-substep':
96        i = i + 1
97        substepsize = float(sys.argv[i])
98    elif sys.argv[i] == '-range':
99        xmin = float(sys.argv[i+1])
100        ymin = float(sys.argv[i+2])
101        xmax = float(sys.argv[i+3])
102        ymax = float(sys.argv[i+4])
103        i = i + 4
104    elif sys.argv[i][0] == '-':
105        Usage()
106    elif outfile is None:
107        outfile = sys.argv[i]
108    else:
109        Usage()
110
111    i = i + 1
112   
113if outfile is None:
114    outfile = "graticule.shp"
115
116
117if substepsize > stepsize:
118    substepsize = stepsize
119   
120#############################################################################-
121# Do we have an alternate SRS?
122
123ct = None
124
125if t_srs is not None:
126    t_srs_o = osr.SpatialReference()
127    t_srs_o.SetFromUserInput( t_srs )
128
129    s_srs_o = osr.SpatialReference()
130    s_srs_o.SetFromUserInput( 'WGS84' )
131
132    ct = osr.CoordinateTransformation( s_srs_o, t_srs_o )
133else:
134    t_srs_o = osr.SpatialReference()
135    t_srs_o.SetFromUserInput( 'WGS84' )
136   
137#############################################################################-
138# Create graticule file.
139
140drv = ogr.GetDriverByName( 'ESRI Shapefile' )
141
142try:
143    drv.DeleteDataSource( outfile )
144except:
145    pass
146
147ds = drv.CreateDataSource( outfile )
148layer = ds.CreateLayer( 'out', geom_type = ogr.wkbLineString,
149                        srs = t_srs_o )
150
151#########################################################################
152# Not connected case.  Produce individual segments are these are going to
153# be more resilent in the face of reprojection errors.
154
155if not connected:
156    #########################################################################
157    # Generate lines of latitude.
158
159    feat = ogr.Feature( feature_def = layer.GetLayerDefn() )
160    geom = ogr.Geometry( type = ogr.wkbLineString )
161
162    for lat in float_range(ymin,ymax+stepsize/2,stepsize):
163        for long in float_range(xmin,xmax-substepsize/2,substepsize):
164
165            geom.SetPoint( 0, long, lat )
166            geom.SetPoint( 1, long+substepsize, lat )
167
168            err = 0
169            if ct is not None:
170                err = geom.Transform( ct )
171             
172            if err is 0:
173                feat.SetGeometry( geom )
174                layer.CreateFeature( feat )
175
176    #########################################################################
177    # Generate lines of longitude
178
179    for long in float_range(xmin,xmax+stepsize/2,stepsize):
180        for lat in float_range(ymin,ymax-substepsize/2,substepsize):
181            geom.SetPoint( 0, long, lat )
182            geom.SetPoint( 1, long, lat+substepsize )
183
184            err = 0
185            if ct is not None:
186                err = geom.Transform( ct )
187
188            if err is 0:
189                feat.SetGeometry( geom )
190                layer.CreateFeature( feat )
191
192               
193#########################################################################
194# Connected case - produce one polyline for each complete line of latitude
195# or longitude.
196
197if connected:
198    #########################################################################
199    # Generate lines of latitude.
200
201    feat = ogr.Feature( feature_def = layer.GetLayerDefn() )
202
203    for lat in float_range(ymin,ymax+stepsize/2,stepsize):
204
205        geom = ogr.Geometry( type = ogr.wkbLineString )
206       
207        for long in float_range(xmin,xmax+substepsize/2,substepsize):
208            geom.AddPoint( long, lat )
209
210        err = 0
211        if ct is not None:
212            err = geom.Transform( ct )
213             
214        if err is 0:
215            feat.SetGeometry( geom )
216            layer.CreateFeature( feat )
217
218    #########################################################################
219    # Generate lines of longitude
220
221    for long in float_range(xmin,xmax+stepsize/2,stepsize):
222       
223        geom = ogr.Geometry( type = ogr.wkbLineString )
224       
225        for lat in float_range(ymin,ymax+substepsize/2,substepsize):
226            geom.AddPoint( long, lat )
227
228        err = 0
229        if ct is not None:
230            err = geom.Transform( ct )
231
232        if err is 0:
233            feat.SetGeometry( geom )
234            layer.CreateFeature( feat )
235               
236#############################################################################
237# Cleanup
238
239feat = None
240geom = None
241
242ds.Destroy()
243ds = None
244
Note: See TracBrowser for help on using the repository browser.