Select to view content in your preferred language

Determining if a coordinate is inside a  shapefile polygon

1411
4
03-07-2013 07:07 AM
Labels (1)
EzraSidran
Emerging Contributor
I'm loading various shapefiles that contain polygons. Curiously, the polygons appear in different colors every time I run the app. This isn't important, but every time I run it, they appear in green one time, blue the next, etc.

Anyway, I now need to know if a specific location (lat/long) is 'covered' or 'inside' one of these polygons.

So, specifically, if I load a shapefile of the 'Harbor Bay' of San Diego, I need to determine if there is water at a specific lat/long.

How can I do this?

There probably is a method. Anybody know what it is?

Thanks in advance!
0 Kudos
4 Replies
MichaelBranscomb
Esri Frequent Contributor
Hi,

You've actually got several options here...

#1. QueryTask:- Probably the best option... set the search geometry via the Query Class (http://resources.arcgis.com/en/help/runtime-wpf/apiref/index.html?ESRI.ArcGIS.Client~ESRI.ArcGIS.Cli...).

#2. GeometryService task:- The GeometryService is typically very quick/efficient but this route would require first retrieving the polygon geometries to submit as graphics which may incur an overhead (you'd need to submit a query first). Take a look at the Relation operation:
API ref: http://resources.arcgis.com/en/help/runtime-wpf/apiref/index.html?ESRI.ArcGIS.Client~ESRI.ArcGIS.Cli...
Sample: http://resources.arcgis.com/en/help/runtime-wpf/samples/index.html#/Relation/02q200000056000000/

#3. Geoprocessor task:- This would require creating a model/script and packaging as a GPK but you could craft the GP tool to give you the exact response you want (e.g. just a boolean yes/no).


Cheers

Mike
0 Kudos
EzraSidran
Emerging Contributor
Hi,

You've actually got several options here...

#1. QueryTask:- Probably the best option... set the search geometry via the Query Class (http://resources.arcgis.com/en/help/runtime-wpf/apiref/index.html?ESRI.ArcGIS.Client~ESRI.ArcGIS.Cli...).

#2. GeometryService task:- The GeometryService is typically very quick/efficient but this route would require first retrieving the polygon geometries to submit as graphics which may incur an overhead (you'd need to submit a query first). Take a look at the Relation operation:
API ref: http://resources.arcgis.com/en/help/runtime-wpf/apiref/index.html?ESRI.ArcGIS.Client~ESRI.ArcGIS.Cli...
Sample: http://resources.arcgis.com/en/help/runtime-wpf/samples/index.html#/Relation/02q200000056000000/

#3. Geoprocessor task:- This would require creating a model/script and packaging as a GPK but you could craft the GP tool to give you the exact response you want (e.g. just a boolean yes/no).


Cheers

Mike



Mike: many thanks for pointing out QueryTask. I'm completely inexperienced with ArcGIS and learning as I go. By any chance, is there any sort of example for using QueryTask that you could point me to?

We are loading the shapefile poly just fine. I then need to do something like this:

for(double y = SouthLongitude; y < NorthLongitude; y = y++)
     for(double x = WestLatitude; x < EastLatitude; x = x++)
        if (QueryTask == true)
             TerrainMap[x , y ] = TerrainType;
0 Kudos
MichaelBranscomb
Esri Frequent Contributor
Hi,

Here's a QueryTask sample: http://resources.arcgis.com/en/help/runtime-wpf/samples/index.html#/Spatial_Query_Online/02q2000000m.... It sounds like you're using the DynamicLayers capability to add Shapefiles - if this is the case you'll need to set the Source property on the Query object (http://resources.arcgis.com/en/help/runtime-wpf/apiref/index.html?ESRI.ArcGIS.Client~ESRI.ArcGIS.Cli...).

Also - you mentioned that the Shapefiles are added in different colours each time? - you can set the renderer on the layer so the colour is up to you... at the current release unfortunately you'll need to make a manual request to get the geometry type, but for the next release we've extended the GetAllDetails method to support DynnamicLayers.

