Google maps - how to get building's polygon coordinates from address?

(1) Acquire image tile

original

(2) Segment buildings based on pixel color (here, 0xF2EEE6).

parsed

(3) Image cleanup (e.g. erosion then dilation) + algorithm to acquire pixel coordinates of polygon corners.

(4) Mercator projection to acquire lat/long of pixel


You can convert the address to geographic coordinates by the use of the Google Geocoding API.

https://maps.googleapis.com/maps/api/geocode/json?address=SOME_ADDRESS&key=YOUR_API_KEY

Then, you can use Python and a styled static map to obtain the polygon of the building (in pixel coordinates) at some location:

import numpy as np
from requests.utils import quote
from skimage.measure import find_contours, points_in_poly, approximate_polygon
from skimage import io
from skimage import color
from threading import Thread

center_latitude = None ##put latitude here 
center_longitude = None ##put longitude here 
mapZoom = str(20)
midX = 300
midY = 300
# Styled google maps url showing only the buildings
safeURL_Style = quote('feature:landscape.man_made|element:geometry.stroke|visibility:on|color:0xffffff|weight:1')
urlBuildings = "http://maps.googleapis.com/maps/api/staticmap?center=" + str_Center + "&zoom=" + mapZoom + "&format=png32&sensor=false&size=" + str_Size + "&maptype=roadmap&style=visibility:off&style=" + safeURL_Style

mainBuilding = None
imgBuildings = io.imread(urlBuildings)
gray_imgBuildings = color.rgb2gray(imgBuildings)
# will create inverted binary image
binary_imageBuildings = np.where(gray_imgBuildings > np.mean(gray_imgBuildings), 0.0, 1.0)
contoursBuildings = find_contours(binary_imageBuildings, 0.1)

for n, contourBuilding in enumerate(contoursBuildings):
    if (contourBuilding[0, 1] == contourBuilding[-1, 1]) and (contourBuilding[0, 0] == contourBuilding[-1, 0]):
        # check if it is inside any other polygon, so this will remove any additional elements
        isInside = False
        skipPoly = False
        for othersPolygon in contoursBuildings:
            isInside = points_in_poly(contourBuilding, othersPolygon)
            if all(isInside):
                skipPoly = True
                break

        if skipPoly == False:
            center_inside = points_in_poly(np.array([[midX, midY]]), contourBuilding)
            if center_inside:
        # approximate will generalize the polygon
                mainBuilding = approximate_polygon(contourBuilding, tolerance=2)

print(mainBuilding)

Now, you can convert the pixel coordinates to latitude and longitude by the use of little JavaScript, and the Google Maps API:

function point2LatLng(point, map) {
        var topRight = map.getProjection().fromLatLngToPoint(map.getBounds().getNorthEast());
        var bottomLeft = map.getProjection().fromLatLngToPoint(map.getBounds().getSouthWest());
        var scale = Math.pow(2, map.getZoom());
        var worldPoint = new google.maps.Point(point.x / scale + bottomLeft.x, point.y / scale + topRight.y);
        return map.getProjection().fromPointToLatLng(worldPoint);
}

var convertedPointsMain = [];

for (var i = 0; i < pxlMainPolygons[p].length; i++) {
    var conv_point = {
        x: Math.round(pxlMainPolygons[p][i][1]),
        y: Math.round(pxlMainPolygons[p][i][0])
    }; 
    convertedPointsMain[i] = point2LatLng(conv_point, map);
}

console.log(convertedPointsMain);

I've been working on this for hours, the closest I have come is finding a request uri that returns a result with a polygon in it. I believe it specifies the building(boundary) by editids parameter. We just need a way to get the current editids from a building(boundary).

The URI I have is:

https://www.google.com/mapmaker?hl=en&gw=40&output=jsonp&ll=38.934911%2C-92.329359&spn=0.016288%2C0.056477&z=14&mpnum=0&vpid=1354239392511&editids=nAlkfrzSpBMuVg-hSJ&xauth=YOUR_XAUTH_HERE&geowiki_client=mapmaker&hl=en

Part of the result has what is needed:

"polygon":[{"gnew":{"loop":[{"vertex":[{"lat_e7":389364691,"lng_e7":-923341133},{"lat_e7":389362067,"lng_e7":-923342783},{"lat_e7":389361075,"lng_e7":-923343356},{"lat_e7":389360594,"lng_e7":-923342477},