ThinkGeo.com    |     Documentation    |     Premium Support

Select polygons with a TrackShape

Hello,


I have several questions based on a version 2 demo I created which I am now converting to version 3.  Version 3 seems to have a completely different API and this is rather confusing.


In the version 2 sample, SelectedInfoApp, I could draw a TrackShape and the underlying countries (shapes/polygons) would be highlighted.  I am trying to do this in version 3, but can't quite figure it out.  The SpatialQueryAVector sample helps, but the rectangle is predefined in the code.  How do I use a TrackShape that was created on the client-side to select the shapes in the underlying shape file?  I have it working one way, but it seems awkward and I am not sure if it is the best way.  I haven't found any other sample that shows TrackShapes accessing the underlying shape file that is displayed.


Also, how would I make the TrackShape disappear (after the postback), but leaving the countries in the shape file highlighted?


In the version 2 sample, I could also select whether the TrackShape caused selected countries to be added to the selected set, or removed from the selected set, or just start a new set.  Can this be done in version 3?


The layer switcher is cool, but doesn't seem to see that I have more than one static layer.  I will have serveral static layers and would like to be able to individually turn them on or off.


Thanks in advance for any help.  I think I've asked a lot here.


Tom



Tom, 
  
 Help is on the way.  You have a few questions wrangled up in here so I will sort them out.  One thing to remember is that this is a Beta and that we missed some functionality like highlighting.  We do have a way to do it but it is not as easy as the 2.0 version.  This is due to some API purity issues however you are not the first to have an issue with this so I am sure very soon we will add a new API to make this really easy.  Let me put together some sample code and get back with you soon.   
  
 I think we should also make a sample for this in the “How Do I Samples” as this comes up fairly often.  By the way do you find the “How Do I Samples” helpful?  We appreciate your feedback. 
  
 David 
 

David,


Thanks very much.  I'm looking forward to seeing your code.


Overall, the sample are pretty helpful.  If you create any more, I think it would be good to have one that shows the same functionality as does the SelectedInfoApp sample from version 2.  The version 2 samples were very good and allowed me to put together a good demo for our client.  I'm currently also looking at the version 3 samples Get All the Features, Get Column Data, and Get Data When User Clicks samples to help me retrieve data from selected areas of the map.  The samples are helpful.


Tom






 



 


Tom,


