public override string QueryMapFeatures(RuntimeMap rtMap, int maxFeatures, string wkt, bool persist, string selectionVariant, QueryMapOptions extraOptions) { string runtimeMapName = rtMap.Name; MgRenderingService rs = this.Connection.CreateService(MgServiceType.RenderingService) as MgRenderingService; MgResourceService res = this.Connection.CreateService(MgServiceType.ResourceService) as MgResourceService; MgMap map = new MgMap(); string mapname = runtimeMapName.IndexOf(":") > 0 ? new ResourceIdentifier(runtimeMapName).Path : runtimeMapName; map.Open(res, mapname); MgWktReaderWriter r = new MgWktReaderWriter(); MgStringCollection layerNames = null; string featureFilter = ""; int layerAttributeFilter = 0; int op = MgFeatureSpatialOperations.Intersects; if (selectionVariant == "TOUCHES") op = MgFeatureSpatialOperations.Touches; else if (selectionVariant == "INTERSECTS") op = MgFeatureSpatialOperations.Intersects; else if (selectionVariant == "WITHIN") op = MgFeatureSpatialOperations.Within; else if (selectionVariant == "ENVELOPEINTERSECTS") op = MgFeatureSpatialOperations.EnvelopeIntersects; else throw new ArgumentException("Unknown or unsupported selection variant: " + selectionVariant); if (extraOptions != null) { if (!string.IsNullOrEmpty(extraOptions.FeatureFilter)) featureFilter = extraOptions.FeatureFilter; if (extraOptions.LayerNames != null && extraOptions.LayerNames.Length > 0) { layerNames = new MgStringCollection(); foreach (var name in extraOptions.LayerNames) layerNames.Add(name); } layerAttributeFilter = (int)extraOptions.LayerAttributeFilter; } MgFeatureInformation info = rs.QueryFeatures(map, layerNames, r.Read(wkt), op, featureFilter, maxFeatures, layerAttributeFilter); string xml = ""; GetByteReaderMethod fetch = () => { return info.ToXml(); }; using (var sr = new StreamReader(new MgReadOnlyStream(fetch))) { xml = sr.ReadToEnd(); } //We only want the FeatureSet element var doc = new System.Xml.XmlDocument(); doc.LoadXml(xml); xml = doc.DocumentElement["FeatureSet"].OuterXml; MgSelection sel = new MgSelection(map, xml); sel.Save(res, mapname); LogMethodCall("QueryMapFeatures", true, runtimeMapName, wkt, persist, selectionVariant, extraOptions == null ? "null" : "QueryMapOptions"); return xml; }
public IActionResult SelectFeature(SelectInputModel input) { MgSiteConnection conn = CreateConnection(input); MgMap map = new MgMap(conn); map.Open(input.MapName); MgLayerCollection layers = map.GetLayers(); int lidx = layers.IndexOf("Parcels"); if (lidx < 0) { throw new Exception("Layer not found on map: Parcels"); } MgLayerBase layer = layers.GetItem(lidx); MgClassDefinition clsDef = layer.GetClassDefinition(); MgPropertyDefinitionCollection props = clsDef.GetProperties(); MgPropertyDefinition idProp = props.GetItem(0); string idPropName = idProp.Name; MgFeatureQueryOptions query = new MgFeatureQueryOptions(); query.SetFilter(idPropName + " = " + input.id); MgFeatureReader reader = layer.SelectFeatures(query); MgSelection selection = new MgSelection(map, ""); MgResourceService resSvc = (MgResourceService)conn.CreateService(MgServiceType.ResourceService); string result = ""; try { selection.Open(resSvc, input.MapName); selection.FromXml(""); //Clear existing selection.AddFeatures(layer, reader, 0); result = selection.ToXml(); selection.Save(resSvc); } finally { reader.Close(); } return Content(result, MgMimeType.Xml); }