形状不能精确地找到多边形内的点

时间:2016-07-28 21:38:20

标签: python google-maps map-projections shapely

我有几点,我想检查它们是否包含在多边形中。多边形和点由纬度和经度表示。

以下是重现我的场景的代码和谷歌地图打印屏幕,它看起来像多边形,多边形内部/外部的点,如Shapely。

import pyproj
from shapely.geometry import Polygon, Point
from shapely.ops import transform
from functools import partial
import numpy as np

polygon_array = [(1.4666748046875, 49.088257784724675),
                 (1.4447021484375, 47.42808726171425),
                 (2.889404296875, 47.42808726171425),
                 (2.8729248046875, 49.08466020484928),
                 (-0.0054931640625, 47.97521412341619),
                 (0.010986328125, 46.18743678432541),
                 (1.4227294921875, 46.1912395780416),
                 (1.4337158203125, 48.02299832104887),
                 (-1.043701171875, 46.65320687122665),
                 (-1.043701171875, 44.6061127451739),
                 (0.0164794921875, 44.5982904898401),
                 (-0.0054931640625, 46.6795944656402)]

simple_polygon = Polygon(polygon_array)
projection = partial(
    pyproj.transform,
    pyproj.Proj(init='epsg:4326'),
    pyproj.Proj('+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs'))
polygon = transform(projection, simple_polygon)

for latitude in np.arange(44.5435052132, 49.131408414, 0.071388739257):
    for longitude in np.arange(-0.999755859375, 2.99926757812, 0.071388739257):
        point = transform(projection, Point(longitude, latitude))
        if polygon.contains(point):
            print "%s, %s" % (latitude, longitude)

以下是多边形在地图上的外观:

The polygon on Google Maps

以下是多边形内部的点(此处表示为标记):

Points "inside" polygon

点“外”:

"outside"

这里的问题是这些点明显远离多边形,内部或外部。我是这个投影计划的新手,所以我可能会遗漏一些东西。

提前谢谢

1 个答案:

答案 0 :(得分:2)

你的多边形看起来不像你画的画(我能说的最好)。

fiddle

enter image description here

代码段



function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var polygon_array = [{
    lng: 1.4666748046875,
    lat: 49.088257784724675
  }, {
    lng: 1.4447021484375,
    lat: 47.42808726171425
  }, {
    lng: 2.889404296875,
    lat: 47.42808726171425
  }, {
    lng: 2.8729248046875,
    lat: 49.08466020484928
  }, {
    lng: -0.0054931640625,
    lat: 47.97521412341619
  }, {
    lng: 0.010986328125,
    lat: 46.18743678432541
  }, {
    lng: 1.4227294921875,
    lat: 46.1912395780416
  }, {
    lng: 1.4337158203125,
    lat: 48.02299832104887
  }, {
    lng: -1.043701171875,
    lat: 46.65320687122665
  }, {
    lng: -1.043701171875,
    lat: 44.6061127451739
  }, {
    lng: 0.0164794921875,
    lat: 44.5982904898401
  }, {
    lng: -0.0054931640625,
    lat: 46.6795944656402
  }];
  for (var i = 0; i < polygon_array.length; i++) {
    var marker = new google.maps.Marker({
      map: map,
      position: polygon_array[i],
      title: "" + i
    })
  }
  var polygon = new google.maps.Polygon({
    map: map,
    paths: [polygon_array],
    fillOpacity: 0.5,
    strokeWeight: 2,
    strokeOpacity: 1,
    strokeColor: "red",
    fillColor: "red"
  });
  var bounds = new google.maps.LatLngBounds();
  for (var i = 0; i < polygon.getPaths().getAt(0).getLength(); i++) {
    bounds.extend(polygon.getPaths().getAt(0).getAt(i));
  }
  map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, "load", initialize);
&#13;
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>
&#13;
&#13;
&#13;