Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Created December 19, 2024 10:15
Show Gist options
  • Save ThomasG77/96068363e74b4c1c148c55aad9ab0cf4 to your computer and use it in GitHub Desktop.
Save ThomasG77/96068363e74b4c1c148c55aad9ab0cf4 to your computer and use it in GitHub Desktop.
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.15.0/proj4.min.js" integrity="sha512-4cx6CH/aJi50kdbQ5dj6bDJM1WS0AOFBGgBfgSP2Ux2eK/5JhkL2pjUDIHwd8z6rfGuGr/aptDqs0EzbeRndvQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
proj4.defs("EPSG:2154","+proj=lcc +lat_0=46.5 +lon_0=3 +lat_1=49 +lat_2=44 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs");
function geometryPoint(entry, transformer) {
return transformer(entry)
}
function geometryLineString(entry, transformer) {
return entry.map(transformer)
}
function geometryPolygon(entry, transformer) {
return entry.map(point => point.map(coordPair => transformer(coordPair)))
}
geometryMultiPoint = geometryLineString
geometryMultiLineString = geometryPolygon
function geometryMultiPolygon(entry, transformer) {
return entry.map(part => part.map(point => point.map(coordPair => transformer(coordPair))))
}
const convert_info = {
'Point': geometryPoint,
'LineString': geometryLineString,
'Polygon': geometryPolygon,
'MultiPoint': geometryMultiPoint,
'MultiLineString': geometryMultiLineString,
'MultiPolygon': geometryMultiPolygon
}
let url = 'https://static.data.gouv.fr/resources/quartiers-prioritaires-de-la-politique-de-la-ville-qpv/20240822-094641/qp-2024-epsg2154-20240820.geojson'
// url = 'https://static.data.gouv.fr/resources/base-nationale-consolidee-des-zones-a-faibles-emissions/20241216-141149/aires.geojson'
fetch(url).then(res=>res.json()).then(geojson => {
const crs_urn = geojson.crs?.properties?.name
if (crs_urn) {
console.log('reproject !', crs_urn)
const transformer = proj4(crs_urn.replace('urn:ogc:def:crs:', '').replace('::', ':'), "EPSG:4326").forward
console.log(transformer([355608.74, 6690851.32]));
geojson_output = {
"type": "FeatureCollection",
"features": geojson.features.map(feature => ({
'type': feature.type,
'properties': feature.properties,
'geometry': {
'type': feature.geometry.type,
'coordinates': convert_info[feature.geometry.type](feature.geometry.coordinates, transformer)
}
}))
}
console.log(JSON.stringify(geojson_output))
} else {
console.log('keep it!')
}
})
const transformerOther = proj4("EPSG:4326", "EPSG:3857").forward
const point = {
"type": "Point",
"coordinates": [0.4, 45]
}
const line = {
"type": "LineString",
"coordinates": [
[10.0, 0.0],
[11.0, 1.0]
]
}
const poly_simple = {
"type": "Polygon",
"coordinates": [
[
[10.0, 0.0],
[11.0, 0.0],
[11.0, 1.0],
[10.0, 1.0],
[10.0, 0.0]
]
]
}
const poly_hole = {
"type": "Polygon",
"coordinates": [
[
[10.0, 0.0],
[11.0, 0.0],
[11.0, 1.0],
[10.0, 1.0],
[10.0, 0.0]
],
[
[10.8, 0.8],
[10.8, 0.2],
[10.2, 0.2],
[10.2, 0.8],
[10.8, 0.8]
]
]
}
const multipoint = {
"type": "MultiPoint",
"coordinates": [
[10.0, 0.0],
[11.0, 1.0]
]
}
const multiline = {
"type": "MultiLineString",
"coordinates": [
[
[10.0, 0.0],
[11.0, 1.0]
],
[
[12.0, 2.0],
[13.0, 3.0]
]
]
}
const multipoly = {
"type": "MultiPolygon",
"coordinates": [
[
[
[12.0, 2.0],
[13.0, 2.0],
[13.0, 3.0],
[12.0, 3.0],
[12.0, 2.0]
]
],
[
[
[10.0, 0.0],
[11.0, 0.0],
[11.0, 1.0],
[10.0, 1.0],
[10.0, 0.0]
],
[
[10.2, 0.2],
[10.2, 0.8],
[10.8, 0.8],
[10.8, 0.2],
[10.2, 0.2]
]
]
]
}
all_samples = [
point,
line,
poly_simple,
poly_hole,
multipoint,
multiline,
multipoly
]
all_samples.map(sample => {
console.log(convert_info[sample.type](sample.coordinates, transformerOther))
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment