ThinkGeo.com    |     Documentation    |     Premium Support

Translate code from another version - Help needed

HI,


  What I am wanting to define a set of points and display all points in a map at the proper zoom level.  I found this code, but some of the classes do not resolve.  Could someone help out with this transaltion, or suggest a better way?


private RectangleR CalculateMapExtent() 



    //Declare Variables 

    PointShape pointShape; 

    double upperLeftLat = 0; 

    double upperLeftLong = 0; 

    double lowerRightLat = 0; 

    double lowerRightLong = 0;


    //Loop through the points collection to find the upper left and lower right point values 

    foreach (BaseMapShape baseMapShape in Map1.MapShapes)

    {

        if (baseMapShape is PointMapShape)

        {

            pointShape = (PointShape)baseMapShape.BaseShape;

            if (pointShape.Y > upperLeftLat | upperLeftLat == 0) upperLeftLat = pointShape.Y;

            if (pointShape.X < upperLeftLong | upperLeftLong == 0) upperLeftLong = pointShape.X;

            if (pointShape.Y < lowerRightLat | lowerRightLat == 0) lowerRightLat = pointShape.Y;

            if (pointShape.X > lowerRightLong | lowerRightLong == 0) lowerRightLong = pointShape.X;

        }

    }


    //Create variables to set the extent 

    PointR upperLeftPoint = new PointR(upperLeftLong, upperLeftLat);

    PointR lowerRightPoint = new PointR(lowerRightLong, lowerRightLat);

    RectangleR mapExtent = new RectangleR(upperLeftPoint, lowerRightPoint);

    

    //Scale Up Map extent by 5% 

    mapExtent.ScaleUp(5);

    if (mapExtent.get_Width(MapLengthUnits.DecimalDegrees, MapLengthUnits.miles) < 400)

    {

        while (!(mapExtent.get_Width(MapLengthUnits.DecimalDegrees, MapLengthUnits.miles) > 400))

        {

            mapExtent.ScaleUp(5);

        }

    }

    return mapExtent;

}



Hi Steve,



This is the code from 2.0; in 3.0 we get rid of some of the old API and have some good API instead.



In this code, we removed the MapShapes collection; instead, we use InMemoryFeatureLayer instead to maintain these shapes. So let's change the requirement of this code a little: To find an extent to include all the shapes which you passed in from the parameter. The code is quite simple.

private RectangleShape CalculateMapExtent(Collection<Feature> features)
{
    RectangleShape returnExtent = new RectangleShape();
    foreach (Feature feature in features)
    {
        returnExtent.ExpandToInclude(feature);
    }

    returnExtent.ScaleUp(5);

    return returnExtent;
}



Any questions please let me know.



Thanks,

Howard



Howard, 
   Your code works, just not as I expected.  Please look at my code and modify. 
  
     public partial class PlotAPoint : System.Web.UI.Page 
     { 
         protected void Page_Load(object sender, EventArgs e) 
         { 
             if (!Page.IsPostBack) 
             { 
                 Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#B3C6D4")); 
                 Map1.CurrentExtent = new RectangleShape(-88.1422, 42.4009, -87.5169, 41.4529); 
                 Map1.MapUnit = GeographyUnit.DecimalDegree; 
                 Map1.MapTools.PanZoom.Enabled = true; 
                 Map1.MapTools.PanZoomBar.Enabled = false; 
  
                 ShapeFileFeatureLayer defaultLayer = new ShapeFileFeatureLayer(Server.MapPath("~/Map/ILlkA.shp")); 
                 defaultLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(LineStyles.LocalRoad1); 
                 defaultLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; 
  
                 Map1.StaticOverlay.Layers.Add(defaultLayer); 
  
                 // 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.StaticOverlay.ClientCache.CacheId = "defaultOverlay"; 
                 Map1.StaticOverlay.ServerCache.CacheDirectory = MapPath("~/ImageCache/" + Request.Path);    
  
                 InMemoryFeatureLayer shapeLayer = new InMemoryFeatureLayer(); 
                 shapeLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle =  
                     PointStyles.CreateSimplePointStyle(PointSymbolType.Circle, GeoColor.StandardColors.Green, 3); 
                 shapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; 
                 Map1.DynamicOverlay.Layers.Add("pointShapeLayer", shapeLayer); 
  
                 Feature pointFeature = new Feature(new PointShape( 
                     double.Parse("-87.8225020", CultureInfo.InvariantCulture), double.Parse("42.3754010", CultureInfo.InvariantCulture))); 
                 shapeLayer.InternalFeatures.Add(pointFeature.Id, pointFeature); 
  
                 pointFeature = new Feature(new PointShape( 
                     double.Parse("-88.08842", CultureInfo.InvariantCulture), double.Parse("42.19746", CultureInfo.InvariantCulture))); 
                 shapeLayer.InternalFeatures.Add(pointFeature.Id, pointFeature); 
  
                 pointFeature = new Feature(new PointShape( 
                     double.Parse("-88.063", CultureInfo.InvariantCulture), double.Parse("42.21907", CultureInfo.InvariantCulture))); 
                 shapeLayer.InternalFeatures.Add(pointFeature.Id, pointFeature); 
  
                 Map1.CurrentExtent = CalculateMapExtent(shapeLayer.InternalFeatures); 
  
                 Map1.DynamicOverlay.Redraw(); 
             } 
         } 
  
         private RectangleShape CalculateMapExtent(Collection<Feature> features) 
         { 
             RectangleShape returnExtent = new RectangleShape(); 
             foreach (Feature feature in features) 
             { 
                 returnExtent.ExpandToInclude(feature); 
             } 
  
             returnExtent.ScaleUp(5); 
  
             return returnExtent; 
         } 
  
     } 
  
 Thanks, 
   Steven 
  


Steven,



The method I pasted above is a generic method; it'll be much more easier to find the extent from a FeatureLayer.



You can get rid of the CalculateMapExtent method and Map1.DynamicOverlay.Redraw(); use the following script instead:

shapeLayer.Open();
Map1.CurrentExtent = shapeLayer.GetBoundingBox();
shapeLayer.Close();



If you have any questions please let me know.



Thanks,

Howard



Howard, 
   Exellent, thank you very much. 
  
 Steven

Steven, 



You are welcome; if you have any queries, please let me know. 



Thanks, 

Howard