谷歌地图 - 如何从地址获取建筑物的多边形坐标?

Kon*_*nov 18 google-maps geocoding geolocation

如何实现以下内容:

  1. 用户定义地址
  2. 用户定义颜色
  3. 服务在Google地图上搜索相应的建筑物
  4. 服务使用颜色填充地图上的建筑物

我知道如何:

1.找到lat/long的地址

2.绘制多边形

因此,要完成任务,我需要从地址获取构建的多边形坐标.如何?

小智 13

(1)获取图像图块

原版的

(2)基于像素颜色的分段建筑物(此处为0xF2EEE6).

解析的

(3)图像清理(例如侵蚀然后扩张)+算法以获取多边形角的像素坐标.

(4)墨卡托投影获取纬度/长度像素


小智 10

您可以使用Google地理编码API将地址转换为地理坐标.

https://maps.googleapis.com/maps/api/geocode/json?address=SOME_ADDRESS&key=YOUR_API_KEY
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用Python和样式化的静态地图在某个位置获取建筑物的多边形(以像素坐标为单位):

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)
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用小型JavaScript和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);
Run Code Online (Sandbox Code Playgroud)