简体   繁体   English

形状无法精确找到多边形内的点

[英]Shapely not able to precisely find points inside polygon

I have several points and I would like to check if they are contained on a polygon. 我有几个要点,我想检查一下它们是否包含在多边形中。 The polygon and the points are represented by latitudes and longitudes. 多边形和点由纬度和经度表示。

Following is the code of to reproduce my scenario and the Google Maps print screen of what it looks like the polygon, the points inside/outside the polygon as per Shapely. 以下是用于重现我的场景的代码以及Google地图打印屏幕上看起来像多边形的形状,根据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)

Here is what the polygon looks on a map: 这是多边形在地图上的外观:

Google地图上的多边形

Here is what looks like the points (here represented as markers) "inside" the polygon: 这是多边形内的点(此处表示为标记):

指向“内部”多边形

And the points "outside": 和要点“外”:

“外”

The problem here is that the points are clearly way off the polygon, inside or outside. 这里的问题是,这些点明显不在多边形的内部或外部。 I am new to this projection scheme so I may be missing something. 我是这个投影方案的新手,所以我可能会缺少一些东西。

Thank you in advance 先感谢您

Your polygon doesn't look anything like the picture you drew (best I can tell). 您的多边形看上去与您绘制的图片完全不同(据我所知,这是最好的)。

fiddle 小提琴

在此处输入图片说明

code snippet: 代码段:

 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); 
 html, body, #map_canvas { height: 100%; width: 100%; margin: 0px; padding: 0px } 
 <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map_canvas"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM