[mapguide-users] Fetching features within rectangle; layer SRS differs from map SRS

Patrik Johansson mapguide at morkl.se
Thu Dec 8 07:00:08 EST 2011


Hi!

I want to be able to input a point in a map and access all features
within a 500 m x 500 m square around it.

This is the code i currently use (C#):

/****** snippety snip begin ******/
MgCoordinate startCoordMap = geomFactory.CreateCoordinateXY(_startX,
_startY); // center of the square

MgCoordinateSystem mapSRS = coordSysFactory.Create(map.GetMapSRS());

double yoff = mapSRS.ConvertMetersToCoordinateSystemUnits(250.0); //
height and width of square
double xoff = mapSRS.ConvertMetersToCoordinateSystemUnits(250.0);

MgCoordinateCollection extentscoords = new MgCoordinateCollection();
extentscoords.Add(geomFactory.CreateCoordinateXY(startCoordMap.GetX()
- xoff, startCoordMap.GetY() - xoff));
extentscoords.Add(geomFactory.CreateCoordinateXY(startCoordMap.GetX()
- xoff, startCoordMap.GetY() + xoff));
extentscoords.Add(geomFactory.CreateCoordinateXY(startCoordMap.GetX()
+ xoff, startCoordMap.GetY() + xoff));
extentscoords.Add(geomFactory.CreateCoordinateXY(startCoordMap.GetX()
+ xoff, startCoordMap.GetY() - xoff));
MgLinearRing lr = geomFactory.CreateLinearRing(extentscoords);
MgPolygon extentsPolygonMap = geomFactory.CreatePolygon(lr, null); //
square to get features within

MgFeatureQueryOptions opts = new MgFeatureQueryOptions();
MgLayerCollection layers = map.GetLayers();
foreach (MgLayer layer in layers)
{
  string fcn = layer.GetFeatureClassName();
  string fsid = layer.GetFeatureSourceId();
  MgResourceIdentifier rid = new MgResourceIdentifier(fsid);
  opts.SetFilter(layer.GetFilter());
  opts.SetSpatialFilter(layer.GetFeatureGeometryName(),
extentsPolygonMap, MgFeatureSpatialOperations.Intersects);
  MgFeatureReader fr = featureService.SelectFeatures(rid, fcn, opts);
  while (fr.ReadNext()) {
    // process features
  }
  fr.Close();
}
/****** snippety snip end ******/

It works as expected when the map and all layers have the same
coordinate system, but when the layer coordinate system differs from
the map coordinate system no features are found.

If I transform extentsPolygonMap to the coordinate system used by the
features (hard coded), it works fine but I have not been able to
figure out a way to find out what coordinate system a layer or feature
uses.

TL;DR:
I guess what i really want to know is this: Is there a simple way to
determine what coordinate system the features of a MgLayer use? Or at
least what coordinate system a MgFeature uses?

Best regards,
Patrik Johansson


More information about the mapguide-users mailing list