Currently in my project I have a base map layer taken from OpenMapStreet. On top of this I would like to have an additional layer which adds polygons of specific countries on top of the base map layer. I have a geoJson file of the world map which includes all the geometric coordinates to draw polygons for all the countries in the world and a json file, lets call it "cities.json" which looks something like this:


{ "City": "Angers", "Country": "France", "ISO3": "FRA", },

{ "City": "Bar-le-Duc", "Country": "France", "ISO3": "FRA", },

{ "City": "Agawam", "Country": "United States of America", "ISO3": "USA", },

{ "City": "Albany County, NY", "Country": "United States of America", "ISO3": "USA", }


My objective is to check whether or not the country exists in cities.json before adding the country's polygon on to my geoJson layer for my leaflet map. So, as seen in the example, we have the country of France and the United States of America in the cities.json file, and so in the geoJson layer of my leaflet map, there will be a colored polygon over these two countries. How to achieve this outcome?

closed as off-topic by IvanSanchez, BERA, nmtoken, aldo_tapia, xunilk Apr 11 '18 at 14:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – BERA, nmtoken, aldo_tapia, xunilk
If this question can be reworded to fit the rules in the help center, please edit the question.


You can use the filter option of the L.geoJSON object.

I assume

  • a matching "ISO3" property in your GeoJSON

  • your GeoJSON is parsed into var borders

  • your cities.json is parsed into var cities

Define a filter function that returns true if the feature's "ISO3" property matches one of the cities array's object "ISO3" properties:

function filterForISO(feature) {
  for (var i = 0, len = cities.length; i < len; i++) {
    if (cities[i]["ISO3"] == feature.properties["ISO3"]) return true;

Add borders to the map as a L.geoJSON and pass that function to it's filter option:

var border_polygons = L.geoJSON(borders, {filter: filterForISO}).addTo(map);
  • Thank you @ThingumaBob. Your answer and assumptions were spot on. Sorry for the post not being too detailed as I am inexperienced. After implementing the function with my parsed data variables and passing it to L.geoJSON's filter option, only countries found in "cities.json" will be projected on to my leaflet map's geoJSON layer! – MelBB Apr 11 '18 at 17:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.