Skip to content

Instantly share code, notes, and snippets.

@vesse
Last active July 13, 2020 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vesse/007e0d0e2bfafeb917efb0d60fbb6a5c to your computer and use it in GitHub Desktop.
Save vesse/007e0d0e2bfafeb917efb0d60fbb6a5c to your computer and use it in GitHub Desktop.
Finnish postal codes by intersection
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);
}
@vesse
Copy link
Author

vesse commented Jul 13, 2020

The hard-coded polygon covers central Tampere area, and script prints out

33100
33180
33200
33210
33230
33250
33500
33540

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment