-
Notifications
You must be signed in to change notification settings - Fork 20
/
testbatch.py
379 lines (312 loc) · 11.4 KB
/
testbatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import pycrs
import traceback
import logging
###########################
# Drawing routine for testing
raw = None
def render_world(crs, savename):
import urllib2
import json
import pygeoj
import pyagg
import pyproj
import random
# load world borders
global raw
if not raw:
raw = urllib2.urlopen("https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json").read()
rawdict = json.loads(raw)
data = pygeoj.load(data=rawdict)
# convert coordinates
fromproj = pyproj.Proj("+init=EPSG:4326")
toproj = pyproj.Proj(crs.to_proj4())
for feat in data:
if feat.geometry.type == "Polygon":
feat.geometry.coordinates = [zip(*pyproj.transform(fromproj, toproj, zip(*ring)[0], zip(*ring)[1]))
for ring in feat.geometry.coordinates]
elif feat.geometry.type == "MultiPolygon":
feat.geometry.coordinates = [
[zip(*pyproj.transform(fromproj, toproj, zip(*ring)[0], zip(*ring)[1]))
for ring in poly]
for poly in feat.geometry.coordinates]
feat.geometry.update_bbox() # important to clear away old bbox
# get zoom area
data.add_all_bboxes()
data.update_bbox()
bbox = data.bbox
## # to avoid inf bounds and no render in satellite view
## xmins, ymins, xmaxs, ymaxs = zip(*(feat.geometry.bbox for feat in data))
## inf = float("inf")
## xmaxs = (xmax for xmax in xmaxs if xmax != inf)
## ymaxs = (ymax for ymax in ymaxs if ymax != inf)
## bbox = (min(xmins), min(ymins), max(xmaxs), max(ymaxs))
# set up drawing
c = pyagg.Canvas(1000,1000)
c.geographic_space()
c.zoom_bbox(*bbox)
c.zoom_out(1.3)
# draw countries
for feat in data:
try: c.draw_geojson(feat.geometry,
fillcolor=tuple(random.randrange(255) for _ in range(3)),
outlinecolor="white")
except:
# NOTE: feat.__geo_interface__ is one level too high maybe??
print("unable to draw?", feat.geometry)
# draw text of the proj4 string used
c.percent_space()
c.draw_text(crs.to_proj4(), (50,10))
# save
c.save("testrenders/"+savename+".png")
# Source string generator
def sourcestrings(format):
# commonly used projections on global scale
# from http://www.remotesensing.org/geotiff/proj_list/
##Albers Equal-Area Conic
yield pycrs.utils.crscode_to_string("sr-org", 62, format)
##Azimuthal Equidistant
yield pycrs.utils.crscode_to_string("esri", 54032, format)
##Cassini-Soldner
# ...ignore, too specific
##Cylindrical Equal Area
yield pycrs.utils.crscode_to_string("sr-org", 8287, format)
##Eckert IV
yield pycrs.utils.crscode_to_string("esri", 54012, format)
##Eckert VI
yield pycrs.utils.crscode_to_string("esri", 54010, format)
##Equidistant Conic
yield pycrs.utils.crscode_to_string("esri", 54027, format)
##Equidistant Cylindrical
yield pycrs.utils.crscode_to_string("epsg", 3786, format)
##Equirectangular
yield pycrs.utils.crscode_to_string("sr-org", 8270, format)
##Gauss-Kruger
# ...not found???
##Gall Stereographic
yield pycrs.utils.crscode_to_string("esri", 54016, format)
##GEOS - Geostationary Satellite View
yield pycrs.utils.crscode_to_string("sr-org", 81, format)
##Gnomonic
# ...not found
##Hotine Oblique Mercator
yield pycrs.utils.crscode_to_string("esri", 54025, format)
##Krovak
yield pycrs.utils.crscode_to_string("sr-org", 6688, format)
##Laborde Oblique Mercator
# ...not found # yield pycrs.utils.crscode_to_string("epsg", 9813, format)
##Lambert Azimuthal Equal Area
yield pycrs.utils.crscode_to_string("sr-org", 28, format)
##Lambert Conic Conformal (1SP)
# ...not found
##Lambert Conic Conformal (2SP)
yield pycrs.utils.crscode_to_string("sr-org", 29, format) # yield pycrs.utils.crscode_to_string("epsg", 9802, format)
##Lambert Conic Conformal (2SP Belgium)
# ...ignore, too specific
##Lambert Cylindrical Equal Area
yield pycrs.utils.crscode_to_string("sr-org", 8287, format)
##Mercator (1SP)
yield pycrs.utils.crscode_to_string("sr-org", 16, format)
##Mercator (2SP)
yield pycrs.utils.crscode_to_string("sr-org", 7094, format)
##Miller Cylindrical
yield pycrs.utils.crscode_to_string("esri", 54003, format)
##Mollweide
yield pycrs.utils.crscode_to_string("esri", 54009, format)
##New Zealand Map Grid
# ...ignore, too specific
##Oblique Mercator
yield pycrs.utils.crscode_to_string("esri", 54025, format)
##Oblique Stereographic
yield pycrs.utils.crscode_to_string("epsg", 3844, format)
##Orthographic
yield pycrs.utils.crscode_to_string("sr-org", 6980, format)
##Polar Stereographic
yield pycrs.utils.crscode_to_string("sr-org", 8243, format)
##Polyconic
yield pycrs.utils.crscode_to_string("esri", 54021, format)
##Robinson
yield pycrs.utils.crscode_to_string("esri", 54030, format)
##Rosenmund Oblique Mercator
# ...not found
##Sinusoidal
yield pycrs.utils.crscode_to_string("sr-org", 6965, format)
##Swiss Oblique Cylindrical
# ...ignore, too specific
##Swiss Oblique Mercator
# ...ignore, too specific
##Stereographic
yield pycrs.utils.crscode_to_string("sr-org", 6711, format)
##Transverse Mercator
# ...not found???
##Transverse Mercator (Modified Alaska)
# ...ignore, too specific
##Transverse Mercator (South Oriented)
# ...ignore, too specific
##Tunisia Mining Grid
# ...ignore, too specific
##VanDerGrinten
yield pycrs.utils.crscode_to_string("sr-org", 6978, format)
# bunch of randoms
#yield pycrs.utils.crscode_to_string("esri", 54030, format)
#yield pycrs.utils.crscode_to_string("sr-org", 7898, format)
#yield pycrs.utils.crscode_to_string("sr-org", 6978, format)
#yield pycrs.utils.crscode_to_string("epsg", 4324, format)
#yield pycrs.utils.crscode_to_string("sr-org", 6618, format)
#yield pycrs.utils.crscode_to_string("sr-org", 22, format)
#yield pycrs.utils.crscode_to_string("esri", 54031, format)
# add more...
# Misc other crs for testing
#crs = pycrs.utils.crscode_to_string("esri", 54030, "proj4")
#crs = pycrs.utils.crscode_to_string("sr-org", 6978, "proj4")
#crs = pycrs.parse.from_sr_code(7898)
#crs = pycrs.parse.from_epsg_code(4324)
#crs = pycrs.parse.from_sr_code(6618)
#crs = pycrs.parse.from_sr_code(22)
#crs = pycrs.parse.from_esri_code(54031)
#proj4 = "+proj=longlat +ellps=WGS84 +datum=WGS84"
#proj4 = "+proj=aea +lat_1=24 +lat_2=31.5 +lat_0=24 +lon_0=-84 +x_0=400000 +y_0=0 +ellps=GRS80 +units=m +no_defs "
#proj4 = "+proj=larr +datum=WGS84 +lon_0=0 +lat_ts=45 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs"
#proj4 = "+proj=nsper +datum=WGS84 +ellps=WGS84 +lon_0=-60 +lat_0=40 +h=2000000000000000000000000"
# Testing format outputs
def testoutputs(crs):
print("ogc_wkt:\n")
try:
print(crs.to_ogc_wkt()+"\n")
global ogcwkt_outputs
ogcwkt_outputs += 1
except: logging.warn(traceback.format_exc())
print("esri_wkt:\n")
try:
print(crs.to_esri_wkt()+"\n")
global esriwkt_outputs
esriwkt_outputs += 1
except: logging.warn(traceback.format_exc())
print("proj4:\n")
try:
print(crs.to_proj4()+"\n")
global proj4_outputs
proj4_outputs += 1
except: logging.warn(traceback.format_exc())
#############################################################################
#############################################################################
#############################################################################
###########################
# From OGC WKT
print("--------")
print("Testing from ogc wkt:")
print("")
totals = 0
loaded = 0
ogcwkt_outputs = 0
esriwkt_outputs = 0
proj4_outputs = 0
renders = 0
for wkt in sourcestrings("ogcwkt"):
totals += 1
# test parsing
try:
print("From:\n")
print(wkt)
print("")
crs = pycrs.parse.from_ogc_wkt(wkt)
loaded += 1
# test outputs
print("To:\n")
testoutputs(crs)
# test render
try:
print("Rendering...")
savename = "%i_from_ogcwkt" % totals
render_world(crs, savename)
renders += 1
print("Successully rendered! \n")
except:
logging.warn(traceback.format_exc()+"\n")
except:
logging.warn(traceback.format_exc()+"\n")
print("Summary results:")
print(" Loaded: %f%%" % (loaded/float(totals)*100) )
print(" Outputs (OGC WKT): %f%%" % (ogcwkt_outputs/float(totals)*100) )
print(" Outputs (ESRI WKT): %f%%" % (esriwkt_outputs/float(totals)*100) )
print(" Outputs (Proj4): %f%%" % (proj4_outputs/float(totals)*100) )
print(" Renders: %f%%" % (renders/float(totals)*100) )
###########################
# From PROJ4
print("--------")
print("Testing from proj4:")
print("")
totals = 0
loaded = 0
ogcwkt_outputs = 0
esriwkt_outputs = 0
proj4_outputs = 0
renders = 0
for proj4 in sourcestrings("proj4"):
totals += 1
# test parsing
try:
print("From:\n")
print(proj4)
print("")
crs = pycrs.parse.from_proj4(proj4)
loaded += 1
# test outputs
print("To:\n")
testoutputs(crs)
# test render
try:
print("Rendering...")
savename = "%i_from_proj4" % totals
render_world(crs, savename)
renders += 1
print("Successully rendered! \n")
except:
logging.warn(traceback.format_exc()+"\n")
except:
logging.warn(traceback.format_exc()+"\n")
print("Summary results:")
print(" Loaded: %f%%" % (loaded/float(totals)*100) )
print(" Outputs (OGC WKT): %f%%" % (ogcwkt_outputs/float(totals)*100) )
print(" Outputs (ESRI WKT): %f%%" % (esriwkt_outputs/float(totals)*100) )
print(" Outputs (Proj4): %f%%" % (proj4_outputs/float(totals)*100) )
print(" Renders: %f%%" % (renders/float(totals)*100) )
###########################
# From ESRI WKT/PRJ FILE
print("--------")
print("Testing from esri wkt:")
print("")
totals = 0
loaded = 0
ogcwkt_outputs = 0
esriwkt_outputs = 0
proj4_outputs = 0
renders = 0
for wkt in sourcestrings("esriwkt"):
totals += 1
# test parsing
try:
print("From:\n")
print(wkt)
print("")
crs = pycrs.parse.from_esri_wkt(wkt)
loaded += 1
# test outputs
print("To:\n")
testoutputs(crs)
# test render
try:
print("Rendering...")
savename = "%i_from_esriwkt" % totals
render_world(crs, savename)
renders += 1
print("Successully rendered! \n")
except:
logging.warn(traceback.format_exc()+"\n")
except:
logging.warn(traceback.format_exc()+"\n")
print("Summary results:")
print(" Loaded: %f%%" % (loaded/float(totals)*100) )
print(" Outputs (OGC WKT): %f%%" % (ogcwkt_outputs/float(totals)*100) )
print(" Outputs (ESRI WKT): %f%%" % (esriwkt_outputs/float(totals)*100) )
print(" Outputs (Proj4): %f%%" % (proj4_outputs/float(totals)*100) )
print(" Renders: %f%%" % (renders/float(totals)*100) )