Last active
July 13, 2020 19:48
-
-
Save vesse/007e0d0e2bfafeb917efb0d60fbb6a5c to your computer and use it in GitHub Desktop.
Finnish postal codes by intersection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
import * as proj4 from 'proj4'; | |
interface Result { | |
readonly features: ReadonlyArray<{ | |
readonly properties: { | |
readonly posti_alue: string; | |
}; | |
}>; | |
} | |
proj4.defs( | |
'TM35FIN', | |
'+proj=utm +zone=35 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs' | |
); | |
const getFilter = (coordinates: [number, number][]) => ` | |
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"> | |
<ogc:Intersects> | |
<ogc:PropertyName>geom</ogc:PropertyName> | |
<gml:Polygon xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:3067"> | |
<gml:exterior> | |
<gml:LinearRing> | |
<gml:posList> | |
${coordinates.map((coordinate) => coordinate.join(' ')).join(' ')} | |
</gml:posList> | |
</gml:LinearRing> | |
</gml:exterior> | |
</gml:Polygon> | |
</ogc:Intersects> | |
</ogc:Filter> | |
`; | |
const polygon: [number, number][] = [ | |
[61.511309, 23.726957], | |
[61.513618, 23.786633], | |
[61.491548, 23.797155], | |
[61.490941, 23.737657], | |
[61.511309, 23.726957], | |
]; | |
const filter = getFilter( | |
polygon.map(([lat, lon]) => proj4('WGS84', 'TM35FIN', [lon, lat])) | |
); | |
const endpoint = 'http://geo.stat.fi/geoserver/postialue/wfs'; | |
const params = new URLSearchParams({ | |
service: 'wfs', | |
version: '1.1.0', | |
request: 'GetFeature', | |
srsName: 'EPSG:3067', | |
outputFormat: 'json', | |
typeName: 'postialue:pno', | |
filter, | |
propertyName: 'posti_alue', | |
}); | |
if (!module.parent) { | |
axios | |
.get(endpoint, { params }) | |
.then((response) => response.data) | |
.then((geojson: Result) => | |
geojson.features | |
.map((feature) => feature.properties.posti_alue) | |
.join('\n') | |
) | |
.then(console.log) | |
.catch(console.error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The hard-coded
polygon
covers central Tampere area, and script prints out