/// <summary> /// Create valid json string from object /// </summary> /// <param name="RawGeoJson"></param> /// <returns>Valid json string, returns {} if empty</returns> private string GeoJsonObjectToString(Data.DBGeojsonObject RawGeoJson) { if (RawGeoJson.RawGeoJson == null) { return("{}"); } else { string JsonString = RawGeoJson.RawGeoJson.ToString(); if (JsonString == "") { return("{}"); } else { return(JsonString); } } }
/// <summary> /// Create standard geojson feature list from Raw geojson features(not like database structured) /// </summary> /// <param name="RawGeoJson"></param> /// <returns></returns> public List <Feature> ToGISGeojsonFeatures(Data.DBGeojsonObject RawGeoJson, Data.Layer Layer) { List <Feature> features = new List <Feature>(); string JsonString = GeoJsonObjectToString(RawGeoJson); var parsed = Newtonsoft.Json.Linq.JObject.Parse(JsonString); JArray Val = (JArray)parsed["features"]; if (Val != null) { foreach (JToken jfeature in Val) { Feature geoFeature = new Feature(); geoFeature.friendlyname = Layer.LayerName; geoFeature.guid = Layer.LayerGuId; geoFeature.type = "Feature"; var featureGeometry = jfeature["geometry"]; var featureProperties = jfeature["properties"]; var geometryType = featureGeometry["type"]; var coordinates = featureGeometry["coordinates"]; //Properties geojsonProperties = (Properties)GetProperties(featureProperties); geoFeature.properties = GetProperties(featureProperties); geoFeature.properties.oID = RawGeoJson.oID; if ((string)geometryType == "Point") { var lat = (decimal)coordinates.Values().ElementAt(0); var lon = (decimal)coordinates.Values().ElementAt(1); geoFeature.geometry = new PointGeometry("Point") { coordinates = new[] { lat, lon } }; features.Add(geoFeature); } else if ((string)geometryType == "LineString") { int coordCount = coordinates.Count(); decimal[][] lineStringCoordinates = new decimal[coordCount][]; for (int i = 0; i < coordCount; i++) { var lat = (decimal)coordinates[i].Values().ElementAt(0); var lon = (decimal)coordinates[i].Values().ElementAt(1); lineStringCoordinates[i] = new decimal[] { lat, lon }; } geoFeature.geometry = new LineStringGeometry("LineString") { coordinates = lineStringCoordinates }; features.Add(geoFeature); } else if ((string)geometryType == "Polygon") { var polygonCoordinates = coordinates.First ?? ""; int coordCount = polygonCoordinates.Count(); decimal[][] polyFeatureCoordinates = new decimal[coordCount][]; for (int i = 0; i < coordCount; i++) { var lat = (decimal)polygonCoordinates[i].Values().ElementAt(0); var lon = (decimal)polygonCoordinates[i].Values().ElementAt(1); polyFeatureCoordinates[i] = new decimal[] { lat, lon }; } // donut polygon capability is unimplemented geoFeature.geometry = new PolygonGeometry("Polygon") { coordinates = new decimal[][][] { polyFeatureCoordinates } }; features.Add(geoFeature); } else if ((string)geometryType == "MultiPolygon") { //var polygonCoordinates = coordinates.First ?? ""; //int polygonCount = polygonCoordinates.Count(); //for (int i = 0; i < polygonCount; i++) //{ // var smallPolygon = polygonCoordinates[i].Values(); // int smallCount = smallPolygon.Count(); // decimal[][] polyFeatureCoordinates = new decimal[polygonCount][]; // for (int j = 0; j < smallCount; j++) // { // decimal lat = 0; // decimal lon = 0; // if (j % 2 == 0) // { // lat = (decimal)polygonCoordinates[i].Values().ElementAt(j); // } // else // { // lon = (decimal)polygonCoordinates[i].Values().ElementAt(j); // } // } //} } else if ((string)geometryType == "MultiPoint") { } else if ((string)geometryType == "MultiLineString") { } } } return(features); }
public string Get(string guid) { _logger.LogInformation("-> GeoJson request started: " + guid); DataBase.DBQuery dBQuery = new DataBase.DBQuery(_appConfiguration); List <Data.Layer> layers = new List <Data.Layer>(); // Zero if simple layer, otherwise it's a collection List <string> lt = dBQuery.GetLayersInGroup(guid); if (lt.Count == 0) { layers = dBQuery.GetLayers(new List <string>() { guid }); } else { var layersInGroup = dBQuery.GetLayersInGroup(guid); layers = dBQuery.GetLayers(layersInGroup); } GIS.GJson gJson = new GIS.GJson(); gJson.type = "FeatureCollection"; gJson.processedlayers = new List <GIS.ProcessedLayer>(); gJson.guid = guid; foreach (var layer in layers) { // array of names to create filter when create layer tree in GUI // 19/03/19 --> guid instead //gJson.layernames.Add(layer.LayerName); //gJson.layerguids.Add(layer.LayerGuId); // originally: with empty TABLENAME in MAP_LAYERS table, default SOURCE TABLE has been set // 19/10/07 --> TABLENAME inMAP_LAYERS table is mandatory // 19/12/13 friendlyname bekerült, aktív layer listához //gJson.layerfriendlynames.Add(layer.LayerName); // 20/01/15 --> processedlayer object to encapsulate geojson special properties gJson.processedlayers.Add(new GIS.ProcessedLayer() { layerguid = layer.LayerGuId, layerfriendlyname = layer.LayerName, zoomlimit = layer.ZoomLimit }); string tableName = _gisServices.SetDataTableName(layer); if (layer.LayerType == "point") { List <Data.Point> points = new List <Data.Point>(); List <GIS.Feature> features = new List <GIS.Feature>(); // source table: default name, or defined in map_layers if (string.IsNullOrEmpty(layer.Color)) { points = dBQuery.GetSinglePointsDETAILED(tableName, layer.LayerGuId.ToString()); features = _gisServices.ToGISGeojsonFeatures(points, layer); } else { points = dBQuery.GetSinglePointsSIMPLIFIED(tableName, layer.LayerGuId.ToString()); features = _gisServices.ToGISGeojsonFeaturesSimplePoint(points, layer); } gJson.features.AddRange(features); } else if (layer.LayerType == "geojson") { List <GIS.Feature> features = new List <GIS.Feature>(); Data.DBGeojsonObject geojson = dBQuery.GetPlainGeojson("MAP_LAYER_GEOJSONS", layer.LayerGuId.ToString()); features = _gisServices.ToGISGeojsonFeatures(geojson, layer); gJson.features.AddRange(features); } else if (layer.LayerType == "line") { List <Data.Line> lines = new List <Data.Line>(); List <GIS.Feature> features = new List <GIS.Feature>(); if (string.IsNullOrEmpty(layer.Color)) { lines = dBQuery.GetSingleLinesDETAILED(tableName, layer.LayerGuId.ToString()); features = _gisServices.ToGISGeojsonFeatures(lines, layer); } else { lines = dBQuery.GetSingleLinesSIMPLIFIED(tableName, layer.LayerGuId.ToString()); features = _gisServices.ToGISGeojsonFeaturesSimpleLine(lines, layer); } gJson.features.AddRange(features); } } return(JsonConvert.SerializeObject(gJson).ToString());// gJson.ToString(); }