• C#+SharpMap的相关代码


    //放大的代码:
    private void MapZoomIn(NameValueCollection queryString)
        {
            SharpMap.Map map = Session["map"] as SharpMap.Map;
            PointF pointFLeftBottom = new PointF(float.Parse(queryString["Left"]), float.Parse(queryString["Bottom"]));
            PointF pointFRightTop = new PointF(float.Parse(queryString["Right"]), float.Parse(queryString["Top"]));
    
            SharpMap.Geometries.BoundingBox zoomToBoundingBox = new SharpMap.Geometries.BoundingBox(map.ImageToWorld(pointFLeftBottom), map.ImageToWorld(pointFRightTop));
            map.ZoomToBox(zoomToBoundingBox);
            Session.Remove("map");
            Session["map"] = map;
        }
    
    //缩小的代码:
        private void MapZoomOut(NameValueCollection queryString)
        {
            SharpMap.Map map = Session["map"] as SharpMap.Map;
            PointF pointFLeftBottom = new PointF(float.Parse(queryString["Left"]), float.Parse(queryString["Bottom"]));
            PointF pointFRightTop = new PointF(float.Parse(queryString["Right"]), float.Parse(queryString["Top"]));
            SharpMap.Geometries.BoundingBox boundingBox = new SharpMap.Geometries.BoundingBox(map.ImageToWorld(pointFLeftBottom), map.ImageToWorld(pointFRightTop));        
            SharpMap.Geometries.Point mapCenterPoint = boundingBox.GetCentroid();       
            
            double ratio = 0.0;
            double ratioX=boundingBox.Width / map.Envelope.Width;
            double ratioY=boundingBox.Height / map.Envelope.Height;
            if (ratioX > ratioY)
            {
                ratio = ratioX;            
            }
            else
            {
                ratio = ratioY;
            }
            double semiWidth = map.Envelope.Width / ratio / 2;
            double semiHeight = map.Envelope.Height / ratio / 2;
            SharpMap.Geometries.BoundingBox zoomToBoundingBox = new SharpMap.Geometries.BoundingBox(mapCenterPoint.X - semiWidth, mapCenterPoint.Y - semiHeight, mapCenterPoint.X + semiWidth, mapCenterPoint.Y + semiHeight);
                    
            map.ZoomToBox(zoomToBoundingBox);
            Session.Remove("map");
            Session["map"] = map;
    
        }
    
    //平移的代码:
        private void MapPan(NameValueCollection queryString)
        {
            SharpMap.Map map = Session["map"] as SharpMap.Map;
            PointF pointFFirst = new PointF(float.Parse(queryString["FirstX"]), float.Parse(queryString["FirstY"]));
            PointF pointFSecond = new PointF(float.Parse(queryString["SecondX"]), float.Parse(queryString["SecondY"]));
            
            SharpMap.Geometries.Point firstPoint = map.ImageToWorld(pointFFirst);
            SharpMap.Geometries.Point secondPoint = map.ImageToWorld(pointFSecond);
            SharpMap.Geometries.Point mapCenterPoint = map.Center;
    
            SharpMap.Geometries.Point mapNewCenterPoint = new SharpMap.Geometries.Point(mapCenterPoint.X + firstPoint.X - secondPoint.X, mapCenterPoint.Y + firstPoint.Y - secondPoint.Y);
            map.Center = mapNewCenterPoint;
            
            Session.Remove("map");
            Session["map"] = map;
        }
    
    //全图显示的代码:
        private void MapFullExtent(NameValueCollection queryString)
        {
            SharpMap.Map map = Session["map"] as SharpMap.Map;
            map.ZoomToExtents();
            Session.Remove("map");
            Session["map"] = map;
        }
    
    //空间查询的代码:
    private void MapQuery(NameValueCollection queryString)
        {
            SharpMap.Map map = Session["map"] as SharpMap.Map;        
            PointF pointFLeftBottom = new PointF(float.Parse(queryString["Left"]), float.Parse(queryString["Bottom"]));
            PointF pointFRightTop = new PointF(float.Parse(queryString["Right"]), float.Parse(queryString["Top"]));
            SharpMap.Geometries.BoundingBox boundingBox = new SharpMap.Geometries.BoundingBox(map.ImageToWorld(pointFLeftBottom), map.ImageToWorld(pointFRightTop));        
            SharpMap.Data.FeatureDataSet featureDataSet = new SharpMap.Data.FeatureDataSet();
            
            ILayer pLayer = map.GetLayerByName(queryString["LayerName"]);
            SharpMap.Layers.VectorLayer vectorLayer = pLayer as SharpMap.Layers.VectorLayer;
            vectorLayer.DataSource.ExecuteIntersectionQuery(boundingBox, featureDataSet);        
            if (featureDataSet.Tables.Count > 0) //得到了结果 
            {
                //增加点击选择的对象到selection图层  
                //这样就新增了一个图层,因此渲染该副地图则显示选中对象  
                SharpMap.Layers.VectorLayer layerSelected = new SharpMap.Layers.VectorLayer("Selection");
                layerSelected.DataSource = new SharpMap.Data.Providers.GeometryProvider(featureDataSet.Tables[0]);
                layerSelected.Style.Fill = new System.Drawing.SolidBrush(System.Drawing.Color.Yellow);
                map.Layers.Add(layerSelected);
            }
            Session.Remove("map");
            Session["map"] = map;
        }
        
    
    //长度测量的代码:
    private void MapMeasurePolyline(NameValueCollection queryString, out string responseXML)
        {
            SharpMap.Map map = Session["map"] as SharpMap.Map;
            string pointsCoordinate = queryString["PointsCoordinate"];
            string[] pointCoordinatePair = pointsCoordinate.Split(',');
            SharpMap.Geometries.LineString lineStringMeasure = new SharpMap.Geometries.LineString();
    
            for (int i = 0; i < pointCoordinatePair.Length / 2; i++)
            {            
                PointF pointF=new PointF(Convert.ToSingle(pointCoordinatePair[i*2]),Convert.ToSingle(pointCoordinatePair[i*2+1]));
                SharpMap.Geometries.Point point = map.ImageToWorld(pointF);
                lineStringMeasure.Vertices.Add(point);
            }
                   
            StringBuilder resultsXML = new StringBuilder("");
            resultsXML.Append("<?xml version="1.0" encoding="utf-8"?>");        
            resultsXML.Append("<map>");
            resultsXML.Append("<measure>");
    
            resultsXML.Append("<长度>");
            resultsXML.Append("<![CDATA[");
            resultsXML.Append(lineStringMeasure.Length.ToString());
            resultsXML.Append("]]>");
            resultsXML.Append("</长度>");
    
            resultsXML.Append("</measure>");
            resultsXML.Append("</map>");
            responseXML = resultsXML.ToString();       
        }
  • 相关阅读:
    dedecms初解
    Java二十三设计模式之------单例模式
    Java二十三设计模式之------工厂方法模式
    数组和集合的区别及深入了解
    团队项目计划
    团队介绍及团队题目
    第二阶段冲刺(第十天)
    第二阶段冲刺(第九天)
    第二阶段冲刺(第八天)
    第二阶段冲刺(第七天)
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/5874563.html
Copyright © 2020-2023  润新知