I come from the Java world and go straight to C# and ArcGis 10 (I have to make a simple task for the university). So this means I don't really know much of C# but I can live with it and don't know ArcGis API at all. All I have is an example from the classes. It is about how to filter the cities by coordinates X and Y and a Buffer. So we have layers City(Point) and States(Polygon). My task is to make other functionality, and by functionality I mean method, that takes the name of 1 state and get all the cities from state's area.
In the example code that we have some ISpatialFilter that we use with the buffer like this:
public List<City> SearchCities(...)
{
IFeatureClass citiesClass = MyHelper.GetLayer(Layers.CITIES);
ISpatialFilter spatialFilter = getSpatialFilter(centerX, centerY, distance);
IFeatureCursor citiesCursor = citiesClass.Search((QueryFilter)spatialFilter, true);
.....
.....
}
private static ISpatialFilter getSpatialFilter(double centerX, double centerY, double distance)
{
IPoint center = getCenter(centerX, centerY);
IGeometry buffer = getBuffer(distance, center);
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = buffer;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
return spatialFilter;
}
private static IGeometry getBuffer(double distance, IPoint center)
{
return (center as ITopologicalOperator).Buffer(distance);
}
private static IPoint getCenter(double centerX, double centerY)
{
IPoint center = new Point();
center.PutCoords(centerX, centerY);
return center;
}
So I decided to reuse some of the code and only change the way the data is filtered. So, I came up with something like this:
private IQueryFilter GetQueryFilter(string stateAbbr)
{
IGeometry stateGeometry = getGeometry(stateAbbr);
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = stateGeometry;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
return (QueryFilter)spatialFilter;
}
// TODO fix it
private IGeometry getGeometry(string stateAbbr)
{
IFeature state = GetStateByAbbr(stateAbbr);
return (state as ITopologicalOperator).Boundary;
}
private IFeature GetStateByAbbr(string stateAbbr)
{
IFeatureClass statesClass = MyHelper.GetLayer(Layers.STATES);
IQueryFilter queryFilter = GetStateQueryFilter(stateAbbr);
IFeatureCursor stateCursor = statesClass.Search(queryFilter, false);
return stateCursor.NextFeature();
}
private IQueryFilter GetStateQueryFilter(string stateAbbr)
{
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.WhereClause = StatesWhereBuilder(stateAbbr);
return (QueryFilter)spatialFilter;
}
private string StatesWhereBuilder(string stateAbbr)
{
return (MyHelper.isEmpty(stateAbbr)) ? "" : MyHelper.CreateEqualStatement(StateConst.STATE_ABBR, stateAbbr, true);
}
I know it is probably very amusing for some of you, but this is all I came up with :] The problem is that I get NullReferenceException after calling (state as ITopologicalOperator).Boundary but the debug shows that state is not null. So after the as something happens. I might be wrong with the ITopologicalOperator. If so, what should I use instead? Thanks in advance.
EDIT: Please tell me what do you think about this question (how it is exposed, etc.)
I changed the GetGeometry() method to:
private IGeometry getGeometry(string stateAbbr)
{
IFeature state = GetStateByAbbr(stateAbbr);
IPolygon statePolygon = state.Shape as IPolygon;
return (statePolygon as ITopologicalOperator).Boundary;
}
and now I don't have NullPointerException, but I don't get any result.