-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_stories.py
75 lines (64 loc) · 2.21 KB
/
create_stories.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
import yaml
import psycopg2
import psycopg2.extras
import json
import os
from shapely.wkt import dumps, loads
from shapely.geometry import mapping
from PIL import Image, ImageOps
conn = psycopg2.connect('dbname=streetview')
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
stories = yaml.load(open("raw_stories.yml").read())
features = []
sprite_entries = {}
THUMB_SIZE = 64
ATLAS_SIZE = 512
atlas = Image.new('RGBA', (ATLAS_SIZE,ATLAS_SIZE), (255, 255, 255, 0))
for i, story in enumerate(stories):
if 'coords' in story:
geom = {
'type':'Point',
'coordinates':story['coords']
}
else:
cur.execute("select st_astext(st_transform(st_setsrid(st_centroid(geom),2263),4326)) from dtm_1116_tax_lot_polygon where boro = %s and block = %s and lot = %s",(str(story['boro']),str(story['block']),str(story['lot'])))
geom = mapping(loads(cur.fetchone()['st_astext']))
boro = story['boro']
block = story['block']
lot = story['lot']
features.append({
'type':'Feature',
'geometry':geom,
'properties':{
'bbl':[boro,block,lot],
'addr':story['address'],
'text':story['text'],
'id':i
}
})
path = "www-data/photos/{0}/{1:05d}/{2:04d}.jpg".format(boro,block,lot)
if os.path.isfile(path):
img = Image.open(path)
size = (THUMB_SIZE,THUMB_SIZE)
thumb = ImageOps.fit(img, size, method = Image.ANTIALIAS)
x = THUMB_SIZE*(i % (ATLAS_SIZE/THUMB_SIZE))
y = THUMB_SIZE*(i / (ATLAS_SIZE/THUMB_SIZE))
atlas.paste(thumb,(x,y))
sprite_entries["story_{0}".format(i)] = {
"width":THUMB_SIZE,
"height":THUMB_SIZE,
"x":x,
"y":y,
"pixelRatio":1
}
atlas.save("www/public/images/atlas.png")
atlas.save("www/public/images/[email protected]")
# write atlas.json
for x in ["www/public/images/atlas.json","www/public/images/[email protected]"]:
with open(x, 'w') as outfile:
json.dump(sprite_entries,outfile)
with open("www/public/stories.json", 'w') as outfile:
json.dump({
"type":"FeatureCollection",
"features":features
},outfile,indent=4, sort_keys=True)