Back to EIP
Canonical Data Model
⋯
service /map on new http:Listener(8080) {
resource function post uploadPlaceMarks(http:Request request) returns GMapResponse|error? {
Csv|Gpx data = check getPayload(request);
GeoJson geoJson = convertToCanonical(data);
Kml kmlData = convertFromCanonicalToKml(geoJson);
GMapResponse gMapResponse = check gMapClient->post(
string `/v1/projects/${PROJECT_ID}/datasets/${DATASET_ID}:import`, kmlData);
return gMapResponse;
}
resource function post getRouteDirection(http:Request request, string query) returns MicrosoftMapResponse|error {
Csv|Gpx data = check getPayload(request);
GeoJson geoJson = convertToCanonical(data);
MicrosoftMapResponse microsoftMapResponse = check microsoftClient->/route/directions/'json.post(
{"supportingPoints": geoJson.toJson()}, {query: string `${query}`}
);
return microsoftMapResponse;
}
}
isolated function convertToCanonical(Csv|Gpx data) returns GeoJson {
if data is Csv {
return convertFromCsvToCanonical(data);
} else {
return convertFromGpxToCanonical(data);
}
}
isolated function getPayload(http:Request request) returns Csv|Gpx|error {
if request.getContentType().includes("xml") {
return request.getXmlPayload();
}
json data = check request.getJsonPayload();
return data.cloneWithType(Csv);
}
isolated function convertFromCsvToCanonical(Csv data) returns GeoJson {
return {
features: from var member in data
let string X = member.X.toString(), string Y = member.Y.toString()
select {
properties: {X, Y, Name: member.Name, description: member.Description},
geometry: {coordinates: [X, Y]}
}
};
}
isolated function convertFromGpxToCanonical(Gpx gpxData) returns GeoJson {
return {
features: from var extension in gpxData/**/<extensions>
select {
properties: {
X: (extension/**/<X>).data(),
Y: (extension/**/<X>).data(),
Name: (extension/**/<Name>).data(),
description: (extension/**/<description>).data()
},
geometry: {coordinates: [(extension/**/<X>).data(), (extension/**/<Y>).data()]}
}
};
}
isolated function convertFromCanonicalToKml(GeoJson geoJson) returns Kml {
xml kmlData = xml `<kml>
<Document>
<Schema id="temp">
<SimpleField name="X" type="double"/>
<SimpleField name="Y" type="double"/>
<SimpleField name="Name" type="string"/>
</Schema>
${from Feature feature in geoJson.features
select xml `<Placemark>
<description>${feature.properties.description}</description>
<Point>
<coordinates>
${feature.geometry.coordinates[0]},${feature.geometry.coordinates[1]}
</coordinates>
</Point>
</Placemark>`}
</Document>
</kml>`;
return kmlData;
}