C#+MapServer相关代码

//放大的代码:

   private void MapZoomIn(NameValueCollection queryString)
    {
        mapObj map = Session["MapServer_MAP"] as mapObj;
        PointF pointFLeftBottom = new PointF(float.Parse(queryString["Left"]), float.Parse(queryString["Bottom"]));
        PointF pointFRightTop = new PointF(float.Parse(queryString["Right"]), float.Parse(queryString["Top"]));
        rectObj rect = map.extent; 
        rectObj rbox = new rectObj(pointFLeftBottom.X,pointFLeftBottom.Y,pointFRightTop.X,pointFRightTop.Y,1);
        map.zoomRectangle(rbox, map.width, map.height, rect, null); 
        Session.Remove("MapServer_MAP");
        Session["MapServer_MAP"] = map;
        
    }

//缩小的代码:
    private void MapZoomOut(NameValueCollection queryString)
    {
        mapObj map = Session["MapServer_MAP"] as mapObj;
        PointF pointFLeftBottom = new PointF(float.Parse(queryString["Left"]), float.Parse(queryString["Bottom"]));
        PointF pointFRightTop = new PointF(float.Parse(queryString["Right"]), float.Parse(queryString["Top"]));
        PointF pointFCenter = new PointF((pointFLeftBottom.X + pointFRightTop.X) / 2, (pointFLeftBottom.Y + pointFRightTop.Y) / 2);
        double ratio = 0.0;
        double ratioX = (pointFRightTop.X - pointFLeftBottom.X) * 1.0 / map.width;
        double ratioY = (pointFLeftBottom.Y - pointFRightTop.Y) * 1.0 / map.height;
        if (ratioX > ratioY)
        {
            ratio = ratioX;
        }
        else
        {
            ratio = ratioY;
        }
        map.zoomPoint(-((int)(1 / ratio)), new pointObj(pointFCenter.X, pointFCenter.Y, 0, 0), map.width, map.height, map.extent, null);       
        rectObj rectExtent = map.extent;
        double mapNewCenterX=(pointFCenter.X - 0.0) / map.width * (rectExtent.maxx - rectExtent.minx) + rectExtent.minx;
        double mapNewCenterY=(pointFCenter.Y - 0.0) / map.height * (rectExtent.maxy - rectExtent.miny) + rectExtent.miny;
           
        double minx = mapNewCenterX - (rectExtent.maxx - rectExtent.minx) / ratio / 2;
        double maxx = mapNewCenterX + (rectExtent.maxx - rectExtent.minx) / ratio / 2;
        double miny = mapNewCenterY - (rectExtent.maxy - rectExtent.miny) / ratio / 2;
        double maxy = mapNewCenterY + (rectExtent.maxy - rectExtent.miny) / ratio / 2;
        rectObj rectNewExtent = new rectObj(minx, miny, maxx, maxy, 0);        
        Session.Remove("MapServer_MAP");
        Session["MapServer_MAP"] = map;
    }

//平移的代码:

    private void MapPan(NameValueCollection queryString)
    {
        mapObj map = Session["MapServer_MAP"] as mapObj;
        PointF pointFFirst = new PointF(float.Parse(queryString["FirstX"]), float.Parse(queryString["FirstY"]));
        PointF pointFSecond = new PointF(float.Parse(queryString["SecondX"]), float.Parse(queryString["SecondY"]));
        System.Drawing.PointF pointFCenter = new PointF(map.width / 2 - (pointFSecond.X - pointFFirst.X), map.height / 2 - (pointFSecond.Y - pointFFirst.Y));        
        map.zoomPoint(1, new pointObj(pointFCenter.X, pointFCenter.Y, 0, 0), map.width, map.height, map.extent, null);
        Session.Remove("MapServer_MAP");
        Session["MapServer_MAP"] = map;
    }

//全图显示的代码:
    private void MapFullExtent(NameValueCollection queryString)
    {
        mapObj map = Session["MapServer_MAP"] as mapObj;
        rectObj rectOriginalExtent = Session["MapServer_OriginalExtent"] as rectObj;
        map.extent = rectOriginalExtent;        
        Session.Remove("MapServer_MAP");
        Session["MapServer_MAP"] = map;
    }

