将geoJSON与另一个数组进行比较,如果存在匹配项,该怎么办?

时间:2019-02-08 04:04:54

标签: javascript jquery arrays leaflet

在这里遇到一些麻烦。我有一个30个机场的geoJSON变量,我试图将其与FAA的数据进行比较,但是不确定如果两个数组包含匹配的机场名称,如何更改我的传单图标。

{"status":{"code":200,"info":"OK","count":3},"GroundDelays":{"groundDelay":[{"airport":"CYYZ","avgTime":"4 hours and 29 minutes","reason":"WX / WIND"},{"airport":"EWR","avgTime":"1 hour and 57 minutes","reason":"VOLUME / VOLUME"},{"airport":"MSP","avgTime":"4 hours and 21 minutes","reason":"WEATHER / SNOW-ICE"}],"count":3},"GroundStops":{"groundStop":[],"count":0},"ArriveDepartDelays":{"arriveDepart":[],"count":0},"Closures":{"closure":[],"count":0}}

以上内容来自FAA Rest API。

var airports=[{"type":"Feature","properties":{"icao":"KSEA","iata":"SEA","name":"Seattle Tacoma International Airport"},"geometry":{"type":"Point","coordinates":[-122.3089981079,47.4490013123]}},{"type":"Feature","properties":{"icao":"KPDX","iata":"PDX","name":"Seattle Tacoma International Airport"},"geometry":{"type":"Point","coordinates":[-122.5979996,45.58869934]}},{"type":"Feature","properties":{"icao":"KSFO","iata":"SFO","name":"San Francisco International Aiport"},"geometry":{"type":"Point","coordinates":[-122.375,37.6189994812]}},{"type":"Feature","properties":{"icao":"KSJC","iata":"SJC","name":"Norman Y. Mineta San Jose International Airport"},"geometry":{"type":"Point","coordinates":[-121.9290008545,37.3625984192]}},{"type":"Feature","properties":{"icao":"KLAX","iata":"LAX","name":"Los Angeles International Airport"},"geometry":{"type":"Point","coordinates":[-118.4079971,33.94250107]}},{"type":"Feature","properties":{"icao":"KSAN","iata":"SAN","name":"San Diego International Airport"},"geometry":{"type":"Point","coordinates":[-117.1900024414,32.7336006165]}},{"type":"Feature","properties":{"icao":"KLAS","iata":"LAS","name":"McCarran International Airport"},"geometry":{"type":"Point","coordinates":[-115.1520004,36.08010101]}},{"type":"Feature","properties":{"icao":"KPHX","iata":"PHX","name":"Phoenix Sky Harbor International Airport"},"geometry":{"type":"Point","coordinates":[-112.0120010376,33.434299469]}},{"type":"Feature","properties":{"icao":"KSLC","iata":"SLC","name":"Salt Lake City International Airport"},"geometry":{"type":"Point","coordinates":[-111.9779968262,40.7883987427]}},{"type":"Feature","properties":{"icao":"KDEN","iata":"DEN","name":"Denver International Airport"},"geometry":{"type":"Point","coordinates":[-104.672996521,39.8616981506]}},{"type":"Feature","properties":{"icao":"KMSP","iata":"MSP","name":"Minneapolis-St Paul International\/Wold-Chamberlain Airport"},"geometry":{"type":"Point","coordinates":[-93.2218017578,44.8819999695]}},{"type":"Feature","properties":{"icao":"KMCI","iata":"MCI","name":"Kansas City International Airport"},"geometry":{"type":"Point","coordinates":[-94.7138977051,39.2975997925]}},{"type":"Feature","properties":{"icao":"KDFW","iata":"DFW","name":"Dallas Fort Worth International Airport"},"geometry":{"type":"Point","coordinates":[-97.0380020142,32.8968009949]}},{"type":"Feature","properties":{"icao":"KIAH","iata":"IAH","name":"George Bush Intercontinental Houston Airport"},"geometry":{"type":"Point","coordinates":[-95.3414001465,29.9843997955]}},{"type":"Feature","properties":{"icao":"KORD","iata":"ORD","name":"Chicago O'Hare International Airport"},"geometry":{"type":"Point","coordinates":[-87.90480042,41.97859955]}},{"type":"Feature","properties":{"icao":"KSTL","iata":"STL","name":"Lambert St Louis International Airport"},"geometry":{"type":"Point","coordinates":[-90.3700027466,38.7486991882]}},{"type":"Feature","properties":{"icao":"KMEM","iata":"MEM","name":"Memphis International Airport"},"geometry":{"type":"Point","coordinates":[-89.9766998291,35.0424003601]}},{"type":"Feature","properties":{"icao":"KBNA","iata":"BNA","name":"Nashville International Airport"},"geometry":{"type":"Point","coordinates":[-86.6781997681,36.1245002747]}},{"type":"Feature","properties":{"icao":"KIND","iata":"IND","name":"Indianapolis International Airport"},"geometry":{"type":"Point","coordinates":[-86.2944030762,39.717300415]}},{"type":"Feature","properties":{"icao":"KMDW","iata":"MDW","name":"Chicago Midway International Airport"},"geometry":{"type":"Point","coordinates":[-87.7524032593,41.7859992981]}},{"type":"Feature","properties":{"icao":"KDTW","iata":"DTW","name":"Detroit Metropolitan Wayne County Airport"},"geometry":{"type":"Point","coordinates":[-83.3534011841,42.2123985291]}},{"type":"Feature","properties":{"icao":"KCLE","iata":"CLE","name":"Cleveland Hopkins International Airport"},"geometry":{"type":"Point","coordinates":[-81.8498001099,41.4117012024]}},{"type":"Feature","properties":{"icao":"KCVG","iata":"CVG","name":"Cincinnati Northern Kentucky International Airport"},"geometry":{"type":"Point","coordinates":[-84.6678009033,39.0488014221]}},{"type":"Feature","properties":{"icao":"KATL","iata":"ATL","name":"Hartsfield Jackson Atlanta International Airport"},"geometry":{"type":"Point","coordinates":[-84.4281005859,33.6366996765]}},{"type":"Feature","properties":{"icao":"KPIT","iata":"PIT","name":"Pittsburgh International Airport"},"geometry":{"type":"Point","coordinates":[-80.23290253,40.49150085]}},{"type":"Feature","properties":{"icao":"KIAD","iata":"IAD","name":"Washington Dulles International Airport"},"geometry":{"type":"Point","coordinates":[-77.45580292,38.94449997]}},{"type":"Feature","properties":{"icao":"KDCA","iata":"DCA","name":"Ronald Reagan Washington National Airport"},"geometry":{"type":"Point","coordinates":[-77.0376968384,38.8521003723]}},{"type":"Feature","properties":{"icao":"KBWI","iata":"BWI","name":"Baltimore\/Washington International Thurgood Marshal Airport"},"geometry":{"type":"Point","coordinates":[-76.66829681,39.17539978]}},{"type":"Feature","properties":{"icao":"KPHL","iata":"PHL","name":"Philadelphia International Airport"},"geometry":{"type":"Point","coordinates":[-75.2410964966,39.8718986511]}},{"type":"Feature","properties":{"icao":"KEWR","iata":"EWR","name":"Newark Liberty International Airport"},"geometry":{"type":"Point","coordinates":[-74.1687011719,40.6925010681]}},{"type":"Feature","properties":{"icao":"KTEB","iata":"TEB","name":"Teterboro Airport"},"geometry":{"type":"Point","coordinates":[-74.060798645,40.8501014709]}},{"type":"Feature","properties":{"icao":"KJFK","iata":"JFK","name":"John F Kennedy International Airport"},"geometry":{"type":"Point","coordinates":[-73.77890015,40.63980103]}},{"type":"Feature","properties":{"icao":"KLGA","iata":"LGA","name":"La Guardia Airport"},"geometry":{"type":"Point","coordinates":[-73.87259674,40.77719879]}},{"type":"Feature","properties":{"icao":"KBOS","iata":"BOS","name":"General Edward Lawrence Logan International Airport"},"geometry":{"type":"Point","coordinates":[-71.00520325,42.36429977]}},{"type":"Feature","properties":{"icao":"KRDU","iata":"RDU","name":"Raleigh Durham International Airport"},"geometry":{"type":"Point","coordinates":[-78.7874984741,35.8776016235]}},{"type":"Feature","properties":{"icao":"KCLT","iata":"CLT","name":"Charlotte Douglas International Airport"},"geometry":{"type":"Point","coordinates":[-80.9430999756,35.2140007019]}},{"type":"Feature","properties":{"icao":"KTPA","iata":"TPA","name":"Tampa International Airport"},"geometry":{"type":"Point","coordinates":[-82.533203125,27.9755001068]}},{"type":"Feature","properties":{"icao":"KMCO","iata":"MCO","name":"Orlando International Airport"},"geometry":{"type":"Point","coordinates":[-81.3089981079,28.4293994904]}},{"type":"Feature","properties":{"icao":"KFLL","iata":"FLL","name":"Fort Lauderdale Hollywood International Airport"},"geometry":{"type":"Point","coordinates":[-80.1527023315,26.072599411]}},{"type":"Feature","properties":{"icao":"KMIA","iata":"MIA","name":"Miami International Airport"},"geometry":{"type":"Point","coordinates":[-80.2906036377,25.7931995392]}}];

