[mapguide-users] How to zoom to the selected feature, or how to caculate the scale?

JohnSoft giszhao at gmail.com
Tue Aug 14 01:34:18 PDT 2012


public string GetZoomScaleFromSel()
        {
            Double fullExtentScale =
Convert.ToDouble(GetParameter(this.args, "FullExtentScale"));
            MgResourceService resourceService =
(MgResourceService)this.site.CreateService(MgServiceType.ResourceService);
            MgFeatureService featureService =
(MgFeatureService)this.site.CreateService(MgServiceType.FeatureService);

            MgMap map = new MgMap();
            map.Open(resourceService, GetParameter(this.args, "MAPNAME"));
            MgLayer layer =
(MgLayer)map.GetLayers().GetItem(GetParameter(this.args, "LAYERNAME"));
            MgResourceIdentifier resId = new
MgResourceIdentifier(layer.GetFeatureSourceId());
            String featureClass = layer.GetFeatureClassName();

            String[] schemaClass = layer.GetFeatureClassName().Split(new
Char[] { ':' });
            MgClassDefinition classDef =
featureService.GetClassDefinition(resId, schemaClass[0], schemaClass[1]);

            MgPropertyCollection properties = new MgPropertyCollection();
            MgDataPropertyDefinition dataPropDef = null;
            String jsonIdList = GetParameter(this.args, "IDLIST");
            JsonObject idList = JsonConvert.Import(jsonIdList) as
JsonObject;
            if (idList != null)
            {
                foreach (String key in idList.Names)
                {
                    String value = idList[key].ToString();
                    dataPropDef =
(MgDataPropertyDefinition)classDef.GetProperties().GetItem(key);
                    switch (dataPropDef.GetDataType())
                    {
                        case MgPropertyType.Boolean:
                            properties.Add(new MgBooleanProperty(key,
Boolean.Parse(value)));
                            break;
                        case MgPropertyType.Byte:
                            properties.Add(new MgByteProperty(key,
Byte.Parse(value)));
                            break;
                        case MgPropertyType.Single:
                            properties.Add(new MgSingleProperty(key,
(float)Double.Parse(value)));
                            break;
                        case MgPropertyType.Double:
                            properties.Add(new MgDoubleProperty(key,
Double.Parse(value)));
                            break;
                        case MgPropertyType.Int16:
                            properties.Add(new MgInt16Property(key,
Int16.Parse(value)));
                            break;
                        case MgPropertyType.Int32:
                            properties.Add(new MgInt32Property(key,
Int32.Parse(value)));
                            break;
                        case MgPropertyType.Int64:
                            properties.Add(new MgInt64Property(key,
Int64.Parse(value)));
                            break;
                        case MgPropertyType.String:
                            properties.Add(new MgStringProperty(key,
value));
                            break;
                        case MgPropertyType.DateTime:
                        case MgPropertyType.Null:
                        case MgPropertyType.Blob:
                        case MgPropertyType.Clob:
                        case MgPropertyType.Feature:
                        case MgPropertyType.Geometry:
                        case MgPropertyType.Raster:
                            break;
                    }
                }
            }

            MgSelection selection = new MgSelection(map);
            selection.AddFeatureIds(layer, featureClass, properties);

            MgEnvelope vSelExtents = selection.GetExtents(featureService);

            MgCoordinate vLLeftXY,vURightXY;
            Double vMapHeight, vMapWidth;
            Double vLLx, vLLy, vURx, vURy, vCtrX, vCtrY;
            String _MapScale, _CtrPtX, _CtrPtY;
 
            //Get the lower left and upper right coordinate points of the
extents to calculate the center
            vLLeftXY = vSelExtents.GetLowerLeftCoordinate(); 
            vURightXY = vSelExtents.GetUpperRightCoordinate();
            //Get the selection's height and width used to calculate a new
scale 
            vMapHeight = vSelExtents.GetHeight();
            vMapWidth = vSelExtents.GetWidth();
            //Get the individial values for X and Y for each the lower left
and upper right coordinate points
            vLLx = vLLeftXY.GetX();
            vLLy = vLLeftXY.GetY();
            vURx = vURightXY.GetX();
            vURy = vURightXY.GetY();
            //Calculate the center point X and Y values and convert them to
strings - that is what we need
            _CtrPtX = ((vURx - vLLx) / 2 + vLLx).ToString();
            _CtrPtY = ((vURy - vLLy) / 2 + vLLy).ToString();

            //Calculate the new map scale.  fullExtentScale is the map scale
when zoomed to the map's extent 
            if (vMapHeight > vMapWidth) 
            {
                 //Calculate scale when height of selection envelope is
greater than its width
                _MapScale = (((vMapHeight * fullExtentScale) /
map.DataExtent.Width) * 1.1).ToString();
            }
            else
            {
                // Calculate scale when width of selection envelope is
greater than its height
                _MapScale = (((vMapWidth * fullExtentScale) /
map.DataExtent.Height) * 1.1).ToString(); 
            }

            return _MapScale;
        }



--
View this message in context: http://osgeo-org.1560.n6.nabble.com/How-to-zoom-to-the-selected-feature-or-how-to-caculate-the-scale-tp4994178p4995031.html
Sent from the MapGuide Users mailing list archive at Nabble.com.


More information about the mapguide-users mailing list