//属性查询的代码:
    private void MapAttributeQuery(NameValueCollection queryString, out string responseXML)
    {
        mapObj map = Session["MapServer_MAP"] as mapObj;
        layerObj layer = null;
        string layerName = queryString["LayerName"];
        string value = queryString["Value"];
        layer = map.getLayerByName(layerName);
        String attributeQueryString = "((\"[NAME]\" == '" + value + "') AND (\"[SHAPE_Area]\" > " + "'10'" + "))";
        Console.WriteLine("queryString="+queryString);
        String path = Server.MapPath("");
        Console.WriteLine("path: " + path);
        layer.status = 1;
        layer.queryByAttributes(map, "NAME", attributeQueryString, 1);
        layer.open();
        resultCacheObj resultCache = layer.getResults();
        int numresults=resultCache.numresults;
        string imagePath = GenerateMap();
        StringBuilder resultsXML = new StringBuilder("");
        resultsXML.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        resultsXML.Append("<map>");
        resultsXML.Append("<mapInfo>");
        resultsXML.Append("<mapPath>");
        resultsXML.Append("<![CDATA[");
        resultsXML.Append(imagePath);
        resultsXML.Append("]]>");
        resultsXML.Append("</mapPath>");
        resultsXML.Append("</mapInfo>");
        for(int j=0; j<numresults; j++)
        {
            resultCacheMemberObj resultCacheMember=resultCache.getResult(j);
            resultCacheMember = layer.getResult(j);                
            layer.open();
            shapeObj shapeResult = new shapeObj((int)MS_SHAPE_TYPE.MS_SHAPE_POLYGON); 
            layer.getShape(shapeResult, resultCacheMember.tileindex,resultCacheMember.shapeindex);
            layer.open();            

            resultsXML.Append("<feature>");
            resultsXML.Append("<objectID>");
            resultsXML.Append("<![CDATA[");
            resultsXML.Append(shapeResult.getValue(0));
            resultsXML.Append("]]>");
            resultsXML.Append("</objectID>");

            resultsXML.Append("<name>");
            resultsXML.Append("<![CDATA[");
            resultsXML.Append(shapeResult.getValue(4));
            resultsXML.Append("]]>");
            resultsXML.Append("</name>");

            resultsXML.Append("<area>");
            resultsXML.Append("<![CDATA[");
            resultsXML.Append(shapeResult.getValue(11));
            resultsXML.Append("]]>");
            resultsXML.Append("</area>");
            resultsXML.Append("</feature>");
        }
        layer.close();
        resultsXML.Append("</map>");
        Session.Remove("MapServer_MAP");
        Session["MapServer_MAP"] = map;
        responseXML = resultsXML.ToString();
    }

//空间查询的代码:

    private void MapSpatialQuery(NameValueCollection queryString, out string responseXML)
    {        
        mapObj map = Session["MapServer_MAP"] as mapObj;
        layerObj layer = null;
        for (int i = 0; i < map.numlayers; i++)
        {
            layer = map.getLayer(i);
            layer.opacity = 100;
            System.Diagnostics.Debug.WriteLine("LayerName= " + layer.name + " FeatureCount= " + layer.maxfeatures);
            layer.status = (int)mapscript.MS_ON;
            
        }
        
        string layerName = queryString["LayerName"];
        string value = queryString["Value"];
        layer = map.getLayerByName(layerName);
        PointF pointFLeftBottom = new PointF(float.Parse(queryString["Left"]), float.Parse(queryString["Bottom"]));
        PointF pointFRightTop = new PointF(float.Parse(queryString["Right"]), float.Parse(queryString["Top"]));
        pointObj pointMapLeftBottom = Pixel2Point(map, pointFLeftBottom.X, pointFLeftBottom.Y);
        pointObj pointMapRightTop = Pixel2Point(map, pointFRightTop.X, pointFRightTop.Y);
        rectObj rectSpatialQuery = new rectObj(pointMapLeftBottom.x, pointMapLeftBottom.y, pointMapRightTop.x, pointMapRightTop.y, 0);

        layer.status = (int)mapscript.MS_ON;
        layer.queryByRect(map, rectSpatialQuery);

        layer.open();
        resultCacheObj resultCache = layer.getResults();
        int numresults = resultCache.numresults;
        string imagePath = GenerateMap();
        StringBuilder resultsXML = new StringBuilder("");
        resultsXML.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        resultsXML.Append("<map>");
        resultsXML.Append("<mapInfo>");
        resultsXML.Append("<mapPath>");
        resultsXML.Append("<![CDATA[");
        resultsXML.Append(imagePath);
        resultsXML.Append("]]>");
        resultsXML.Append("</mapPath>");
        resultsXML.Append("</mapInfo>");
        for (int j = 0; j < numresults; j++)
        {
            resultCacheMemberObj resultCacheMember = resultCache.getResult(j);
            resultCacheMember = layer.getResult(j);
            layer.open();
            shapeObj shapeResult = new shapeObj((int)MS_SHAPE_TYPE.MS_SHAPE_POLYGON);
            layer.getShape(shapeResult, resultCacheMember.tileindex, resultCacheMember.shapeindex);
            layer.open();

            resultsXML.Append("<feature>");
            resultsXML.Append("<objectID>");
            resultsXML.Append("<![CDATA[");
            resultsXML.Append(shapeResult.getValue(0));
            resultsXML.Append("]]>");
            resultsXML.Append("</objectID>");

            resultsXML.Append("<name>");
            resultsXML.Append("<![CDATA[");
            resultsXML.Append(shapeResult.getValue(4));
            resultsXML.Append("]]>");
            resultsXML.Append("</name>");

            resultsXML.Append("<area>");
            resultsXML.Append("<![CDATA[");
            resultsXML.Append(shapeResult.getValue(11));
            resultsXML.Append("]]>");
            resultsXML.Append("</area>");
            resultsXML.Append("</feature>");
        }
        layer.close();
        resultsXML.Append("</map>");
        Session.Remove("MapServer_MAP");
        Session["MapServer_MAP"] = map;
        responseXML = resultsXML.ToString();
        System.Diagnostics.Debug.WriteLine(responseXML);
    }
posted @ 2016-09-15 11:30  ParamousGIS  阅读(504)  评论(0编辑  收藏  举报