Skip to content

Instantly share code, notes, and snippets.

@LouisaKB
Last active April 24, 2020 13:12
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 LouisaKB/ee67e97279691bee7b99c444776fbe98 to your computer and use it in GitHub Desktop.
Save LouisaKB/ee67e97279691bee7b99c444776fbe98 to your computer and use it in GitHub Desktop.
The complete code for reference:
function remapLinearRing(linearRing) {
return linearRing.map(c => [c['lng'], c['lat']]);
}
function shapesToMultiPolygon(shapes) {
var allRings = shapes.map(function (shape) {
var shell = remapLinearRing(shape['shell']);
var holes = shape['holes'].map(h => remapLinearRing(h));
return [shell] + holes;
});
return {
'type': 'MultiPolygon',
'coordinates': allRings
};
}
function toGeojson(timeMapResponse) {
var responseData = JSON.parse(timeMapResponse);
var multiPolygons = responseData['results'].map(r => shapesToMultiPolygon(r['shapes']));
return {
'type': 'FeatureCollection',
'features': multiPolygons
};
}
@mathiash98
Copy link

The code is not working

Here is a Working typescript code:

function remapLinearRing(linearRing: Shell[]): GeoJSON.Position[] {
    return linearRing.map(latlng => [latlng['lng'], latlng['lat']]);
}

function shapesToMultiPolygon(shapes: Shape[]): GeoJSON.MultiPolygon {
    const allRings: GeoJSON.Position[][][] = shapes.map((shape) => {
        const shell = remapLinearRing(shape['shell']);
        const holes = shape.holes.map(h => remapLinearRing(h));
        return [shell].concat(holes);
    });
    return {
   	 'type': 'MultiPolygon',
   	 'coordinates': allRings
    };
}

export default function toGeojson(timeMapResponse: string): GeoJSON.GeoJSON {
    const responseData: TravelTimeMapResponse = JSON.parse(timeMapResponse);
    const multiPolygons = responseData.results.map(r => shapesToMultiPolygon(r['shapes']));
    const features: GeoJSON.Feature[] = multiPolygons.map(mp => {
        return {
            geometry: mp,
            type: "Feature",
            properties: {}
        }        
    });

    return {
   	 'type': 'FeatureCollection',
   	 'features': features
    };
}

export interface TravelTimeMapResponse {
    results: Result[];
}

export interface Result {
    search_id:  string;
    shapes:     Shape[];
    properties: Properties;
}

export interface Properties {
    [key: string]: unknown;
}

export interface Shape {
    shell: Shell[];
    holes: Array<Shell[]>;
}

export interface Shell {
    lat: number;
    lng: number;
}

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