| 1 | |
| 2 | == Query Features with a filter == |
| 3 | This page describes how to use the MaestroAPI from the [wiki:maestro MapGuide Maestro] project in your own application. |
| 4 | |
| 5 | This sample shows how to read geometry features from a !FeatureSource using MaestroAPI. |
| 6 | |
| 7 | (C#) |
| 8 | {{{ |
| 9 | using OSGeo.MapGuide.MaestroAPI; |
| 10 | ... |
| 11 | |
| 12 | //Read the setup, either from QueryString, Form, Cookies or hardcoded values |
| 13 | string username = Request.Params("USERNAME") == null ? "Anonymous" : Request.Params("USERNAME"); |
| 14 | string password = Request.Params("PASSWORD") == null ? "" : Request.Params("PASSWORD"); |
| 15 | string layout = Request.Params("LAYOUT") == null ? "Library://MyLayout.WebLayout" : Request.Params("LAYOUT"); |
| 16 | string locale = Request.Params("LOCALE") == null ? "en" : Request.Params("LOCALE"); |
| 17 | string sessionid = Request.Params("SESSION"); |
| 18 | |
| 19 | |
| 20 | //Variables that should be replaced |
| 21 | string serverUrl = "http://localhost/mapguide/mapagent/mapagent.fcgi"; |
| 22 | string featureSourceId = "Library://my data source.FeatureSource"; |
| 23 | string classname = "Default:Test"; |
| 24 | string filter = "ID = 5 AND Y LIKE 'test*'"; |
| 25 | |
| 26 | //Setup connection |
| 27 | Uri host = new Uri(serverUrl); |
| 28 | ServerConnectionI con; |
| 29 | |
| 30 | //Either use session id or explicit credentials |
| 31 | if (string.IsNullOrEmpty(sessionid)) |
| 32 | con = new HttpServerConnection(host, username , password, locale, true); |
| 33 | else |
| 34 | con = new HttpServerConnection(host, sessionid, true); |
| 35 | |
| 36 | //Get the featuresource |
| 37 | FeatureSource fs = con.GetFeatureSource(featureSourceId); |
| 38 | FeatureSourceDescription desc = fs.DescribeSource(classname); |
| 39 | |
| 40 | //Find the geometry column |
| 41 | FeatureSetColumn geometryColumn = null; |
| 42 | foreach(FeatureSetColumn col in desc.Schemas[0].Columns) |
| 43 | if (col.Type == MaestroAPI.Utility.GeometryType) |
| 44 | geometryColumn = col; //TODO: If there are more than one? |
| 45 | |
| 46 | //Add any extra columns that you need |
| 47 | string[] columns = new string[] { geometryColumn.Name }; |
| 48 | |
| 49 | //Prepare a list |
| 50 | List<Topology.IO.Geometry.IGeometry> features = new List<Topology.IO.Geometry.IGeometry>(); |
| 51 | |
| 52 | //Execute the query |
| 53 | using (FeatureSetReader rd = fs.SelectFeatures(classname, filter, columns)) |
| 54 | while (rd.Read()) |
| 55 | features.Add(rd.Row[geometryColumn.Name] as Topology.IO.Geometry.IGeometry); |
| 56 | |
| 57 | //The list "features" is now a list of all geometry found by applying the filter. |
| 58 | }}} |
| 59 | |
| 60 | |