ThinkGeo.com    |     Documentation    |     Premium Support

Query Layer Shapes contained within Shape from another layer

I have 2 layers on a given map.  When a user click on the map, I am getting the shape from layer1 they are selecting, I then need to query layer2 the shapes that are contained within the shape from layer1.  Got any sample code for that?


Thanx,


Rob



 


Robert,
Welcome to MapSuite discussion forum. Any questions please feel free to post here.


 public partial class DisplayASimpleMap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));
                Map1.CurrentExtent = new RectangleShape(-125, 72, 50, -46);
                Map1.MapUnit = GeographyUnit.DecimalDegree;

                ShapeFileFeatureLayer layer1 = new ShapeFileFeatureLayer("");
                ShapeFileFeatureLayer layer2 = new ShapeFileFeatureLayer("");

                Map1.StaticOverlay.Layers.Add("layer1",layer1);
                Map1.StaticOverlay.Layers.Add("layer2",layer2);

                Map1.Click += new EventHandler<MapClickedEventArgs>(Map1_Click);
            }
        }

        void Map1_Click(object sender, MapClickedEventArgs e)
        {
            ShapeFileFeatureLayer layer1 = Map1.StaticOverlay.Layers["layer1"] as ShapeFileFeatureLayer;
            ShapeFileFeatureLayer layer2 = Map1.StaticOverlay.Layers["layer2"] as ShapeFileFeatureLayer;
            
            // To get the shape from layer1
            Collection<Feature> features = layer1.FeatureSource.GetFeaturesNearestTo(e.Position, GeographyUnit.DecimalDegree, 1, ReturningColumnsType.NoColumns);
            PolygonShape polygon = features[0].GetShape() as PolygonShape;

            // Get the Shapes that are contained within the selected shape
            RectangleShape rec = features[0].GetBoundingBox();
            Collection<Feature> featuresWithin = layer2.FeatureSource.GetFeaturesInsideBoundingBox(rec, ReturningColumnsType.NoColumns);
            foreach (Feature item in featuresWithin)
            {
                if (polygon.Contains(item))
                { 
                    // The feature is in the selected area.
                }
            }
        }


Here is the sample code that is helpful.
Johnny