def no_building(x, y): (left, top) = tileMath.tile2deg(x, y, zoomlevel) (right, bottom) = tileMath.tile2deg(x + 1, y + 1, zoomlevel) res = queryosm( "select exists(select 1 from building_polygon where geometry && ST_MakeEnvelope(%s, %s, %s, %s, 4326))" % (right, bottom, left, top)) building = res[0][0] return building == False
def getRoads(bbox): (startX, startY, endX, endY) = bbox (left, top) = tileMath.tile2deg(startX, startY, zoomlevel) (right, bottom) = tileMath.tile2deg(endX, endY, zoomlevel) roads = queryosm( "select ST_AsGeoJSON(geometry) from highway_line where highway='tertiary' and ST_Intersects(geometry, ST_MakeEnvelope(%s, %s, %s, %s, 4326))" % (right, bottom, left, top)) return roads
def getPolygon(bbox): (startX, startY, endX, endY) = bbox (left,top) = tileMath.tile2deg(startX, startY, zoomlevel) (right,bottom) = tileMath.tile2deg(endX, endY, zoomlevel) return Polygon([[ (left, top), (left, bottom), (right, bottom), (right, top), (left, top) ]])
def getRoads(bbox): (startX, startY, endX, endY) = bbox (left,top) = tileMath.tile2deg(startX, startY, zoomlevel) (right,bottom) = tileMath.tile2deg(endX, endY, zoomlevel) roads = queryosm("select ST_AsGeoJSON(geometry) from highway_line where highway='tertiary' and ST_Intersects(geometry, ST_MakeEnvelope(%s, %s, %s, %s, 4326))" % (right, bottom, left, top)) for road in roads: print("{\n\"type\": \"Feature\",\n\"geometry\": " + str(road[0]) + ",") print("\"properties\": {\"name\": \"roads\"}\n},") return getTileNeighbors(roads)
def hasData(bbox): (startX, startY, endX, endY) = bbox (left,top) = tileMath.tile2deg(startX, startY, zoomlevel) (right,bottom) = tileMath.tile2deg(endX, endY, zoomlevel) building = queryosm("select exists(select 1 from building_polygon where geometry && ST_MakeEnvelope(%s, %s, %s, %s, 4326))" % (right, bottom, left, top))[0][0] if building == True: return True cur.execute("select exists(select 1 from predictions where x>=%s and y>=%s and x<%s and y<%s and has_building=TRUE)",(startX, startY, endX, endY)) (scanned, ) = cur.fetchone(); return scanned
def test_tile2deg(self): x = tileMath.tile2deg(100, 100, 17) y = tileMath.tile2deg(-9999, -9999, 17) z = tileMath.tile2deg(-9999, 9999, 17) self.assertAlmostEqual(x[0], -179.72534179, places=6) self.assertAlmostEqual(x[1], 85.02737824, places=6) self.assertAlmostEqual(y[0], -207.46307373, places=6) self.assertAlmostEqual(y[1], 86.93446581, places=6) self.assertAlmostEqual(z[0], -207.46307373, places=6) self.assertAlmostEqual(z[1], 82.01565745, places=6)
def getMask(startX,startY, zoomlevel): # skip if this is already in our tarining data cur.execute('select * from segmentation_training_tiles where x=%d and y=%d' % (startX, startY)) res = cur.fetchone() if res != None: print 'skip' return # find the lat lng bounding box of the tile endX = startX+1 endY = startY+1 (left,top) = tileMath.tile2deg(startX, startY, zoomlevel) (right,bottom) = tileMath.tile2deg(endX, endY, zoomlevel) # find all buildings in this tile buildings = queryosm("SELECT ST_AsGeoJSON(geometry) FROM building_polygon where geometry && ST_MakeEnvelope(%s, %s, %s, %s, 4326)" % (right, bottom, left, top)) # size in degrees of each side of the tile. x and y may not be the same xLength = abs(left-right) yLength = abs(top-bottom) # create a new image to draw our mask on with a black (0,0,0) background img = Image.new("RGB", (256,256), (0,0,0)) drw = ImageDraw.Draw(img, "RGB") # for each building vertex, convert its points to 0-255 coordinates on the image # and draw them as a white poly for rawbuilding in buildings: building = json.loads(rawbuilding[0]) try: buildingCoord = [((buildingX-left)/xLength*255,(top-buildingY)/yLength*255) for (buildingX,buildingY) in building["coordinates"][0]] drw.polygon(buildingCoord, fill=(255,255,255)) except ValueError: print ("could not fetch", startX, startY) # save the output to our masks directory, and download the tile from naip img.save("%s/%s_%s.jpg" % (maskDir,startX,startY)) try: realImg = imagery.fetchTile(startX,startY,zoomlevel) file_jpgdata = StringIO(realImg) i = Image.open(file_jpgdata).convert('RGB') i.save("%s/%s_%s.jpg" % (tileDir,startX,startY)) except TypeError: print ("failed to load tile", startX, startY) # mark it as saved in segmentation_training_tiles, but not verified cur.execute("insert into segmentation_training_tiles (x, y, verified ) values (%s, %s, %s)",(startX, startY, False)) conn.commit() print(startX, startY)
def getMask(startX, startY, zoomlevel): cur.execute( 'select * from segmentation_training_tiles where x=%d and y=%d' % (startX, startY)) res = cur.fetchone() if res != None: print 'skip' return endX = startX + 1 endY = startY + 1 (left, top) = tileMath.tile2deg(startX, startY, zoomlevel) (right, bottom) = tileMath.tile2deg(endX, endY, zoomlevel) buildings = queryosm( "SELECT ST_AsGeoJSON(geometry) FROM building_polygon where geometry && ST_MakeEnvelope(%s, %s, %s, %s, 4326)" % (right, bottom, left, top)) xLength = abs(left - right) yLength = abs(top - bottom) img = Image.new("RGB", (256, 256), (0, 0, 0)) drw = ImageDraw.Draw(img, "RGB") for rawbuilding in buildings: building = json.loads(rawbuilding[0]) try: buildingCoord = [((buildingX - left) / xLength * 255, (top - buildingY) / yLength * 255) for (buildingX, buildingY) in building["coordinates"][0]] drw.polygon(buildingCoord, fill=(255, 255, 255)) except ValueError: print("could not fetch", startX, startY) img.save("%s/%s_%s.jpg" % (maskDir, startX, startY)) try: realImg = imagery.fetchTile(startX, startY, zoomlevel) file_jpgdata = StringIO(realImg) i = Image.open(file_jpgdata).convert('RGB') i.save("%s/%s_%s.jpg" % (tileDir, startX, startY)) except TypeError: print("failed to load tile", startX, startY) cur.execute( "insert into segmentation_training_tiles (x, y, verified ) values (%s, %s, %s)", (startX, startY, False)) conn.commit() print(startX, startY)
def scanRoads(quad): global quads roadsToScan = sorted(getRoads(quad), key=lambda x: x[1]) #roadsToScan = getRoads(quad) for (x,y) in roadsToScan: #scan(x, y) (lon, lat) = tileMath.tile2deg(x, y, zoomlevel)
def findGeoJSONArea(roadTiles): #print(roadTiles) near = [] nearDeg = [] tiles = {} degTiles = {} for key, value in roadTiles.iteritems(): del near[:] for (x, y) in value: near.append((x+1, y+1)) near.append((x-1, y-1)) near.append((x+1, y-1)) near.append((x-1, y+1)) near.append((x, y+1)) near.append((x, y-1)) near.append((x+1, y)) near.append((x-1, y)) tiles[key] = near for key, value in tiles.iteritems(): del nearDeg[:] for (x, y) in value: nearDeg.append(tileMath.tile2deg(x, y, zoomlevel)) degTiles[key] = nearDeg # Now updatedTiles is a dict with a list of all neighbors corresponding # to each LineString entry in the bounding box. The list represents # neighbors as their latitude and longitude print degTiles
def contours(x,y): z=17 (startX, startY) = tileMath.tile2deg(x,y,z) (endX, endY) = tileMath.tile2deg(x+1,y+1,z) dx = endX-startX dy = endY-startY img = imagery.fetchTile(x,y,z) file_jpgdata = StringIO(img) i = Image.open(file_jpgdata) out = predict.predictMask(i) contours = predict.getContours(out) realContours = [ [[((float(x)/255)*dx)+startX, ((float(y)/255)*dy)+startY] for [x,y] in pointset] for pointset in contours] return jsonify(realContours)
def contours(x, y): z = 17 # get lat lng boundaries and size of tile (startX, startY) = tileMath.tile2deg(x, y, z) (endX, endY) = tileMath.tile2deg(x + 1, y + 1, z) dx = endX - startX dy = endY - startY # download tile img = imagery.fetchTile(x, y, z) file_jpgdata = StringIO(img) i = Image.open(file_jpgdata) #get image mask and contours out = predict.predictMask(i) contours = predict.getContours(out) # transform contours from pixel space to lat lng space realContours = [[[((float(x) / 255) * dx) + startX, ((float(y) / 255) * dy) + startY] for [x, y] in pointset] for pointset in contours] return jsonify(realContours)
def hasData(bbox, anyData): (startX, startY, endX, endY) = bbox (left, top) = tileMath.tile2deg(startX, startY, zoomlevel) (right, bottom) = tileMath.tile2deg(endX, endY, zoomlevel) # if there is a building in OSM with the data already, just exit building = queryosm( "select exists(select 1 from building_polygon where geometry && ST_MakeEnvelope(%s, %s, %s, %s, 4326))" % (right, bottom, left, top))[0][0] if building == True: return True # check to see if we've already predicted. if anyData is true, predicitions true or false count if anyData: cur.execute( "select exists(select 1 from predictions where x>=%s and y>=%s and x<%s and y<%s)", (startX, startY, endX, endY)) else: cur.execute( "select exists(select 1 from predictions where x>=%s and y>=%s and x<%s and y<%s and has_building=TRUE)", (startX, startY, endX, endY)) (scanned, ) = cur.fetchone() return scanned
zoomlevel = 17 resolution = 256 for cat in ['tiles', 'masks']: if os.path.exists(os.path.join(outdir, cat)): shutil.rmtree(os.path.join(outdir, cat)) path = os.path.join(outdir, cat) if not os.path.exists(path): os.makedirs(path) cur.execute( 'select x,y,dx,dy from segmentation_training_tiles where useable=true;') tiles = cur.fetchall() for tile in tiles: (x, y, dx, dy) = tile (left, top) = tileMath.tile2deg(x, y, zoomlevel) (right, bottom) = tileMath.tile2deg(x + 1, y + 1, zoomlevel) buildings = queryosm( "SELECT ST_AsGeoJSON(geometry) FROM building_polygon where geometry && ST_MakeEnvelope(%s, %s, %s, %s, 4326)" % (right, bottom, left, top)) xLength = abs(left - right) yLength = abs(top - bottom) img = Image.new("RGB", (resolution, resolution), (0, 0, 0)) drw = ImageDraw.Draw(img, "RGB") for rawbuilding in buildings: building = json.loads(rawbuilding[0]) try: buildingCoord = [ (((buildingX - left) / xLength * resolution) + (dx / 2),