-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaymulti.py
More file actions
593 lines (365 loc) · 14 KB
/
Copy pathplaymulti.py
File metadata and controls
593 lines (365 loc) · 14 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# testbed
# global imports
import geopandas as gpd
import fiona
import shapely
import pickle
import json
import random
import numpy as np
import geopandas as gpd
from scipy.sparse import csr_matrix
from scipy.spatial import SphericalVoronoi, cKDTree
from PIL import Image, ImageFilter
# import my lib
from bclib import *
# big pictures
Image.MAX_IMAGE_PIXELS = 10**9
DEGREE = np.pi/180
# longest segment finding?
def play8():
sf = gpd.read_file('ne_10m_admin_0_scale_rank_minor_islands.shp')
# sf = gpd.read_file('densekm.shp')
# count how many points
count = 0
for i in sf['geometry'].exterior:
# print("I",i)
count = count + len(i.coords)
print("COUNT", count)
for j in zip(i.coords[:-1], i.coords[1:]):
pass
# print(j)
# print(np.cos(j[0][1]/180*np.pi)*np.abs(j[0][0]-j[1][0]))
# print(np.abs(j[0][1]-j[1][1]))
# print("COUNT", count)
# pt1 = gpd.Point((j[0]))
# print(new gpd.Point(j[0]).distance(new gpd.Point[j[1]]))
# print("J", j[0])
# for j in i.exterior.coords:
# print("J", j)
# print(debug0(object=i))
# for seg in i['geometry']:
# print("SEG", seg)
# print(sf['geometry'])
# read part of a shapefile
def play1():
# Load the shapefile (this is what takes the time, regardless of chunksize)
shapefile = gpd.read_file("ne_10m_admin_0_scale_rank_minor_islands.shp", chunksize=100)
# Create a select query
# query = "sr_sov_a3 = 'US1'"
# print(shapefile.query(query))
# Select the features that match the query
selected_features = shapefile[shapefile["sr_sov_a3"] == "US1"]
debug0(object = selected_features)
# loop
# for i in selected_features:
# debug0(object = i)
# Print the selected features
# print(selected_features)
def play2():
sf = shapefile.Reader("ne_10m_admin_0_scale_rank_minor_islands.shp")
shps = sf.shapes()
print(shps)
def play3():
arr = {}
width = 1800
height = width/2
lats = (-np.arange(-90, 90, 180./height)-180./height/2)/180*np.pi
lngs = (np.arange(-180, 180, 360./width)+360./width/2)/180*np.pi
lngs, lats = np.meshgrid(lngs, lats)
x = np.round(6371*np.cos(lats)*np.cos(lngs))
y = np.round(6371*np.cos(lats)*np.sin(lngs))
z = np.round(6371*np.sin(lats))
# for i in x: print("X", i)
# for i in y: print("Y", i)
# for i in z: print("Z", i)
ptsAll = np.column_stack([x.flatten(), y.flatten(), z.flatten()]).astype(np.int32)
for i in ptsAll: arr[tuple(i)] = 1
# print(arr)
print(arr.get((107, -21, -6370)))
# pts2 = np.unique(ptsAll, axis=0)
# print("PTS", ptsAll)
# print("PTS2", pts2)
# print("PTSALL", ptsAll.shape, pts2.shape)
def play4():
arr = {}
arr[4,5,6] = 7
if (4,5,6) in arr: print("YES")
if (1,2,4) in arr:
print("YES")
else:
print("NO")
for i in arr: print("I", i)
# print(arr)
# try:
# x = arr[1,2,3]
# finally:
# x = 0
def widthHeight2xyz(width, height):
"""
Given a width and height, return xyz values of width points of
longitude equally spaced and height points of latitude equally spaced
"""
lats = (-np.arange(-90, 90, 180./height)-180./height/2)/180*np.pi
lngs = (np.arange(-180, 180, 360./width)+360./width/2)/180*np.pi
print("LATS", lats)
print("LNGS", lngs)
lngs, lats = np.meshgrid(lngs, lats)
x = np.cos(lats)*np.cos(lngs)
y = np.cos(lats)*np.sin(lngs)
z = np.sin(lats)
return np.column_stack([x.flatten(), y.flatten(), z.flatten()])
def image2xyz(filename):
"""
Given an image filename, treat that image as an equiangular map and return the lit pixels in xyz format
"""
imMain = Image.open(filename)
width, height = imMain.size
edge1 = imMain.filter(ImageFilter.FIND_EDGES)
# edge1.save("/tmp/temp1.png")
edgePix1 = np.where(np.array(edge1) != 0)
print(np.shape(edgePix1))
filter4 = ImageFilter.Kernel((3, 3), (0, -1, 0, -1, 4, -1, 0, -1, 0), 1, 0)
edge2 = imMain.filter(filter4)
edgePix2 = np.where(np.array(edge2) != 0)
print(np.shape(edgePix2))
# edge2.save("/tmp/temp2.png")
return
allPixels = np.array(imMain)
litPixels = np.where(allPixels != 0)
print(litPixels)
litPixels = litPixels[0]*width + litPixels[1]
print(litPixels)
print(width, height)
def play5():
print(random.uniform(0,1))
return
points = np.array([[0, 0, 1], [0, 0, -1], [1, 0, 0], [0, 1, 0], [0, -1, 0], [-1, 0, 0]])
center = np.array([0, 0, 0])
radius = 1
sv = SphericalVoronoi(points, radius, center)
debug0(object=sv)
# store edge pixels to JSON
def play6(filename):
imMain = Image.open(filename)
width, height = imMain.size
edges = imMain.filter(ImageFilter.Kernel((3, 3), (0, -1, 0, -1, 4, -1, 0, -1, 0), 1, 0))
edgePix = np.where(np.array(edges) != 0)
pixNums = edgePix[0]*width + edgePix[1]
edges.save("/tmp/temp3.png")
print(pixNums)
np.savetxt("/tmp/temp4.txt", pixNums, fmt = '%d', newline=',')
# with open('/tmp/arr.json', 'w') as f:
# json.dump(pixNums, f, indent=4)
def raster2JSONPixels(**obj):
"""
Converts a raster map into a .js file defining a variable with the height, weight, and edge pixels. Input:
raster: the filename containing the raster map
var: the variable to assign in the js
outfile: the name of the output file
"""
imMain = Image.open(obj['raster'])
width, height = imMain.size
edges = imMain.filter(ImageFilter.Kernel((3, 3), (0, -1, 0, -1, 4, -1, 0, -1, 0), 1, 0))
edgePix = np.where(np.array(edges) != 0)
pixNums = edgePix[0]*width + edgePix[1]
# str = obj[var] + "= {"height": " + height +, "width": {width}, "points": ['
# the double { and } below are to escape them, JS vs Python
str = f'{obj["var"]} = {{"height": {height}, "width": {width}, "points": '
str += np.array2string(pixNums, separator=', ', max_line_width = np.inf, threshold = np.inf)
str += "};"
with open(obj['outfile'], 'w', encoding='utf-8') as f:
f.write(str)
def raster2KDTree(**obj):
"""
Given a raster image presumed to be an equiangular map, return a 3D KDTree of the edge points projected to 3 dimensions. Input:
raster: the filename containing the raster map
"""
imMain = Image.open(obj['raster'])
width, height = imMain.size
# the latitudes and longitudes of pixel centers in radians
lats = (-np.arange(-90, 90, 180./height)-180./height/2.)*np.pi/180.
lngs = (np.arange(-180, 180, 360./width)+360./width/2.)*np.pi/180.
edges = imMain.filter(ImageFilter.Kernel((3, 3), (0, -1, 0, -1, 4, -1, 0, -1, 0), 1, 0))
edgePix = np.where(np.array(edges) != 0)
# the latitudes and longitudes for the edge points
lats = lats[edgePix[0]]
lngs = lngs[edgePix[1]]
x = np.cos(lats)*np.cos(lngs)
y = np.cos(lats)*np.sin(lngs)
z = np.sin(lats)
# reshape to array of 3D points
ptsAll = np.column_stack([x.flatten(), y.flatten(), z.flatten()])
return cKDTree(ptsAll)
def equiRectangularTile2Closest(**obj):
"""
Given a spherical rectangle, and list of CKDTrees, determine which of the CDKTrees (points) are closest to the anywhere in the tile or unresolved it if can't be determined
slat, nlat, wlng, elng: rectangle coordinates
trees: a list of CKDTrees
Assumptions: wlng < elng, slat < nlat, and the rectangle crosses neither the equator nor the antimeridian, units are radians
"""
# TODO: there has to be a better way to pass objects with extracting and rolling up like this
# extract variables
nlat, slat, wlng, elng, trees = [obj['nlat'], obj['slat'], obj['wlng'], obj['elng'], obj['trees']]
# height is the difference in latitudes
height = nlat-slat
print("HEIGHT", (nlat-slat)/DEGREE)
# width is bounded by the cos of the latitude closer to the equator
width = (elng-wlng)*np.cos(np.min(np.abs([nlat, slat])))
# special case for equator crossing
if slat < 0 and nlat > 0:
width = elng-wlng
# the max distance from center assuming Pythag (which overestimates which is fine)
maxDist = np.sqrt(height**2 + width**2)
# the central lat and lng
clat = (nlat+slat)/2
clng = (wlng+elng)/2
# convert to xyz coords
x = np.cos(clat)*np.cos(clng)
y = np.cos(clat)*np.sin(clng)
z = np.sin(clat)
dists = []
points = []
for i in trees:
dists.append((i.query([x,y,z], k=1)[0]))
points.append((i.query([x,y,z], k=1)[1]))
sorted = np.argsort(dists)
if ((dists[sorted[1]] - dists[sorted[0]] > maxDist)):
return sorted[0]
# if we're less than 500m, just perma-unresolved
if maxDist < 2*np.pi/80000:
return -1
# -2 means currently unresolved but may resolve later
return -2
def slippyTile2Closest(**obj):
"""
Given a slippy tile in xyz format and list of CKDTrees, determine which of the CDKTrees (points) are closest to the center pixel of the slippy tile. Input:
x, y, z: slippy tile coordinates
trees: a list of CKDTrees
TODO: check to see if the min applies to the whole tile
"""
latLng = tile2LngLat(z=obj['z'], x=obj['x']+0.5, y=obj['y']+0.5)
lat = latLng['lat']
lng = latLng['lng']
# print("LATLGN",lat,lng)
# this is the angle length across the whole tile and is therefore MUCH larger than the minimum value here which would be the half diagonal length as a straight line
delta = 2*np.pi/2**obj['z']
# TODO: this MAY BE WRONG
delta = 2*np.pi/2**obj['z']*np.cos(lat)/np.sqrt(2)
# print("DELTA: ",delta)
x = np.cos(lat)*np.cos(lng)
y = np.cos(lat)*np.sin(lng)
z = np.sin(lat)
dists = []
points = []
for i in trees:
dists.append((i.query([x,y,z], k=1)[0]))
points.append((i.query([x,y,z], k=1)[1]))
sorted = np.argsort(dists)
if ((dists[sorted[1]] - dists[sorted[0]] > delta) or obj['z'] >= 15):
# if (dists[sorted[1]] - dists[sorted[0]] > delta):
# print("PS", sorted[0], points[sorted[0]])
return sorted[0]
return -1
def tile2LngLat(**obj):
"""
Ported from bclib.js.
Converts a slippy tile to a latitude and longitude value:
Input object:
z: tile zoom value
x: tile x value (may be fractional, 0 = left edge of tile, 0.5 = middle of leftmost pixel)
y: tile y value (may be fractional, 0 = top edge of tile, 0.5 = middle of top pixel of tile)
Output object:
lng: longitude in radians corresponding to z/x/y
lat: latitude in radians corresponding to z/x/y
"""
ret = {};
# the longitude is linear
ret['lng'] = obj['x'] / 2 ** obj['z'] * 2 * np.pi - np.pi;
# for Mercator (http://mathworld.wolfram.com/MercatorProjection.html)
ret['lat'] = np.pi / 2 - 2 * atan(np.exp((obj['y'] / 2 ** obj['z'] - 1 / 2) * 2 * np.pi))
return ret;
def play9():
# Set the filepath to the shapefile
filepath = "densekm.shp"
# Read the shapefile into a GeoDataFrame
df = gpd.read_file(filepath)
# Create a SQL query
sql = "sr_sov_a3 == 'GHA'"
# Select the features from the GeoDataFrame that match the SQL query
df = df.query(sql)
# Print the GeoDataFrame
for i in df['geometry'].exterior:
print(list(i.coords))
######### TESTING BELOW THIS LINE, NO MORE FUNCTIONS PLEASE ########
trees = []
for i in ["ARG", "GB1", "US1"]:
with open(f'{i}.tree', "rb") as f:
trees.append(pickle.load(f))
# order is slat, nlat, wlng, elng
tiles = [{'slat': -90*DEGREE, 'nlat': 90*DEGREE, 'wlng': -180*DEGREE, 'elng': 180*DEGREE}]
print(tiles)
while (tiles):
tile = tiles.pop(0)
val = equiRectangularTile2Closest(slat = tile['slat'], nlat = tile['nlat'], wlng = tile['wlng'], elng = tile['elng'], trees=trees)
if val >= -1:
print(tile,val)
continue
print(tile, "UNRESOLVED")
# couldn't find country, add 4 child tiles
for i in range(2):
for j in range(2):
tiles.append(
{'slat': tile['slat'] + i/2*(tile['nlat']-tile['slat']),
'nlat': tile['nlat'] - (1-i)/2*(tile['nlat']-tile['slat']),
'wlng': tile['wlng'] + j/2*(tile['elng']-tile['wlng']),
'elng': tile['elng'] - (1-j)/2*(tile['elng']-tile['wlng'])
})
# print("TILES", tiles)
exit()
# {'x': tile['x']*2, 'y': tile['y']*2, 'z': tile['z']+1})
# tiles.append({'x': tile['x']*2+1, 'y': tile['y']*2, 'z': tile['z']+1})
# tiles.append({'x': tile['x']*2, 'y': tile['y']*2+1, 'z': tile['z']+1})
# tiles.append({'x': tile['x']*2+1, 'y': tile['y']*2+1, 'z': tile['z']+1})
# print(tiles)
# tile = tiles[0]
# print(slippyTile2Closest(x=tile['x'], y=tile['y'], z=tile['z'], trees = trees))
exit()
print(slippyTile2Closest(z=11, x=417, y=810, trees = trees))
print(slippyTile2Closest(z=6, x=21, y=39, trees = trees))
print(slippyTile2Closest(z=8, x=82, y=171, trees = trees))
print(slippyTile2Closest(z=9, x=164, y=342, trees = trees))
print(slippyTile2Closest(z=0, x=0, y=0, trees = trees))
print(slippyTile2Closest(z=9, x=165, y=229, trees = trees))
exit()
play9()
exit()
play8()
exit()
with open("US1.tree", "rb") as f:
tree = pickle.load(f)
print(tree.query([1,0,0], 1))
print(tree.query([0,1,0], 1))
print(tree.query([0,0,1], 1))
exit()
for i in ['US1', 'GB1', 'ARG']:
with open(f'{i}.tree', 'wb') as f:
pickle.dump(raster2KDTree(raster = f'RASTER/{i}-raster-43200.png'), f)
exit()
tree = raster2KDTree(raster = "RASTER/US1-raster-43200.png")
with open('/tmp/tree.txt', 'wb') as f:
pickle.dump(tree, f)
exit()
# print(trees)
# play6("usa-raster-flat-1350.png")
# raster2JSONPixels(raster="RASTER/US1-raster-43200.png", var="usa", outfile="usa.js")
# raster2JSONPixels(raster="RASTER/GB1-raster-43200.png", var="uk", outfile="uk.js")
# raster2JSONPixels(raster="RASTER/ARG-raster-43200.png", var="arg", outfile="arg.js")
exit()
# play5()
# pts = widthHeight2xyz(20, 10)
# print(pts)
# print(np.shape(pts))
# print(image2xyz("usa-raster-flat-1350.png"))
# print(image2xyz("playground.png"))
# print(image2xyz("usa-raster-flat-43200.png"))