Skip to content

Instantly share code, notes, and snippets.

@vesse
Last active July 13, 2020 19:48
Show Gist options
  • Select an option

  • Save vesse/007e0d0e2bfafeb917efb0d60fbb6a5c to your computer and use it in GitHub Desktop.

Select an option

Save vesse/007e0d0e2bfafeb917efb0d60fbb6a5c to your computer and use it in GitHub Desktop.

Revisions

  1. vesse renamed this gist Jul 13, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. vesse revised this gist Jul 13, 2020. 1 changed file with 7 additions and 12 deletions.
    19 changes: 7 additions & 12 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,3 @@
    /**
    * Quick and dirty test only
    */
    import axios from 'axios';
    import * as proj4 from 'proj4';

    @@ -43,9 +40,7 @@ const polygon: [number, number][] = [
    ];

    const filter = getFilter(
    polygon.map(([lat, lon]: [number, number]) =>
    proj4('WGS84', 'TM35FIN', [lon, lat])
    )
    polygon.map(([lat, lon]) => proj4('WGS84', 'TM35FIN', [lon, lat]))
    );

    const endpoint = 'http://geo.stat.fi/geoserver/postialue/wfs';
    @@ -57,18 +52,18 @@ const params = new URLSearchParams({
    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.forEach((feature) =>
    console.log(feature.properties.posti_alue)
    )
    geojson.features
    .map((feature) => feature.properties.posti_alue)
    .join('\n')
    )
    .catch((err) => {
    console.error('Failed', err);
    });
    .then(console.log)
    .catch(console.error);
    }
  3. vesse created this gist Jul 13, 2020.
    74 changes: 74 additions & 0 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    /**
    * Quick and dirty test only
    */
    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]: [number, number]) =>
    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,
    });

    if (!module.parent) {
    axios
    .get(endpoint, { params })
    .then((response) => response.data)
    .then((geojson: Result) =>
    geojson.features.forEach((feature) =>
    console.log(feature.properties.posti_alue)
    )
    )
    .catch((err) => {
    console.error('Failed', err);
    });
    }