上面是我创建的机场的geoJSON文件。

现在,我使用geoJSON文件填充传单地图,例如:

    var dataLayer = L.geoJSON(airports, {
        pointToLayer: function (feature, latlng) {
            return L.marker(latlng, {icon: myIcon});
        }
    });
    dataLayer.addTo(map);

然后我以$ .each来查询FAA数据,但是我不确定如何遍历geoJSON以找到匹配项,然后更新L.DivIcon来更改颜色。

success: function (data, status, xhr) {
    $.each(data.GroundDelays.groundDelay, function(index, i) {
        var row = "<tr><td>Ground Delay</td><td>"+i.airport+"</td><td>"+i.avgTime+"</td><td>"+i.reason+"</td></tr>";
        table.row.add($(row)).draw();
    });

2 个答案:

答案 0 :(得分:0)

提供了一个可能对您有用的辅助功能。基本上,它会遍历FAA并检查geoJSON中的机场是否存在,并根据该存在返回一个布尔值。

让我知道你的想法。

function hasAirportInFAA(airport) {

    // Filter to check if the it exist
    const filteredAirport = airports.filter(x => x.properties.iata.trim() === airport.trim());

    // If the length is 0 , it doesnt contain and also is false , if one, that means it contains , hence true
    return !!filteredAirport.length;

}

如果要进行测试

const geoJSON = { "status": { "code": 200, "info": "OK", "count": 3 }, "GroundDelays": { "groundDelay": [{ "airport": "CYYZ", "avgTime": "4 hours and 29 minutes", "reason": "WX / WIND" }, { "airport": "EWR", "avgTime": "1 hour and 57 minutes", "reason": "VOLUME / VOLUME" }, { "airport": "MSP", "avgTime": "4 hours and 21 minutes", "reason": "WEATHER / SNOW-ICE" }], "count": 3 }, "GroundStops": { "groundStop": [], "count": 0 }, "ArriveDepartDelays": { "arriveDepart": [], "count": 0 }, "Closures": { "closure": [], "count": 0 } }

const airports = [{ "type": "Feature", "properties": { "icao": "KSEA", "iata": "SEA", "name": "Seattle Tacoma International Airport" }, "geometry": { "type": "Point", "coordinates": [-122.3089981079, 47.4490013123] } }, { "type": "Feature", "properties": { "icao": "KPDX", "iata": "PDX", "name": "Seattle Tacoma International Airport" }, "geometry": { "type": "Point", "coordinates": [-122.5979996, 45.58869934] } }, { "type": "Feature", "properties": { "icao": "KSFO", "iata": "SFO", "name": "San Francisco International Aiport" }, "geometry": { "type": "Point", "coordinates": [-122.375, 37.6189994812] } }, { "type": "Feature", "properties": { "icao": "KSJC", "iata": "SJC", "name": "Norman Y. Mineta San Jose International Airport" }, "geometry": { "type": "Point", "coordinates": [-121.9290008545, 37.3625984192] } }, { "type": "Feature", "properties": { "icao": "KLAX", "iata": "LAX", "name": "Los Angeles International Airport" }, "geometry": { "type": "Point", "coordinates": [-118.4079971, 33.94250107] } }, { "type": "Feature", "properties": { "icao": "KSAN", "iata": "SAN", "name": "San Diego International Airport" }, "geometry": { "type": "Point", "coordinates": [-117.1900024414, 32.7336006165] } }, { "type": "Feature", "properties": { "icao": "KLAS", "iata": "LAS", "name": "McCarran International Airport" }, "geometry": { "type": "Point", "coordinates": [-115.1520004, 36.08010101] } }, { "type": "Feature", "properties": { "icao": "KPHX", "iata": "PHX", "name": "Phoenix Sky Harbor International Airport" }, "geometry": { "type": "Point", "coordinates": [-112.0120010376, 33.434299469] } }, { "type": "Feature", "properties": { "icao": "KSLC", "iata": "SLC", "name": "Salt Lake City International Airport" }, "geometry": { "type": "Point", "coordinates": [-111.9779968262, 40.7883987427] } }, { "type": "Feature", "properties": { "icao": "KDEN", "iata": "DEN", "name": "Denver International Airport" }, "geometry": { "type": "Point", "coordinates": [-104.672996521, 39.8616981506] } }, { "type": "Feature", "properties": { "icao": "KMSP", "iata": "MSP", "name": "Minneapolis-St Paul International\/Wold-Chamberlain Airport" }, "geometry": { "type": "Point", "coordinates": [-93.2218017578, 44.8819999695] } }, { "type": "Feature", "properties": { "icao": "KMCI", "iata": "MCI", "name": "Kansas City International Airport" }, "geometry": { "type": "Point", "coordinates": [-94.7138977051, 39.2975997925] } }, { "type": "Feature", "properties": { "icao": "KDFW", "iata": "DFW", "name": "Dallas Fort Worth International Airport" }, "geometry": { "type": "Point", "coordinates": [-97.0380020142, 32.8968009949] } }, { "type": "Feature", "properties": { "icao": "KIAH", "iata": "IAH", "name": "George Bush Intercontinental Houston Airport" }, "geometry": { "type": "Point", "coordinates": [-95.3414001465, 29.9843997955] } }, { "type": "Feature", "properties": { "icao": "KORD", "iata": "ORD", "name": "Chicago O'Hare International Airport" }, "geometry": { "type": "Point", "coordinates": [-87.90480042, 41.97859955] } }, { "type": "Feature", "properties": { "icao": "KSTL", "iata": "STL", "name": "Lambert St Louis International Airport" }, "geometry": { "type": "Point", "coordinates": [-90.3700027466, 38.7486991882] } }, { "type": "Feature", "properties": { "icao": "KMEM", "iata": "MEM", "name": "Memphis International Airport" }, "geometry": { "type": "Point", "coordinates": [-89.9766998291, 35.0424003601] } }, { "type": "Feature", "properties": { "icao": "KBNA", "iata": "BNA", "name": "Nashville International Airport" }, "geometry": { "type": "Point", "coordinates": [-86.6781997681, 36.1245002747] } }, { "type": "Feature", "properties": { "icao": "KIND", "iata": "IND", "name": "Indianapolis International Airport" }, "geometry": { "type": "Point", "coordinates": [-86.2944030762, 39.717300415] } }, { "type": "Feature", "properties": { "icao": "KMDW", "iata": "MDW", "name": "Chicago Midway International Airport" }, "geometry": { "type": "Point", "coordinates": [-87.7524032593, 41.7859992981] } }, { "type": "Feature", "properties": { "icao": "KDTW", "iata": "DTW", "name": "Detroit Metropolitan Wayne County Airport" }, "geometry": { "type": "Point", "coordinates": [-83.3534011841, 42.2123985291] } }, { "type": "Feature", "properties": { "icao": "KCLE", "iata": "CLE", "name": "Cleveland Hopkins International Airport" }, "geometry": { "type": "Point", "coordinates": [-81.8498001099, 41.4117012024] } }, { "type": "Feature", "properties": { "icao": "KCVG", "iata": "CVG", "name": "Cincinnati Northern Kentucky International Airport" }, "geometry": { "type": "Point", "coordinates": [-84.6678009033, 39.0488014221] } }, { "type": "Feature", "properties": { "icao": "KATL", "iata": "ATL", "name": "Hartsfield Jackson Atlanta International Airport" }, "geometry": { "type": "Point", "coordinates": [-84.4281005859, 33.6366996765] } }, { "type": "Feature", "properties": { "icao": "KPIT", "iata": "PIT", "name": "Pittsburgh International Airport" }, "geometry": { "type": "Point", "coordinates": [-80.23290253, 40.49150085] } }, { "type": "Feature", "properties": { "icao": "KIAD", "iata": "IAD", "name": "Washington Dulles International Airport" }, "geometry": { "type": "Point", "coordinates": [-77.45580292, 38.94449997] } }, { "type": "Feature", "properties": { "icao": "KDCA", "iata": "DCA", "name": "Ronald Reagan Washington National Airport" }, "geometry": { "type": "Point", "coordinates": [-77.0376968384, 38.8521003723] } }, { "type": "Feature", "properties": { "icao": "KBWI", "iata": "BWI", "name": "Baltimore\/Washington International Thurgood Marshal Airport" }, "geometry": { "type": "Point", "coordinates": [-76.66829681, 39.17539978] } }, { "type": "Feature", "properties": { "icao": "KPHL", "iata": "PHL", "name": "Philadelphia International Airport" }, "geometry": { "type": "Point", "coordinates": [-75.2410964966, 39.8718986511] } }, { "type": "Feature", "properties": { "icao": "KEWR", "iata": "EWR", "name": "Newark Liberty International Airport" }, "geometry": { "type": "Point", "coordinates": [-74.1687011719, 40.6925010681] } }, { "type": "Feature", "properties": { "icao": "KTEB", "iata": "TEB", "name": "Teterboro Airport" }, "geometry": { "type": "Point", "coordinates": [-74.060798645, 40.8501014709] } }, { "type": "Feature", "properties": { "icao": "KJFK", "iata": "JFK", "name": "John F Kennedy International Airport" }, "geometry": { "type": "Point", "coordinates": [-73.77890015, 40.63980103] } }, { "type": "Feature", "properties": { "icao": "KLGA", "iata": "LGA", "name": "La Guardia Airport" }, "geometry": { "type": "Point", "coordinates": [-73.87259674, 40.77719879] } }, { "type": "Feature", "properties": { "icao": "KBOS", "iata": "BOS", "name": "General Edward Lawrence Logan International Airport" }, "geometry": { "type": "Point", "coordinates": [-71.00520325, 42.36429977] } }, { "type": "Feature", "properties": { "icao": "KRDU", "iata": "RDU", "name": "Raleigh Durham International Airport" }, "geometry": { "type": "Point", "coordinates": [-78.7874984741, 35.8776016235] } }, { "type": "Feature", "properties": { "icao": "KCLT", "iata": "CLT", "name": "Charlotte Douglas International Airport" }, "geometry": { "type": "Point", "coordinates": [-80.9430999756, 35.2140007019] } }, { "type": "Feature", "properties": { "icao": "KTPA", "iata": "TPA", "name": "Tampa International Airport" }, "geometry": { "type": "Point", "coordinates": [-82.533203125, 27.9755001068] } }, { "type": "Feature", "properties": { "icao": "KMCO", "iata": "MCO", "name": "Orlando International Airport" }, "geometry": { "type": "Point", "coordinates": [-81.3089981079, 28.4293994904] } }, { "type": "Feature", "properties": { "icao": "KFLL", "iata": "FLL", "name": "Fort Lauderdale Hollywood International Airport" }, "geometry": { "type": "Point", "coordinates": [-80.1527023315, 26.072599411] } }, { "type": "Feature", "properties": { "icao": "KMIA", "iata": "MIA", "name": "Miami International Airport" }, "geometry": { "type": "Point", "coordinates": [-80.2906036377, 25.7931995392] } }];


function hasAirportInFAA(airport) {

    // Filter to check if the it exist
    const filteredAirport = airports.filter(x => x.properties.iata.trim() === airport.trim());

    // If the length is 0 , it doesnt contain and also is false , if one, that means it contains , hence true
    return !!filteredAirport.length;

}

geoJSON.GroundDelays.groundDelay.map(data => {

    const hasInFAA = hasAirportInFAA(data.airport);
    console.log(`${data.airport}  :  ${(hasInFAA) ? "Contains" : " Doesn't contain"} `);


})

答案 1 :(得分:0)

我认为您很困惑,因为L.GeoJSON.eachLayer()(继承自L.LayerGroup.eachLayer())遍历传单层,而不是遍历 GeoJSON功能

换句话说:pointToLayer回调一旦运行,特征数据就会丢失。

因此,让我们首先在图层(即每个L.Marker中)存储要素的一些属性(在您的情况下为机场代码):

var airportMarkers = L.geoJSON(airports, {
    pointToLayer: function (feature, latlng) {
        var marker = L.marker(latlng, {icon: myIcon});
        marker.airportCode = feature.properties.icao;
        return marker;
    }
}).addTo(map);

现在,我可以使用airportMarkers.eachLayer()遍历标记,每个标记对象将具有一个.airportCode属性。

首先,为了方便起见,我们将延迟数据map延迟到ICAO代码的简单数组中(从远程端点获取数据之后):

var delayedAirportCodes = data.GroundDelays.groundDelay.map( function(record) {
    return record.airport;
} );

现在delayedAirportCodes应该是纯字符串数组,格式为["CYYZ", "EWR", "MSP"]。我只希望能够使用Array.includes(),恕我直言,该代码使代码更具可读性。

有了这个,我们现在可以遍历机场的 markers 并重置图标:

airportMarkers.eachMarker(function(marker){
     // Check if the array of delayed ICAO codes contains the ICAO
     // code stored in this marker
     if (delayedAirportCodes.includes(marker.airportCode)) {
        // If so, change the icon
        marker.setIcon( L.icon(  /* etc */ ) );
     }
});