From: Torsten Dreyer Date: Fri, 21 Mar 2014 12:00:59 +0000 (+0100) Subject: httpd navdb: report ils/loc as geojson multilinestring X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=527eb9d444071b1e1aec01b6bc41af165e28403b;p=flightgear.git httpd navdb: report ils/loc as geojson multilinestring --- diff --git a/src/Network/http/NavdbUriHandler.cxx b/src/Network/http/NavdbUriHandler.cxx index 14125f51b..7cc341d6a 100644 --- a/src/Network/http/NavdbUriHandler.cxx +++ b/src/Network/http/NavdbUriHandler.cxx @@ -20,7 +20,7 @@ #include "NavdbUriHandler.hxx" #include -#include +#include #include <3rdparty/cjson/cJSON.h> #include @@ -31,11 +31,64 @@ namespace http { //Ref: http://geojson.org/ -static cJSON * createGeometryFor(FGPositionedRef positioned) +static cJSON * createPosition(double x, double y) { - // for now, we are lazy - everything is just a point. + cJSON * p = cJSON_CreateArray(); + cJSON_AddItemToArray(p, cJSON_CreateNumber(x)); + cJSON_AddItemToArray(p, cJSON_CreateNumber(y)); + return p; +} +static cJSON * createGeometryFor(FGNavRecord * navRecord) +{ + if ( NULL == navRecord) return NULL; cJSON * geometry = cJSON_CreateObject(); + + if (navRecord->type() == FGPositioned::LOC || navRecord->type() == FGPositioned::ILS) { + int range = navRecord->get_range(); + + double width = navRecord->localizerWidth(); + double course = navRecord->get_multiuse(); + + double px[4]; + double py[4]; + + px[0] = navRecord->longitude(); + py[0] = navRecord->latitude(); + + for (int i = -1; i <= +1; i++) { + double c = SGMiscd::normalizeAngle((course + 180 + i*width/2 ) * SG_DEGREES_TO_RADIANS); + SGGeoc geoc = SGGeoc::fromGeod(navRecord->geod()); + SGGeod p2 = SGGeod::fromGeoc(geoc.advanceRadM(c, range * SG_NM_TO_METER)); + px[i+2] = p2.getLongitudeDeg(); + py[i+2] = p2.getLatitudeDeg(); + } + // Add three lines: centerline, left and right edge + cJSON_AddItemToObject(geometry, "type", cJSON_CreateString("MultiLineString")); + cJSON * coordinates = cJSON_CreateArray(); + cJSON_AddItemToObject(geometry, "coordinates", coordinates); + for (int i = 1; i < 4; i++) { + cJSON * line = cJSON_CreateArray(); + cJSON_AddItemToArray(coordinates, line); + cJSON_AddItemToArray(line, createPosition(px[0], py[0])); + cJSON_AddItemToArray(line, createPosition(px[i], py[i])); + } + + } else { + + cJSON_AddItemToObject(geometry, "type", cJSON_CreateString("Point")); + cJSON_AddItemToObject(geometry, "coordinates", createPosition(navRecord->longitude(), navRecord->latitude())); + } + + return geometry; +} + +static cJSON * createGeometryFor(FGPositionedRef positioned) +{ + cJSON * geometry = createGeometryFor(dynamic_cast(positioned.get())); + if ( NULL != geometry) return geometry; + + geometry = cJSON_CreateObject(); cJSON_AddItemToObject(geometry, "type", cJSON_CreateString("Point")); cJSON * coordinates = cJSON_CreateArray(); @@ -54,6 +107,24 @@ static cJSON * createPropertiesFor(FGPositionedRef positioned) cJSON_AddItemToObject(properties, "name", cJSON_CreateString(positioned->name().c_str())); cJSON_AddItemToObject(properties, "type", cJSON_CreateString(positioned->typeString())); cJSON_AddItemToObject(properties, "elevation-m", cJSON_CreateNumber(positioned->elevationM())); + FGNavRecord * navRecord = dynamic_cast(positioned.get()); + if ( NULL != navRecord) { + cJSON_AddItemToObject(properties, "range-nm", cJSON_CreateNumber(navRecord->get_range())); + cJSON_AddItemToObject(properties, "frequency", cJSON_CreateNumber((double) navRecord->get_freq() / 100.0)); + switch (navRecord->type()) { + case FGPositioned::ILS: + cJSON_AddItemToObject(properties, "localizer-course", cJSON_CreateNumber(navRecord->get_multiuse())); + break; + + case FGPositioned::VOR: + cJSON_AddItemToObject(properties, "variation", cJSON_CreateNumber(navRecord->get_multiuse())); + break; + + default: + break; + } + + } return properties; } @@ -81,6 +152,7 @@ static cJSON * createFeatureFor(FGPositionedRef positioned) bool NavdbUriHandler::handleRequest(const HTTPRequest & request, HTTPResponse & response) { + response.Header["Content-Type"] = "application/json; charset=UTF-8"; bool indent = request.RequestVariables.get("i") == "y";