Original: Compound multi-criteria spatial query using ArcGIS API for Silverlight
These two days to help the online acquaintance of a brother made an example of a query, more or less summed up, here and you share.
Why is compounding multiple conditions? Because of the spatial query sometimes we query the conditions can be very complex, such as requirements for a certain attribute of a feature is greater than how much, and less than how much, and not equal to how much and so on. There is no explanation for compound queries in the examples given on the website. However, after viewing the API, you will find a very important sentence:
A WHERE clause for the query. Any legal SQL WHERE clause operating on the ' fields ' are allowed, for example:where=pop2000 > 350000.
That is, when a spatial query is queried, the where property is actually a string of SQL where query, so we write the query condition so that we can imitate the SQL where query statement. Refer to the example below for details.
First, the front desk part
The interface is designed as follows:
The foreground XAML code is as follows:
MainPage.xaml
View Code
Description
The foreground page also adds features to the web online editing feature, and if the feature comes from an ARCSDE database, you can also synchronize the database with the new to the background.
Second, backstage code
Background is the implementation of the query, the query through Querytask specify the layer of the query and the task of declaring the query, Querytask through the Executeasync method to receive the query parameter, and then start the query, through the Querycompleted event to return the results of the query.
Where query specifies the criteria and query-related parameters for the queries, a list of common parameters for query is listed below:
Parameter list |
Parameter description |
Geometry |
Specifies the spatial filter for the query, and can describe the spatial relationship of the query through Query.spatialrelationship, with valid geometry types: MapPoint, Polyline, Polygon, Envelope, or MultiPoint. |
Outfields |
The Filtered field collection is the attribute that the feature contains |
Returngeometry |
Returns the geometry feature of the query and, if all fields are returned, forces the setting of Returngeometry to True |
Source |
If you are querying a dynamic layer, this sets the layer source |
Spatialrelationship |
describes spatial relationships between features queried by spatial queries and input geometric features |
Text |
Fields displayed in the corresponding feature service layer |
Where |
A query statement that is represented by a valid SQL statement |
Let's look at how the query is implemented in detail:
First declare the querytask and the target layer of the query
// the task used for the query New Querytask (); // target layer of the query Featurelayer Featurelayer;
//Declare a draw to draw the area of the query and make a spatial query
Draw Mydraw;
Assigning and instantiating in constructors
PublicMainPage () {InitializeComponent (); Featurelayer= Map1. layers["Riversourcelayer"] asFeaturelayer; editorwidget.loaded+=NewRoutedeventhandler (editorwidget_loaded); Querytask.url=Featurelayer. URL; Querytask.executecompleted+=NewEventhandler<queryeventargs>(querytask_executecompleted); Querytask.failed+=NewEventhandler<taskfailedeventargs>(querytask_failed);
Mydraw = new Draw (MAP1);
Mydraw.drawmode = Drawmode.polygon;
mydraw.isenabled = false;
Mydraw.fillsymbol = new Simplefillsymbol ()
{
Borderbrush=new SolidColorBrush (Colors.black),
Borderthickness=3,
Fill=new SolidColorBrush (colors.red),
};
Mydraw.drawcomplete + = new eventhandler<draweventargs> (mydraw_drawcomplete);
= attributestring.graphicattributesennamestring; }
Customize two query methods:
Startquerybysql (String sqlString): Queries by the WHERE statement of SQL.
Private voidStartquerybysql (stringsqlString) { if(Expressiontextbox.text = ="") return; Query Query=NewQuery (); Query. Returngeometry=true; Query. Outfields.addrange (attributestring.graphicattributesennamestring); Query. Where=sqlString; Query. Outspatialreference=Map1. Spatialreference; Querytask.executeasync (query); }
Startquerybyspatial (ESRI. ArcGIS.Client.Geometry.Geometry Geometry): Query by drawing spatial graph
Private void startquerybyspatial (ESRI. ArcGIS.Client.Geometry.Geometry Geometry) { new Query (); true ; Query. Outfields.addrange (attributestring.graphicattributesennamestring); = geometry; = Map1. spatialreference; Querytask.executeasync (query); }
The next step is to click on the different buttons to query, and to process the results of the query, such as displaying the results of the query on the map.
Note: This article because the query is a point feature, so in the query completed event completion function only the symbol display operation, if the result of the query is the polygon features also need to deal with the opposite, the reader can add themselves.
Here is the code for the query to finish processing the result:
Private voidQuerytask_executecompleted (Objectsender, Queryeventargs e) {Graphicslayer Graphicslayer= Map1. layers["Queryresultlayer"] asGraphicslayer; Graphicslayer.cleargraphics (); if(E.featureset.features.count >0) { foreach(Graphic resultfeatureinche.featureset.features) {Resultfeature.symbol=NewSimplemarkersymbol () {Color=NewSolidColorBrush (colors.red), Size= -, Style=SimpleMarkerSymbol.SimpleMarkerStyle.Circle,}; GRAPHICSLAYER.GRAPHICS.ADD (resultfeature); } } Else{MessageBox.Show ("no query to target features! "); } }
Spatial query submits a spatial query request in the draw's competed event:
Private void mydraw_drawcomplete (object sender, Draweventargs e) { startquerybyspatial (E. Geometry);
mydraw.isenabled = false;
}
The other part of the code is the input of the query control, as well as some of the program bug processing (still some bugs are not fixed, but does not affect the overall function of the query, the reader can modify their own perfect).
The complete code is given below: Coding-behind
MainPage.csAttributeString.cs
The final effect (the result is indicated by a red dot, click on the feature to see its list of attributes):
1.Where query:
A, query condition one: source_id > 1 and source_id < and source_id! = 14
Query Result:
B, query Conditions II: source_id > 1 and source_emissions are NOT null and Source_emissions < 200
Query Result:
C. Spatial queries
Input Query polygon:
To query the features within a polygon:
Source and test data download: "Download Code"
Data usage Note: The data in the compressed file is an XML file exported by ArcMap, and when used, creates a new geodatabase in ArcMap and then right-imports and selects the XML file to give the data of the cost example. It is recommended that the service for this data be published as a feature service, which supports web online editing.
(All rights reserved, reproduced please indicate the source)