我有两个数据框,一个包含纬度/长点,另一个包含绘制多个多边形的geojson数据。
第一个数据框(countyDF)是从CSV导入的,第二个数据框(basin)是使用readLines()从geoJSON导入的(我应该使用getJSON()代替json数据吗?
即。 (通过简化的lat和long,让我知道一个更现实的例子是否有帮助) countyDF
pointNum Lat Long
1 100 251
2 150 175
3 50 -330
4 -150 100
和geoJSON格式如下(basin):
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "Basin_ID": "9-19", "Basin_Subb": "9-19", "Basin_Name": "TIA JUANA", "Subbasin_N": null }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -117.0679768595155, 32.574291366336219 ], [ -117.06716593683379, 32.573400317874729 ], [ -117.06341997467541, 32.569091397019029 ], [ -117.06194315333148, 32.566356289257612 ], [ -117.0590009947062, 32.565022113434004 ], [ -117.05426277324793, 32.561991889800737 ], [ -117.04796901017907, 32.559156827796407 ], [ -117.03949416454982, 32.555722526353854 ], [ -117.03658670964059, 32.55260623337746 ], [ -117.03547505795123, 32.551186682532702 ], [ -117.03106791328587, 32.547633284977714 ], [ -117.02453341519558, 32.542766715943806 ], [ -117.03096733611001, 32.542203418941845 ], [ -117.0324558562139, 32.542073586201752 ], [ -117.03444148063983, 32.541897693349327 ], [ -117.03508530679723, 32.541840746762325 ], [ -117.03893675722108, 32.541503138385245 ], [ -117.04563848478635, 32.540916090254136 ], [ -117.04690666072744, 32.540805072734159 ], [ -117.04975321345368, 32.540555662782488 ], [ -117.05487638996065, 32.540105955834157 ], [ -117.05619151994331, 32.539990441256123 ], [ -117.05848531771566, 32.539788923486533 ], [ -117.0631749250457, 32.54215137136817 ], [ -117.06680187749691, 32.543877383980934 ], [ -117.06927539222501, 32.545562859775892 ], [ -117.07152258989198, 32.546046045900979 ], [ -117.07489884859852, 32.54530496670057 ], [ -117.07773834685484, 32.54485919550919 ], [ -117.0794889459513, 32.544200153274701 ], [ -117.08117134135811, 32.543542769155188 ], [ -117.08203266401881, 32.542495002174064 ], [ -117.08180018990477, 32.541005184972832 ], [ -117.07878527133475, 32.53800290854668 ], [ -117.08430738067622, 32.537515913009408 ], [ -117.08445861115598, 32.541710736294391 ], [ -117.08701141119104, 32.543969332906386 ], [ -117.08926545491882, 32.544796327561301 ], [ -117.09253292944108, 32.545435592907872 ], [ -117.09470680391962, 32.545631754871643 ], [ -117.09647622272129, 32.545948915009404 ], [ -117.09856416256312, 32.545227266049267 ], [ -117.10023514667088, 32.543994836689315 ], [ -117.10068933038696, 32.542953304281674 ], [ -117.09977208759979, 32.541129535758976 ], [ -117.09846043769052, 32.539943208794561 ], [ -117.09742346564566, 32.538925042182839 ], [ -117.0947242456092, 32.536597809766022 ], [ -117.09829488087858, 32.536282964307077 ], [ -117.09840179566876, 32.536275072973709 ], [ -117.10107619525394, 32.538466493837575 ], [ -117.10364734517533, 32.538196700466642 ], [ -117.10522233351519, 32.538976382033773 ], [ -117.10697118628674, 32.538259847181273 ], [ -117.10895422266631, 32.535344375464277 ], [ -117.11407862537365, 32.537631294453931 ], [ -117.11625581912718, 32.537999378719483 ], [ -117.11827996690573, 32.537508348187259 ], [ -117.11981739848021, 32.536392491701257 ], [ -117.11950653557783, 32.534417856408112 ], [ -117.12372820778141, 32.534103225208959 ], [ -117.12463878866241, 32.534032917908206 ], [ -117.12483027265007, 32.538135448497222 ], [ -117.12502048372495, 32.539350678974635 ], [ -117.12507499529305, 32.540448930323947 ], [ -117.12528896501055, 32.54199741245435 ], [ -117.12534718931569, 32.542421432291746 ], [ -117.12599067680821, 32.544551370001955 ], [ -117.12618141788914, 32.545696646353761 ], [ -117.12645239828784, 32.546429360119511 ], [ -117.12685554422104, 32.548102449008979 ], [ -117.12737138919654, 32.549703348943893 ], [ -117.12763885987745, 32.550533107582829 ], [ -117.12774693079939, 32.550626279391835 ], [ -117.12774760184091, 32.550738637890753 ], [ -117.12880070942489, 32.553488055429675 ], [ -117.13066553380129, 32.556444612620524 ], [ -117.13079138058607, 32.556859005175724 ], [ -117.13090941562115, 32.557248309210252 ], [ -117.13144943066766, 32.558277136765511 ], [ -117.1318548691015, 32.55947068448608 ], [ -117.13255675068075, 32.56215226422006 ], [ -117.13271798929109, 32.563505590313213 ], [ -117.13279809835319, 32.563664669666316 ], [ -117.13291972036119, 32.564513801799826 ], [ -117.13304092465259, 32.565358954253092 ], [ -117.13317600342353, 32.566483563557291 ], [ -117.13322866985202, 32.568729809322086 ], [ -117.13322781196094, 32.569368502964551 ], [ -117.13308936989753, 32.57249315647227 ], [ -117.13300847391125, 32.57431809617708 ], [ -117.13308839881873, 32.575694177896445 ], [ -117.13327826763549, 32.576450195841588 ], [ -117.13316931636456, 32.57809929670556 ], [ -117.12945373702573, 32.577489321400627 ], [ -117.12803166530341, 32.577626667375966 ], [ -117.12613400930002, 32.577713895283864 ], [ -117.1215685168029, 32.576694287394176 ], [ -117.11623399545566, 32.574537576560076 ], [ -117.11386955416937, 32.575034406011433 ], [ -117.11190978595226, 32.575409504886181 ], [ -117.10647756024149, 32.575149662771501 ], [ -117.10010384262075, 32.575306669257621 ], [ -117.09510331040457, 32.576303242644421 ], [ -117.09115860317613, 32.575789876157522 ], [ -117.08924842286103, 32.575244771913034 ], [ -117.08413987452015, 32.574174428676862 ], [ -117.0780854909149, 32.573290881813598 ], [ -117.07518693202192, 32.574254743186088 ], [ -117.07241851206417, 32.574928753914364 ], [ -117.0679768595155, 32.574291366336219 ] ] ] ] } },
{ "type": "Feature", "properties": { "Basin_ID": "9-18", "Basin_Subb": "9-18", "Basin_Name": "OTAY VALLEY", "Subbasin_N": null }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -117.09094993890868, 32.619563585315568 ], [ -117.09024988989258, 32.616130450534968 ], [ -117.08912052342582, 32.606956836308925 ], [ -117.08936013555311, 32.601840287378998 ], [ -117.08875364427725, 32.598575478052986 ], [ -117.08680278644947, 32.595962898606246 ], [ -117.08441502669503, 32.595310423365085 ], [ -117.08156822333883, 32.595469231360795 ], [ -117.07566674287644, 32.595559902113109 ], [ -117.06972154230749, 32.596914588847568 ], [ -117.06432122834885, 32.598375496484095 ], [ -117.05964154380946, 32.598503627441907 ], [ -117.05616095566411, 32.597465378923445 ], [ -117.05457753753359, 32.596282762027229 ], [ -117.05073717232032, 32.594157791168556 ], [ -117.04589484061222, 32.592852213251177 ], [ -117.04312260910274, 32.593353146236865 ], [ -117.03899112955028, 32.593759832413419 ], [ -117.03371516847811, 32.59464309052418 ], [ -117.02862864592969, 32.594776592758308 ], [ -117.02272717085718, 32.594863862255089 ], [ -117.0174198657681, 32.594081171207122 ], [ -117.01427902225983, 32.593035965218846 ], [ -117.01229182668048, 32.591973496787411 ], [ -117.00991124266857, 32.59166411636609 ], [ -117.00874224342975, 32.590819514563513 ], [ -117.00439778679819, 32.59071141973822 ], [ -116.99862263551589, 32.590278958905216 ], [ -116.99501811309899, 32.589814373506385 ], [ -116.99227778615513, 32.588360796763084 ], [ -116.9878300187743, 32.586300314735269 ], [ -116.98294017247588, 32.586026512778012 ], [ -116.97812890271364, 32.586325564277786 ], [ -116.97467386624824, 32.586605260066648 ], [ -116.97337558327038, 32.586106965024619 ], [ -116.97141803299374, 32.586652564095417 ], [ -116.96871423244761, 32.587208317388864 ], [ -116.96622438005944, 32.588335212493227 ], [ -116.96042390526929, 32.590256368604443 ], [ -116.95557273144384, 32.592164131117251 ], [ -116.95221499420181, 32.594107400176057 ], [ -116.94872755550411, 32.596397127940342 ], [ -116.9459745479417, 32.59804505057086 ], [ -116.94404991193704, 32.60048471476123 ], [ -116.94239254000078, 32.602691044509697 ], [ -116.94129817998231, 32.602189536366552 ], [ -116.93999093720781, 32.601173820492207 ], [ -116.9387687523143, 32.601133484218465 ], [ -116.93659488322565, 32.600992021811464 ], [ -116.93460813321069, 32.599928207546924 ], [ -116.93113424649738, 32.599173132290474 ], [ -116.9290142367999, 32.598225880469577 ], [ -116.9297423527787, 32.597181836096283 ], [ -116.93156090942639, 32.596409563977744 ], [ -116.93419840172811, 32.59591322458288 ], [ -116.93526867881214, 32.59503617591411 ], [ -116.93605726039273, 32.593589412189353 ], [ -116.93802757033967, 32.593733854613859 ], [ -116.94108823292062, 32.59415039913921 ], [ -116.94387352503604, 32.59434074502488 ], [ -116.94622507549801, 32.593043461784966 ], [ -116.94952443572129, 32.591618310534955 ], [ -116.95503847459827, 32.588839924862008 ], [ -116.96069206495801, 32.586289312384324 ], [ -116.96744074207328, 32.584296717135018 ], [ -116.97352519139275, 32.58311772322412 ], [ -116.981310419243, 32.58220119915768 ], [ -116.98674868342913, 32.582811874361674 ], [ -116.98884496772166, 32.58243634623561 ], [ -116.99120624064994, 32.581712707774301 ], [ -116.99562328744466, 32.582107923888408 ], [ -117.00194664640887, 32.582877158297073 ], [ -117.0089538927989, 32.583923150362544 ], [ -117.01141157202515, 32.584748556651512 ], [ -117.01434837489472, 32.585796911898029 ], [ -117.01815538476318, 32.586200321663206 ], [ -117.02285874709682, 32.587394380373176 ], [ -117.02575839115465, 32.586431974986255 ], [ -117.02839270252196, 32.585818237773253 ], [ -117.03196930799476, 32.584788393166896 ], [ -117.0353541393241, 32.584393233527791 ], [ -117.03987787273692, 32.583234126182219 ], [ -117.04456223839728, 32.583393332527173 ], [ -117.05183697612944, 32.584203049689997 ], [ -117.05593610914512, 32.585692117192636 ], [ -117.06281865949224, 32.587253753081214 ], [ -117.06796448881572, 32.58671587048164 ], [ -117.07223334682625, 32.58642076677404 ], [ -117.07703883952287, 32.585830015255262 ], [ -117.08162775469272, 32.584553520964334 ], [ -117.08146648913291, 32.583234519028593 ], [ -117.08023020967258, 32.582449325419546 ], [ -117.07885455120608, 32.581493901980991 ], [ -117.07727361062912, 32.580426941222797 ], [ -117.07425083808856, 32.578462550566684 ], [ -117.06938355028353, 32.575836820265927 ], [ -117.0679768595155, 32.574291366336219 ], [ -117.07241851206417, 32.574928753914364 ], [ -117.07518693202192, 32.574254743186088 ], [ -117.0780854909149, 32.573290881813598 ], [ -117.08413987452015, 32.574174428676862 ], [ -117.08924842286103, 32.575244771913034 ], [ -117.09115860317613, 32.575789876157522 ], [ -117.09510331040457, 32.576303242644421 ], [ -117.10010384262075, 32.575306669257621 ], [ -117.10647756024149, 32.575149662771501 ], [ -117.11190978595226, 32.575409504886181 ], [ -117.11386955416937, 32.575034406011433 ], [ -117.11623399545566, 32.574537576560076 ], [ -117.1215685168029, 32.576694287394176 ], [ -117.12613400930002, 32.577713895283864 ], [ -117.12803166530341, 32.577626667375966 ], [ -117.12945373702573, 32.577489321400627 ], [ -117.13316931636456, 32.57809929670556 ], [ -117.1331563833194, 32.580461353063633 ], [ -117.13313895151131, 32.583667037314477 ], [ -117.13305735034756, 32.583895362177643 ], [ -117.13302977296665, 32.585640269688504 ], [ -117.13295680012118, 32.588445797309987 ], [ -117.13289081976185, 32.590978124650832 ], [ -117.13297029012178, 32.593220472898004 ], [ -117.13327298374499, 32.596429216273556 ], [ -117.13369223287799, 32.600867834211961 ], [ -117.12357324390332, 32.599849353180694 ], [ -117.11589749104179, 32.600462415367865 ], [ -117.1158318107198, 32.599777199361775 ], [ -117.11569656396389, 32.599570329888948 ], [ -117.11357898780251, 32.600647416312967 ], [ -117.11177228173005, 32.601566355813603 ], [ -117.11086691646757, 32.6021189358108 ], [ -117.109148177021, 32.60316887609109 ], [ -117.10657781402381, 32.605005474819528 ], [ -117.10440205308318, 32.606810459053861 ], [ -117.10381630211057, 32.607296700104691 ], [ -117.10324926304537, 32.607915653098537 ], [ -117.1011662702258, 32.612340439524345 ], [ -117.10075972866082, 32.612638859150657 ], [ -117.09977514995659, 32.61283800452685 ], [ -117.09894626905405, 32.613005656528017 ], [ -117.09840512482522, 32.613004595665437 ], [ -117.09740410874794, 32.613049694419168 ], [ -117.09705139513309, 32.613600174933183 ], [ -117.09705183800251, 32.613714871347049 ], [ -117.09843269412127, 32.614767877138462 ], [ -117.09843275610504, 32.615089758769592 ], [ -117.09764747762975, 32.616740386051163 ], [ -117.09756685742629, 32.617197966049851 ], [ -117.09758669043111, 32.617689185373024 ], [ -117.09759495313509, 32.617885380739892 ], [ -117.09783823577867, 32.618045356304386 ], [ -117.09794643605798, 32.618320326264303 ], [ -117.09786566853079, 32.619785920093079 ], [ -117.09094993890868, 32.619563585315568 ] ] ] ] } }
]
}
我想针对所有" Basin_ID"测试点数1,2,3,4,如果它们在该盆地内,则将其作为列添加到countyDF。
例如,如果第1点位于第9-18区,并且其他任何一点都不属于盆地数据中包含的多边形,则返回的数据框将如下所示......
returnedDF:
pointNum Lat Long Basin
1 100 251 9-18
2 150 175 n/a
3 50 -330 n/a
4 -150 100 n/a
有人可能会建议一个特定的库/方法/解决方案来实现这个目标吗?我想如果有一个库可以测试一个点是否在多边形中,我可以遍历每个点的第二个数据帧吗?
答案 0 :(得分:1)
以下是一种可以执行此操作的方法,使用rgdal
读取geojson(see this answer for more details on this)和sp
和/或rgeos
来测试点是否位于多边形与否。
注意,我调整了你的坐标,因为它们都没有位于多边形内。
首先,阅读数据:
countyDF <- read.table(textConnection("
pointNum Lat Long
1 32.6 -117.1
2 90 175
4 -90 100"), header = TRUE)
basinDF <- rgdal::readOGR("basin.json", "OGRGeoJSON")
确保点和多边形具有相同的投影:
sp::coordinates(countyDF) <- ~Long+Lat
sp::proj4string(countyDF) <- sp::proj4string(basinDF)
在这里,我们使用sp::over
在每个点提取basinDF
的属性。如果点不在basinDF
NA
的多边形内,则返回。
sp::over(countyDF, basinDF)
# Basin_ID Basin_Subb Basin_Name Subbasin_N
# 1 9-18 9-18 OTAY VALLEY <NA>
# 2 <NA> <NA> <NA> <NA>
# 3 <NA> <NA> <NA> <NA>
或者,您也可以使用rgeos
,它告诉您第1点位于poygon 1中。
rgeos::gWithin(countyDF, basinDF, byid = TRUE)
# 1 2 3
# 0 FALSE FALSE FALSE
# 1 TRUE FALSE FALSE