Use local countries.geojson

This commit is contained in:
Eugene Burmakin 2024-11-01 14:05:16 +01:00
parent dd91b581f5
commit bc28babf15
5 changed files with 295 additions and 12 deletions

File diff suppressed because one or more lines are too long

View file

@ -154,21 +154,36 @@ export default class extends Controller {
} }
}) })
// the JSON is 23MB, so it's better to fetch it asynchronously or cache or use local copy try {
const response = await fetch('https://raw.githubusercontent.com/datasets/geo-countries/master/data/countries.geojson') // Up-to-date version can be found on Github:
const worldData = await response.json() // https://raw.githubusercontent.com/datasets/geo-countries/master/data/countries.geojson
const response = await fetch('/countries.geojson', {
headers: {
'Accept': 'application/geo+json,application/json'
}
});
const visitedCountries = this.getVisitedCountries(countryCodesMap) if (!response.ok) {
const filteredFeatures = worldData.features.filter(feature => throw new Error(`HTTP error! status: ${response.status}`);
visitedCountries.includes(feature.properties.ISO_A2) }
)
this.scratchLayer.addData({ const worldData = await response.json();
type: 'FeatureCollection',
features: filteredFeatures const visitedCountries = this.getVisitedCountries(countryCodesMap)
}) const filteredFeatures = worldData.features.filter(feature =>
visitedCountries.includes(feature.properties.ISO_A2)
)
this.scratchLayer.addData({
type: 'FeatureCollection',
features: filteredFeatures
})
} catch (error) {
console.error('Error loading GeoJSON:', error);
}
} }
getVisitedCountries(countryCodesMap) { getVisitedCountries(countryCodesMap) {
if (!this.markers) return []; if (!this.markers) return [];

View file

@ -64,6 +64,10 @@ Rails.application.configure do
# Suppress logger output for asset requests. # Suppress logger output for asset requests.
config.assets.quiet = true config.assets.quiet = true
config.assets.content_type = {
geojson: 'application/geo+json'
}
# Raises error for missing translations. # Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true # config.i18n.raise_on_missing_translations = true

View file

@ -0,0 +1,3 @@
# frozen_string_literal: true
Mime::Type.register 'application/geo+json', :geojson

261
public/countries.geojson Normal file

File diff suppressed because one or more lines are too long