Background:
- Add-In for ArcGIS 10 Desktop via ArcObjects for .NET
- The below code returns ALL features for the layer when the map is specified with any Geographic Coordinate System.
- However, the below code works fine when the map is specified with a Projected Coordinate System. That is, returning a subset of the features within the radius.
//
IPoint viewPoint = new PointClass() as IPoint;
// lon, lat here are WGS 84
viewPoint.PutCoords(lon, lat);
// project input point to coordinate system of map
ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironmentClass();
IGeographicCoordinateSystem pcs =
srFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
pcs.SetFalseOriginAndUnits(0.0, 0.0, 1000);
ISpatialReference srWgs84;
srWgs84 = pcs;
// if not WGS84 project it (omitting subroutine here..)
if (!CompareSpatialRefs(map.SpatialReference, srWgs84))
{
//Geometry Interface to do actual project
IGeometry geometry;
geometry = viewerPoint;
geometry.SpatialReference = srWgs84;
geometry.Project(map.SpatialReference);
viewPoint = geometry as IPoint;
}
else
viewPoint.SpatialReference = srWgs84;
esriUnits distUnits = map.DistanceUnits;
// convert meters to map dist units (again, omitting subroutine..)
double bufferDist = ConvertToMapUnits(_myQueryRadiusInMeters, distUnits);
// using ITopoOp to generate a buffer
ITopologicalOperator topoOperator = (ITopologicalOperator)viewPoint;
IGeometry viewBuffer = topoOperator.Buffer(bufferDist);
IFeatureClass featureClass = myFeatureLayerInQuestion.FeatureClass;
IFeatureCursor featCursor = PerformSpatialQuery(featureClass, viewBuffer,
esriSpatialRelEnum.esriSpatialRelIntersects, "");
...
// code straight from the ArcGIS Snippet Finder
public IFeatureCursor PerformSpatialQuery(IFeatureClass featureClass, IGeometry searchGeometry, esriSpatialRelEnum spatialRelation, string whereClause)
{
if (featureClass == null)
return null;
// create a spatial query filter
ISpatialFilter spatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass();
// specify the geometry to query with
spatialFilter.Geometry = searchGeometry;
// specify what the geometry field is called on the Feature Class
// that we will be querying against
System.String nameOfShapeField = featureClass.ShapeFieldName;
spatialFilter.GeometryField = nameOfShapeField;
// specify the type of spatial operation to use
spatialFilter.SpatialRel = spatialRelation;
// create the where statement
spatialFilter.WhereClause = whereClause;
// perform the query and use a cursor to hold the results
IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
queryFilter = (ESRI.ArcGIS.Geodatabase.IQueryFilter)spatialFilter;
IFeatureCursor featureCursor = featureClass.Search(queryFilter, false);
return featureCursor;
}
At this point, I iterate over the features in the featureCursor, calling featCursor.NextFeature(). Again any Projected Coordinate System, good. It appears for any Geographic Coordinate System, the cursor contains all the features for the layer.
Many thanks for any help!
EDIT: I believe I've figured out the issue. Please see my posted answer below.