e.g.

/* 
                                 * Apply a renderer for vector layers.
                                 * Note: It is always necessary to provide a renderer when the layer being added (represented by a DynamicLayerInfo) is part of a new 
                                 * DynamicLayerInfoCollection as opposed to using the CreateDynamicLayerInfosFromLayerInfos() method which creates a DynamicLayerInfoCollection 
                                 * containing the existing layers in the map service.
                                */

                                // Create a new LayerDrawingOptions object to hold the renderer information.
                                var layerDrawOpt = new LayerDrawingOptions()
                                {
                                    // Match up the LayerID to the ID of the layer within the service.
                                    LayerID = counter,
                                };

                                // We need to determine the geometry type of the new feature class.  
                                // To do this, we will submit a request to the ..\MapServer\dynamicLayer?.. endpoint which will return the service level metadata,
                                // Allowing us to identify the geometry type and create an appropriate renderer.

                                // Create a new WebClient instance to make the request and download the response.
                                WebClient webClient = new WebClient();

                                // Register an asynchronous handler in which to create the renderers and apply the to the dynamic map service layer.
                                webClient.DownloadDataCompleted += (client, downloadDataEventArgs) => 
                                {

                                    // Read the JSON response as XML
                                    XmlReader reader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(downloadDataEventArgs.Result, new XmlDictionaryReaderQuotas());

                                    // Get the root XML element
                                    XElement root = XElement.Load(reader);

                                    // Query for the "geometryType" element
                                    XElement geometryType = root.XPathSelectElement("//geometryType");

                                    // Create the render based on the geometry type
                                    switch (geometryType.Value)
                                    {
                                        case "esriGeometryPoint":
                                            layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleMarkerSymbol() { Color = new SolidColorBrush(GetRandomColor()), Size=8 } };
                                            break;
                                        case "esriGeometryPolyline":
                                            layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleLineSymbol() { Color = new SolidColorBrush(GetRandomColor()) } };
                                            break;
                                        case "esriGeometryPolygon":
                                            layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleFillSymbol() { Fill = new SolidColorBrush(GetRandomColor()), BorderBrush = new SolidColorBrush(GetRandomColor()) } };
                                            break;
                                    }

                                    // Set the LayerDrawingOptions property on the local dynamic map service layer (the LayerID property ties this to the DynamicLayerInfo object).
                                    layerDrawingOptionsCollection.Add(layerDrawOpt);

                                    // Update the layer drawing options property on the dynamic map service layer.
                                    arcGisLocalDynamicMapServiceLayer.LayerDrawingOptions = layerDrawingOptionsCollection;

                                    // Need to refresh the layer after the renderer(s) have been applied.
                                    arcGisLocalDynamicMapServiceLayer.Refresh();

                                };

                                // Make the request for the service metadata
                                // e.g. http://127.0.0.1:<PORT>/arcgis/rest/services/<MPK_NAME>/MapServer/dynamicLayer?layer={"id":0,"source":{"type":"dataLayer","dataSource":{"type":"table","workspaceId":"MyWorkspace","dataSourceName":"MyFeatureClassName"}}}
                                webClient.DownloadDataAsync(new Uri(arcGisLocalDynamicMapServiceLayer.Url 
                                    + "/dynamicLayer?layer={'id':" + counter.ToString() + ","
                                    + "'source':{'type':'dataLayer','dataSource':{"
                                    + "'type':'table',"
                                    + "'workspaceId':'"+ workspaceInfo.Id + "',"
                                    + "'dataSourceName':'" + fileName + "'"
                                    + "}}}"));


Cheers

Mike
0 Kudos
EzraSidran
Emerging Contributor
Mike:

Thank you very much!

I'm sure I'm going to get stuck, so be prepared for some very stupid questions.

- Ezra
0 Kudos