Select to view content in your preferred language

QueryTask with Polygon not working

3127
4
02-04-2011 09:56 AM
by Anonymous User
Not applicable
Original User: tgorrie

I am unable to pass a polygon and retrieve point features in my Querytask. Returns error "Unable to Complete Operation". What am I missing?

QueryTask rTask = new QueryTask("http://codadmlp2922/ArcGIS/rest/services/OSOW_ND/MapServer/0");
            rTask .ExecuteCompleted += rTask _ExecuteCompleted;
            rTask .Failed += QueryTask_Failed;

            Query query = new ESRI.ArcGIS.Client.Tasks.Query();

            // Specify fields to return from query
            query.OutFields.Add("*");
            query.Geometry = queryPolygon;
            query.ReturnGeometry = true;

            rTask .ExecuteAsync(query);
0 Kudos
4 Replies
by Anonymous User
Not applicable
Original User: ali5110

Hard to tell what is going wrong given that generic error message. What error message/number are you getting with Fiddler?
0 Kudos
ChristopherHill
Deactivated User
How did you create your polygon? verify that the spatial reference of the points inside your polygon match the maps spatial reference. Also make sure you are drawing your polygon in a clockwise direction. Is this a simply polygon? one ring? multiple rings? The Unable to complete operation means that some required parameter is missing or the geometry is bad. I am thinking the polygon could have an issue.

Which version of ArcGIS Server are you using 9.3 or 10.0. if there is a problem with the service then the version will make a difference in tracking down the problem.

Thanks.
0 Kudos
ChristopherHill
Deactivated User
Here is a simple sample that you can use to test your service. You can replace the services in this sample with yours to test that it is working correctly. basically this sample will load up a base map and a feature layer with some points on the screen. when you click the button in the top left of the screen it will grab your current extent of the map and create a polygon out of it. the polygon is then used in a query task to query the same feature layer service you see on the map. When the quey task comepletes it will give you a count of how many items it found on the screen. so the count should be the same as the number items you see on your map.

XAML
<Grid x:Name="LayoutRoot" Background="White">
        <esri:Map x:Name="esriMap">
            <esri:Map.Layers>
                <esri:ArcGISTiledMapServiceLayer Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
                <esri:FeatureLayer Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/MapServer/0" />
            </esri:Map.Layers>
        </esri:Map>

        <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" >
            <Button Content="Query Polygon" Click="Button_Click" />
            <Button Content="Query Envelope" Click="Button_Click_1" />
        </StackPanel>        
    </Grid>


CodeBehind
public partial class MainPage : UserControl
 {
  Query query;
  QueryTask rTask;

  public MainPage()
  {
   InitializeComponent();

   rTask = new QueryTask("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/MapServer/0");
   rTask.ExecuteCompleted += new EventHandler<QueryEventArgs>(rTask_ExecuteCompleted);
   rTask.Failed += new EventHandler<TaskFailedEventArgs>(rTask_Failed);

   query = new ESRI.ArcGIS.Client.Tasks.Query();

   // Specify fields to return from query
   query.OutFields.Add("*");
   query.ReturnGeometry = true;
  }

  private void Button_Click(object sender, RoutedEventArgs e)
  {      
   // Create a polygon of your current extent. Should returns the number
   // of items you see on your screen.
   query.Geometry = CreatePolygonFromMapExtent(); 
      
   rTask.ExecuteAsync(query);
  }

  private void Button_Click_1(object sender, RoutedEventArgs e)
  {
   query.Geometry = esriMap.Extent;

   rTask.ExecuteAsync(query);
  }

  void rTask_ExecuteCompleted(object sender, QueryEventArgs e)
  {
   MessageBox.Show(string.Format("{0} features on screen.",e.FeatureSet.Features.Count));
  }

  Polygon CreatePolygonFromMapExtent()
  {
   Envelope extent = esriMap.Extent;

   // Create a polygon of your current map extent
   PointCollection pc = new PointCollection();
   pc.Add(new MapPoint() { X = extent.XMin, Y = extent.YMax, SpatialReference = esriMap.SpatialReference }); // top left of map screen
   pc.Add(new MapPoint() { X = extent.XMax, Y = extent.YMax, SpatialReference = esriMap.SpatialReference }); // top right of map screen
   pc.Add(new MapPoint() { X = extent.XMax, Y = extent.YMin, SpatialReference = esriMap.SpatialReference }); // bottom right of map screen
   pc.Add(new MapPoint() { X = extent.XMin, Y = extent.YMin, SpatialReference = esriMap.SpatialReference }); // bottom left of map screen
   pc.Add(new MapPoint() { X = extent.XMin, Y = extent.YMax, SpatialReference = esriMap.SpatialReference }); // top left of map screen to close polygon

   Polygon polygon = new Polygon() { SpatialReference = esriMap.SpatialReference };
   polygon.Rings.Add(pc);

   return polygon;
  }

  void rTask_Failed(object sender, TaskFailedEventArgs e)
  {
   MessageBox.Show(e.Error.Message);
  }

  
 }
0 Kudos
by Anonymous User
Not applicable
Original User: tgorrie

My polygon wasn't closed correctly. Thanks for the help, appreciate it.
0 Kudos