def WithinTiles(self): out = set() for obj in self._objs: for pos in obj.positions: tilex, tiley = slippytiles.deg2num(pos[0], pos[1], self.nativeZoom) out.add((tilex, tiley, self.nativeZoom)) return out
def DownloadSlippyTiles(lats, lons, zoom = 12, skipExisting = 1): xtile1, ytile1 = slippytiles.deg2num(min(lats), min(lons), zoom) xtile2, ytile2 = slippytiles.deg2num(max(lats), max(lons), zoom) collectLat, collectLon = [], [] for x in range(xtile1, xtile2+1): for y in range(ytile2, ytile1+1): rootPath = "existing/"+str(zoom) dirName = rootPath+"/"+str(x) fina = dirName+"/"+str(y)+".osm.bz2" if os.path.exists(fina) and skipExisting: continue print x, y tileCorner1 = slippytiles.num2deg(x, y, zoom) tileCorner2 = slippytiles.num2deg(x+1, y+1, zoom) print tileCorner1 print tileCorner2 collectLat.append(tileCorner1[0]) collectLat.append(tileCorner2[0]) collectLon.append(tileCorner1[1]) collectLon.append(tileCorner2[1]) url = "http://fosm.org/api/0.6/map?bbox={0},{1},{2},{3}".format(tileCorner1[1],tileCorner2[0],\ tileCorner2[1],tileCorner1[0]) print url if not os.path.exists(dirName): os.mkdir(dirName) if 1: f = urllib2.urlopen(url) data = f.read() if len(data) > 0: outfi = bz2.BZ2File(fina, "w") outfi.write(data) outfi.close() if len(collectLat) > 0: print "collectLat", min(collectLat), max(collectLat) print "collectLon", min(collectLon), max(collectLon)
def ExportBbox(lats, lons, zoom): latMin, latMax = min(lats), max(lats) lonMin, lonMax = min(lons), max(lons) print "lat", latMin, latMax print "lon", lonMin, lonMax boundsTL = slippytiles.deg2num(latMax, lonMin, zoom) boundsBR = slippytiles.deg2num(latMin, lonMax, zoom) print boundsTL print boundsBR out = CollectedData() repoId = 1 pth = "/home/tim/Desktop/surrey" for tilex in range(boundsTL[0], boundsBR[0]+1): for tiley in range(boundsTL[1], boundsBR[1]+1): ExportFromTile(repoId, tilex, tiley, zoom, pth, out) print "Write output" out.Save("out.xml")
def GetOsTile(tileCode = "so22nw", zoom = 12, out = None): tileBL = osgb.os_streetview_tile_to_grid(tileCode) tileTR = (tileBL[0]+5000, tileBL[1]+5000) tileBR = (tileBL[0]+5000, tileBL[1]) tileTL = (tileBL[0], tileBL[1]+5000) x,y,z= ostn02.OSGB36_to_ETRS89(*tileBL) c1 = osgb.grid_to_ll(x,y) x,y,z= ostn02.OSGB36_to_ETRS89(*tileTR) c2 = osgb.grid_to_ll(x,y) x,y,z= ostn02.OSGB36_to_ETRS89(*tileBR) c3 = osgb.grid_to_ll(x,y) x,y,z= ostn02.OSGB36_to_ETRS89(*tileTL) c4 = osgb.grid_to_ll(x,y) lat = [c1[0], c2[0], c3[0], c4[0]] lon = [c1[1], c2[1], c3[1], c4[1]] minLat, maxLat = min(lat), max(lat) minLon, maxLon = min(lon), max(lon) tilex1, tiley1 = slippytiles.deg2num(maxLat, minLon, zoom) tilex2, tiley2 = slippytiles.deg2num(minLat, maxLon, zoom) tilePath = "/home/tim/dev/batch-garmin-map/{0}/{1}/{2}.osm.bz2" fileList = [] for x in range(tilex1, tilex2+1): for y in range(tiley1, tiley2+1): fina = tilePath.format(zoom, x, y) print x, y, fina if os.path.isfile(fina): fileList.append(fina) mergeTiles.MergeFiles(fileList, out)
def ExportBbox(lats, lons, zoom): latMin, latMax = min(lats), max(lats) lonMin, lonMax = min(lons), max(lons) print "lat", latMin, latMax print "lon", lonMin, lonMax boundsTL = slippytiles.deg2num(latMax, lonMin, zoom) boundsBR = slippytiles.deg2num(latMin, lonMax, zoom) print boundsTL print boundsBR out = CollectedData() repoId = 1 pth = "/home/tim/Desktop/surrey" for tilex in range(boundsTL[0], boundsBR[0] + 1): for tiley in range(boundsTL[1], boundsBR[1] + 1): ExportFromTile(repoId, tilex, tiley, zoom, pth, out) print "Write output" out.Save("out.xml")
def _compute_city_tiles(self): ''' Filter input.csv (bssid, lat, lon) through the osm tile filter so that we can compute the set of tiles for a city. The tileset *must* fit within 16bits (64k tiles). If the tileset is too large, we : * compute a bounding box around the total lat/lon set * compute the center of the bounding box * Compute total 2^16/# of actual tiles as N * Scale the allowed longitudinal (horizontal) points from the center point by sqrt(N*1.05) * Scale the allowed latitude (vertical) points from the center point by sqrt(N*1.05) * Recalculate the tileset ''' # This code has been removed to simplify the entire process. # Just assume everything fits into a 64k tiled space for now. # If the actual number of tiles exceeds 64k, we'll # just scale down the the number of tiles to fit. rows = 0 tile_set = set() with open(self.incity_tiles, 'w') as f_out: writer = csv.writer(f_out) with open(self.bssid_input, 'r') as f_in: for i, row in enumerate(csv.reader(f_in)): bssid = row[0] lat = float(row[1]) lon = float(row[2]) tile_x, tile_y = deg2num(lat, lon, ZOOM_LEVEL) entry = (bssid, tile_x, tile_y, ZOOM_LEVEL) writer.writerow(entry) tile_key = (tile_x, tile_y) tile_set.add(tile_key) rows += 1 num_tiles = len(tile_set) if num_tiles > 2**16: # TODO: this is where we need to apply scaling. For now, # just abort early raise RuntimeError("Too many tiles: [%d]" % num_tiles) print "Total tileset size: %d" % num_tiles self.total_city_tiles = num_tiles
def AssignUuids(self): #Count how many tiles object crosses objToTileMap = {} for obj in self._objs: objInTiles = set() for pos in obj.positions: tilex, tiley = slippytiles.deg2num(pos[0], pos[1], self.nativeZoom) objInTiles.add((tilex, tiley)) objToTileMap[obj] = objInTiles #Assign uuid to any cross tile object that has none already for obj in objToTileMap: objInTiles = objToTileMap[obj] count = len(objInTiles) if count <= 1: continue #Object is indeed in multiple tiles if obj.uuid is not None: continue #Already has a uuid obj.uuid = uuid.uuid4()
out.write("</osm>\n") out.close() return countNodes, countWays, countRelations if __name__ == "__main__": #lats, lons = [51.383075,50.705752], [-1.955713,-0.729294] #lats, lons = [51.26630,51.00434], [-3.26607,-4.02825] #Exmoor #lats, lons = [49.0018439,49.3644891], [-0.6632996,0.0054932] #Caen lats, lons = [27.673799, 31.297328], [32.1679688, 35.0024414] #Sinai zoom = 12 xtile1, ytile1 = slippytiles.deg2num(min(lats), min(lons), zoom) xtile2, ytile2 = slippytiles.deg2num(max(lats), max(lons), zoom) collectLat, collectLon = [], [] fiList = [] for x in range(xtile1, xtile2 + 1): for y in range(ytile2, ytile1 + 1): rootPath = "12" dirName = rootPath + "/" + str(x) fina = dirName + "/" + str(y) + ".osm.bz2" if os.path.isfile(fina): fiList.append(fina) out = bz2.BZ2File("merge.osm.bz2", "w") #out = open("merge.osm","wt") MergeFiles(fiList, out)
MergeFile(pth+"/"+fi, ty, existing, fiOut) if __name__=="__main__": out = bz2.BZ2File("merge.osm.bz2","w") #lats, lons = [51.383075,50.705752], [-1.955713,-0.729294] #Hampshire? #lats, lons = [27.673799,31.297328], [32.1679688,35.0024414] #Sinai #lats, lons = [50.51865087505076,50.77226494056972], [-1.6009003235343708,-1.046058247973109] #Isle of wight #lats, lons = [50.703, 51.167], [-0.955, 0.040] #West sussex #lats, lons = [50.7217072, 51.1475977], [-0.1424041, 0.8675128] #East sussex lats, lons = [51.072,51.472], [-0.850,0.040] #Surrey zoom = 12 xtile1, ytile1 = slippytiles.deg2num(min(lats), min(lons), zoom) xtile2, ytile2 = slippytiles.deg2num(max(lats), max(lons), zoom) collectLat, collectLon = [], [] fiList = [] for x in range(xtile1, xtile2+1): for y in range(ytile2, ytile1+1): rootPath = "existing/12" dirName = rootPath+"/"+str(x) fina = dirName+"/"+str(y)+".osm.bz2" if os.path.isfile(fina): fiList.append(fina) MergeFiles(fiList, out) def MergeFiles(fiList, out):
import ostn02.OSTN02 as ostn02 import ostn02.OSGB as osgb import urllib2, slippytiles if __name__=="__main__": zoom = 14 tile1 = slippytiles.deg2num(51.793123644359504, -3.4304282535044774, zoom) tile2 = slippytiles.deg2num(52.04658609916299, -2.854674472952469, zoom) print tile1 print tile2 for a in range(tile1[0], tile2[0]+1): for b in range(tile2[1], tile1[1]+1): url = "http://map.4x4falcon.com/default/{2}/{0}/{1}.png/dirty".format(a,b,zoom) print url print urllib2.urlopen(url).read()