Skip to content

Instantly share code, notes, and snippets.

@mbostock
Created August 26, 2010 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbostock/550849 to your computer and use it in GitHub Desktop.
Save mbostock/550849 to your computer and use it in GitHub Desktop.
polymaps.appspot.com
application: polymaps
version: 1
runtime: python
api_version: 1
handlers:
- url: /state/.*
script: main.py
- url: /county/.*
script: main.py
- url: /
script: main.py
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import wsgiref.handlers
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write('Hello world!')
class JsonHandler(webapp.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), self.request.path[1:])
self.response.headers.add_header("Access-Control-Allow-Origin", "*")
if os.path.exists(path):
self.response.out.write(open(path, 'r').read())
else:
self.response.out.write('{"type":"FeatureCollection","features":[]}')
def main():
application = webapp.WSGIApplication([
('/', MainHandler),
('/state/.*', JsonHandler),
('/county/.*', JsonHandler),
], debug=False)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()
import os
import errno
import re
from urllib import urlopen
from json import JSONEncoder
import ModestMaps
import geojson
import simplejson
from shapely.geometry import asShape
from shapely.geometry import asPolygon
if __name__ == '__main__':
provider = ModestMaps.builtinProviders['OPENSTREETMAP']()
float_pat = re.compile(r'^-?\d+\.\d+$')
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST:
pass
else: raise
def scrape(row, col, zoom):
nw = provider.coordinateLocation(ModestMaps.Core.Coordinate(row, col, zoom))
se = provider.coordinateLocation(ModestMaps.Core.Coordinate(row + 1, col + 1, zoom))
url = 'http://localhost/pushpin?area=county&where=bbox:{nw.lat:f},{se.lon:f},{se.lat:f},{nw.lon:f}&zoom={Z}'.format(nw=nw, se=se, Z=zoom)
print url
fcb = geojson.loads(urlopen(url).read())
if not len(fcb['features']):
return
nwc = provider.coordinateLocation(ModestMaps.Core.Coordinate(row - .01, col - .01, zoom))
sec = provider.coordinateLocation(ModestMaps.Core.Coordinate(row + 1.01, col + 1.01, zoom))
clip = asPolygon([[nwc.lon, nwc.lat], [sec.lon, nwc.lat], [sec.lon, sec.lat], [nwc.lon, sec.lat]])
match = 0
for f in fcb['features']:
g = f['geometry']
shape = asShape(f['geometry'])
if shape.intersects(clip):
match = 1
f['geometry'] = geojson.loads(geojson.dumps(shape.intersection(clip)))
else:
f['geometry'] = geojson.loads('{}')
if not match:
return
dir = 'scrape/{Z}/{X}'.format(X=col, Y=row, Z=zoom)
mkdir_p(dir)
file = 'scrape/{Z}/{X}/{Y}.json'.format(X=col, Y=row, Z=zoom)
print file
open(file, 'w').write(''.join([(float_pat.match(x) and ('%.6f' % float(x)) or x) for x in JSONEncoder(separators=(',', ':')).iterencode(fcb)]))
if zoom < maxZoom:
zoom += 1
row <<= 1
col <<= 1
scrape(row, col, zoom)
scrape(row, col + 1, zoom)
scrape(row + 1, col, zoom)
scrape(row + 1, col + 1, zoom)
minZoom = 3
maxZoom = 7
for row in range(0, 1 << minZoom):
for col in range(0, 1 << minZoom):
scrape(row, col, minZoom)
@Kreozot
Copy link

Kreozot commented Aug 18, 2015

How can it work in http://bl.ocks.org/mbostock?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment