2017-03-08 175 views
2

我正試圖編寫一個腳本,使用Turf.js的inside方法,該腳本將用戶提交的座標與匹配到相應的人口普查通道。下面的代碼,作爲一個測試,我相信應該返回true。它不是。Turf.js inside()方法將不會返回true

那些座標一定是瑞格利球場。道多邊形包圍它。我弄了兩塊geoJson,它們很乾淨(除了右邊的規則)。

const wrigleyField = [41.947783, -87.655889]; 
const wrigleyCensusTract = { "type": "Feature", "properties": { "STATEFP10": "17", "COUNTYFP10": "031", "TRACTCE10": "061100", "GEOID10": "17031061100", "NAME10": "611", "NAMELSAD10": "Census Tract 611", "MTFCC10": "G5020", "FUNCSTAT10": "S", "ALAND10": 173037, "AWATER10": 0, "INTPTLAT10": "+41.9491397", "INTPTLON10": "-087.6568035" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -87.656985, 41.951084 ], [ -87.656858, 41.951086 ], [ -87.656806, 41.951087 ], [ -87.656704, 41.951088 ], [ -87.656661, 41.951088 ], [ -87.656535, 41.951091 ], [ -87.656494, 41.951092 ], [ -87.656325, 41.951094 ], [ -87.656245, 41.951096 ], [ -87.655821, 41.951102 ], [ -87.655653, 41.951105 ], [ -87.655411, 41.951109 ], [ -87.655059, 41.951115 ], [ -87.654685, 41.95112 ], [ -87.654444, 41.951124 ], [ -87.654431, 41.950707 ], [ -87.654395, 41.949459 ], [ -87.654384, 41.949043 ], [ -87.654373, 41.948683 ], [ -87.654344, 41.947698 ], [ -87.654341, 41.947604 ], [ -87.65433, 41.947245 ], [ -87.654576, 41.94724 ], [ -87.654938, 41.947234 ], [ -87.655317, 41.947228 ], [ -87.655565, 41.947225 ], [ -87.655755, 41.947221 ], [ -87.656313, 41.947213 ], [ -87.656325, 41.947212 ], [ -87.656516, 41.947202 ], [ -87.656663, 41.947194 ], [ -87.656849, 41.947184 ], [ -87.657104, 41.94718 ], [ -87.657252, 41.947178 ], [ -87.657382, 41.947176 ], [ -87.657635, 41.947171 ], [ -87.657739, 41.94717 ], [ -87.658784, 41.947154 ], [ -87.659168, 41.947149 ], [ -87.659165, 41.947254 ], [ -87.659167, 41.94733 ], [ -87.659176, 41.947623 ], [ -87.659181, 41.947876 ], [ -87.659186, 41.948059 ], [ -87.65919, 41.94824 ], [ -87.659196, 41.948487 ], [ -87.659204, 41.948785 ], [ -87.65921, 41.948967 ], [ -87.659219, 41.949292 ], [ -87.659229, 41.949598 ], [ -87.659245, 41.950269 ], [ -87.659254, 41.950595 ], [ -87.659257, 41.950685 ], [ -87.659266, 41.950957 ], [ -87.659269, 41.951048 ], [ -87.658928, 41.951052 ], [ -87.657908, 41.951068 ], [ -87.657569, 41.951074 ], [ -87.657468, 41.951075 ], [ -87.657167, 41.951081 ], [ -87.657067, 41.951083 ], [ -87.657014, 41.951083 ], [ -87.656985, 41.951084 ] ] ] } } 

const wrigleyPoint = { "type": "Feature", "properties": { "marker-color": "#0f0" }, "geometry": { "type": "Point", "coordinates": wrigleyField } }; 

console.log(turf.inside(wrigleyPoint, wrigleyCensusTract)); // false 

我錯過了什麼?爲什麼上面的代碼不會返回true

謝謝!

+0

你確定你有正確的順序那些COORDS?在WF點上,具體而言。 – pvg

+0

Google和Leaflet都會將它指向正確的位置。與官方地圖相比,FWIW,Leaflet也正確地繪製了道地數據。 – ryanbmarx

+1

您的多邊形中的座標,turf.js示例中文檔中的座標以不同的順序排列。你是否嘗試交換wrigleyField coords並查看會發生什麼?即改爲'wrigleyField = [-87.655889,41.947783];' – pvg

回答

0

問題是你的點座標是交換的。如果你切換他們的地方,那麼它將返回true。我只是用你的數據自己測試了這一點,並且在交換點座標之後我確實變得真實。這裏有一個片段,你可以自己測試一下。

p1 = { "type": "Feature", "properties": { "STATEFP10": "17", "COUNTYFP10": "031", "TRACTCE10": "061100", "GEOID10": "17031061100", "NAME10": "611", "NAMELSAD10": "Census Tract 611", "MTFCC10": "G5020", "FUNCSTAT10": "S", "ALAND10": 173037, "AWATER10": 0, "INTPTLAT10": "+41.9491397", "INTPTLON10": "-087.6568035" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -87.656985, 41.951084 ], [ -87.656858, 41.951086 ], [ -87.656806, 41.951087 ], [ -87.656704, 41.951088 ], [ -87.656661, 41.951088 ], [ -87.656535, 41.951091 ], [ -87.656494, 41.951092 ], [ -87.656325, 41.951094 ], [ -87.656245, 41.951096 ], [ -87.655821, 41.951102 ], [ -87.655653, 41.951105 ], [ -87.655411, 41.951109 ], [ -87.655059, 41.951115 ], [ -87.654685, 41.95112 ], [ -87.654444, 41.951124 ], [ -87.654431, 41.950707 ], [ -87.654395, 41.949459 ], [ -87.654384, 41.949043 ], [ -87.654373, 41.948683 ], [ -87.654344, 41.947698 ], [ -87.654341, 41.947604 ], [ -87.65433, 41.947245 ], [ -87.654576, 41.94724 ], [ -87.654938, 41.947234 ], [ -87.655317, 41.947228 ], [ -87.655565, 41.947225 ], [ -87.655755, 41.947221 ], [ -87.656313, 41.947213 ], [ -87.656325, 41.947212 ], [ -87.656516, 41.947202 ], [ -87.656663, 41.947194 ], [ -87.656849, 41.947184 ], [ -87.657104, 41.94718 ], [ -87.657252, 41.947178 ], [ -87.657382, 41.947176 ], [ -87.657635, 41.947171 ], [ -87.657739, 41.94717 ], [ -87.658784, 41.947154 ], [ -87.659168, 41.947149 ], [ -87.659165, 41.947254 ], [ -87.659167, 41.94733 ], [ -87.659176, 41.947623 ], [ -87.659181, 41.947876 ], [ -87.659186, 41.948059 ], [ -87.65919, 41.94824 ], [ -87.659196, 41.948487 ], [ -87.659204, 41.948785 ], [ -87.65921, 41.948967 ], [ -87.659219, 41.949292 ], [ -87.659229, 41.949598 ], [ -87.659245, 41.950269 ], [ -87.659254, 41.950595 ], [ -87.659257, 41.950685 ], [ -87.659266, 41.950957 ], [ -87.659269, 41.951048 ], [ -87.658928, 41.951052 ], [ -87.657908, 41.951068 ], [ -87.657569, 41.951074 ], [ -87.657468, 41.951075 ], [ -87.657167, 41.951081 ], [ -87.657067, 41.951083 ], [ -87.657014, 41.951083 ], [ -87.656985, 41.951084 ] ] ] } }; 
 

 
p2 = {"type": "Feature", "properties": {}, "geometry": {"type": "Point", "coordinates": [-87.655889, 41.947783]}}; 
 

 
alert(turf.inside(p2, p1))
<script src="https://npmcdn.com/@turf/turf/turf.min.js"></script>

+0

謝謝!這樣可行。 – ryanbmarx

相關問題