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
const response = await fetch('https://raw.githubusercontent.com/datasets/geo-countries/master/data/countries.geojson')
const worldData = await response.json()
try {
// Up-to-date version can be found on Github:
// 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)
const filteredFeatures = worldData.features.filter(feature =>
visitedCountries.includes(feature.properties.ISO_A2)
)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
this.scratchLayer.addData({
type: 'FeatureCollection',
features: filteredFeatures
})
const worldData = await response.json();
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) {
if (!this.markers) return [];

View file

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