Below is the sample.  What I did was to replace two methods on the sample: MapShapes\FinishTackShapeEvent.  If you past the two methods below over the existing methods then it will run correctly and do what you want.  I have also highlighted the main code I changed.  To answer your questions..



        

  1.     

    When track shapes are finished  drawing they will raise an event on the client side called  FinsihedTrackShape.  On the client side you need an asp.net button  that goes server side.  In the server side code you need something  like Map1.Mode = ModeType.TrackPolygon.  When the code returns  client side it will be in a track polygon mode.  The sample I  modified shows this.


        

    protected void  buttonDrawPolygon_Click(object sender, ImageClickEventArgs e)

        {

             Map1.Mode =  ModeType.TrackPolygon;

        }


        




        

  1.     

    To make the track shape disappear  you need to clear it from the Map1.EditLayers.Features.Clear. What  happens is that when you draw track shapes on the client side we add  them to this Map1.EditLayers which is an in memory layer.  You can  add/remove features and it will reflect that on the client side.   This is kind of the link the client has with the server.

         


        


  2.     

  3.     

    As far as the selected set goes  there inst a version of this.  If you want to add to the selected  set you can loop through the items to add and then check if the  current list contains the value.  I do this in the sample with code  like.. If(!highlightLayer.Features.ContainsKey(feature.Id) then add  the item to the feature.  If you want a new set you can simple call  highlightLayer.Features.Clear()

         


        


  4.     

  5.     

    On the Layer Switcher currently we  dont support many layers.  We only support Static and Dynamic  layers.  We will be enhancing this in the future. We will have a  sample on how to build your own switcher and specify whatever you  want.  The current switcher has some limitation.


        



Thanks again, as we implemented the sample we saw some opportunities to make some API modifications to make this easier.



        protected void Page_Load(object sender, EventArgs e)

        {

            if (!Page.IsPostBack)

            {

                Map1.BackgroundFillBrush = new GeoSolidBrush(GeoColor.FromHtml("#B3C6D4"));

                Map1.CurrentExtent = new RectangleShape(-131.22, 55.05, -54.03, 16.91);

                Map1.MapUnit = GeographyUnit.DecimalDegree;



                // The following two lines of code enable the client and server caching.

                // If you enable these features it will greatly increase the scalability of your

                // mapping application however there some side effects that may be counter intuitive.

                // Please read the white paper on web caching or the documentation regarding these methods.

                // Map1.ClientCache.CacheId = "WorldLayer";

                // Map1.ServerCache.CacheDirectory = MapPath("~/ImageCache/" + Request.Path);



                ShapeFileLayer worldLayer = new ShapeFileLayer(MapPath("~/SampleData/world/cntry02.shp"));

                worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.GetSimpleAreaStyle(GeoColor.FromArgb(255, 243, 239, 228), GeoColor.FromArgb(255, 218, 193, 163), 1);

                worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;



                ShapeFileLayer citiesLayer = new ShapeFileLayer(MapPath("~/SampleData/USA/cities_a.shp"));

                citiesLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.City3;

                citiesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

                citiesLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = new TextStyle("AREANAME", new GeoFont("Verdana", 9), new GeoSolidBrush(GeoColor.StandardColors.Black));

                citiesLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.HaloPen = new GeoPen(GeoColor.StandardColors.White, 2);



                InMemoryLayer highlightLayer = new InMemoryLayer();

                highlightLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = new AreaStyle(new GeoSolidBrush(GeoColor.FromArgb(200, GeoColor.SimpleColors.PastelRed)));

                highlightLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.OutlinePen.Color = GeoColor.StandardColors.Red;

                highlightLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;



                Map1.StaticLayers.Add("WorldLayer", worldLayer);

                Map1.StaticLayers.Add("CitiesLayer", citiesLayer);

                Map1.DynamicLayers.Add("HighlightLayer", highlightLayer);

            }

        }



        protected void Map1_FinishedTrackShape(object sender, EventArgs e)

        {

            InMemoryLayer highlightLayer = (InMemoryLayer)Map1.DynamicLayers["HighlightLayer"];

            VectorLayer worldLayer = (VectorLayer) Map1.StaticLayers["WorldLayer"];



            highlightLayer.Features.Clear();



            //Loop though all the track shapes and highlight the items that intersect it.            

            foreach (string key in Map1.EditLayer.Features.Keys)

            {

                Feature trackShape = Map1.EditLayer.Features[key];

                Collection<Feature> features = worldLayer.QueryTools.GetFeaturesIntersecting(trackShape, new string[] { });



                //Loop though all the spatial query results and add them to the highlight layer

                foreach (Feature feature in features)

                {

                    if(!highlightLayer.Features.ContainsKey(feature.Id))

                    {

                        highlightLayer.Features.Add(feature.Id, feature);

                    }

                }

            }



            // Clear the track shape..

            Map1.EditLayer.Features.Clear();

        }

 


David



Thank you David, 



This is helping.  I had also implemented the foreach loop within the foreach loop approach, but wasn't sure if that was the best way to do it.  I can clear out the TrackShape like you suggested using Features.Clear(), but I DON'T call Features.Clear() for the highlight layer and that retains the selected polygons and that is what I needed. 



So it looks like the layer switcher only sees one static layer, not multiple ones (in your example, you have two static layers), right?  So I'll have to create my own.  Is there an easy way to tell a layer not to display, without removing it from memory?  If not, I'll use Map.StaticLayers.Remove() when I want a layer to not show up and then use Add() when I want it to be seen again.  I was doing this in my version 2 demo. 



Thanks again, 

Tom



Tom, 
  
   We will be enhancing the layer switcher as many people are asking about that.  I don’t have a time frame though. :(  When you build you own it is really easy to turn a layer off just use the Layer.IsVisible.  It is set to true by default and if you want the layer to not show just set to to False.  In this way you can keep your layer collection intact and just manipulate the layers you want.  On a side note most of our object have an IsVisible or and IsActive property.  If you set them to False then they are ignored.  Examples of this are ZoomLevels, Styles, and Layers of course. 
  
 Keep asking questions when you get stuck it really helps up make our product better. 
  
 David