首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用WinMapGIS或DotSpatial库在地图窗口中筛选出某些形状

如何使用WinMapGIS或DotSpatial库在地图窗口中筛选出某些形状
EN

Stack Overflow用户
提问于 2013-07-24 16:53:56
回答 1查看 3.1K关注 0票数 1

我有加州所有道路的大型街道形状档案。每当我在地图窗口重新绘制地图时,都要花很长时间(放大、缩小、移动地图.)。我想知道是否有任何方法告诉地图窗口只渲染一定数量的街道,根据特定的范围。这样,我就可以提高我的应用程序的整体性能。任何帮助都将不胜感激。

武川,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-15 16:32:25

像DotSpatial这样的地图应用程序将尝试为加载的shapefile呈现屏幕上的所有向量。他们不试图划出任何不在当前范围内的界线。但是当你被放大的时候,他们会试图精确地画出所有的线,这是缓慢的。一种可能有帮助的策略是设置可见性刻度,以便只在放大后绘制图层。DotSpatial中的Layer类允许您为该层设置动态可见性,这样您只能在放大超过某个点时才绘制完整的shapefile。

代码语言:javascript
复制
myLayer.UseDynamicVisiblity = true;
myLayer.DynamicVisibilityMode = DynamicVisibilityMode.ZoomedIn;
myLayer.DynamicVisibiltyWidth = .2; // eg. .2 degrees of longitude in WGS84 

这种策略的唯一缺点是,在缩小到指定的范围之前,您根本看不到任何道路。因此,一种选择是创建一个低分辨率版本的道路形状,当你放大,但不包含任何“重复”点,为一个更大的网格大小。可以通过使用另一个行shapefile或创建图像或创建图像块来做到这一点。下面的第一个示例使用1000 x 1000的网格大小,从而将冗余点减少到最小表示形式。使用不同网格大小和动态可见性的组合,您应该能够更有效地呈现大行形状文件。Raster通常有总体视图,但是向量通常没有,因此这是为向量创建人工概述的一种方法。第二个示例将使用DP行简化,它保留所有形状,但使用较少的点来表示每个形状。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DotSpatial.Data;
using DotSpatial.Projections;
using DotSpatial.Topology;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Shapefile source = new Shapefile();
            source = Shapefile.OpenFile(@"C:\Data\Rivers\River.shp");

            bool[,] pointRepresented = new bool[1000, 1000];
            double width = source.Extent.Width;
            double height = source.Extent.Height;
            double dx = width / 1000;
            double dy = height / 1000;
            FeatureSet result = new FeatureSet(FeatureType.Line);
            result.Projection = KnownCoordinateSystems.Geographic.World.WGS1984; // lat lon in WGS84
            result.DataTable.Columns.Add("Index", typeof(int));
            int index = 0;
            foreach (ShapeRange shape in source.ShapeIndices) {

                bool started = false;
                List<LineString> linestrings = new List<LineString>();
                foreach (PartRange part in shape.Parts) {
                    List<Coordinate> coords = new List<Coordinate>();
                    foreach (Vertex vert in part) {
                        int i = (int)((vert.X - source.Extent.MinX) / dx);
                        int j = (int)((vert.Y - source.Extent.MinY) / dy);
                        if (i > 999) {
                            i = 999;
                        }
                        if (j > 999) {
                            j = 999;
                        }
                        if (pointRepresented[i, j] == true) continue;
                        coords.Add(new Coordinate(vert.X, vert.Y));
                        pointRepresented[i, j] = true;
                    }
                    if (coords.Count > 0) {
                        if (coords.Count == 1) {
                            coords.Add(coords[0]); // add a duplicate "endpoint" to the line if we only have one point.
                        }
                        linestrings.Add(new LineString(coords));
                    }
                }
                if (linestrings.Count > 0) {
                    IFeature feature;
                    if (linestrings.Count > 1)
                    {
                        feature = result.AddFeature(new MultiLineString(linestrings));
                    }
                    else {
                        feature = result.AddFeature(linestrings[0]);
                    }
                    feature.DataRow["Index"] = index;
                    index++;
                }
                result.SaveAs(@"C:\Data\Rivers\RiverPreview.shp", true);
            }

            MessageBox.Show(@"Finished creating file: C:\Data\Rivers\RiverPreview.shp");
        }
    }

}

第二种方法使用DP线简化,不会删除形状,而只是减少用于表示每个形状的点数。公差必须调整,以匹配您的数据集和坐标。

代码语言:javascript
复制
        /// <summary>
        /// This alternative uses DP line simplification, which keeps every shape, but "simplifies" the shape, simply reducing
        /// the number of points.  This will not accomplish the goal of reducing the number of shapes, but will
        /// give you a reduced representation that matches the original features in cases where slowness is caused
        /// by too much detail, which can be useful if you still want to keep all your features.
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            Shapefile source = new Shapefile();
            source = Shapefile.OpenFile(@"C:\Data\Rivers\River.shp");
            FeatureSet result = new FeatureSet(FeatureType.Line);
            result.Projection = source.Projection;
            result.CopyTableSchema(source);
            foreach (IFeature feature in source.Features) {
                LineString linestring = feature.BasicGeometry as LineString;

                if (linestring != null)
                {
                    IList<Coordinate> points = linestring.Coordinates;
                    IList<Coordinate> simplified = DouglasPeuckerLineSimplifier.Simplify(points, .00002);
                    IFeature resultFeature = result.AddFeature(new LineString(simplified));
                    resultFeature.CopyAttributes(feature);
                }
                else {
                    MultiLineString multipleLines = feature.BasicGeometry as MultiLineString;

                    if (multipleLines != null)
                    {
                        List<LineString> resultLines = new List<LineString>();
                        foreach (IGeometry line in multipleLines.Geometries)
                        {
                            IList<Coordinate> points = line.Coordinates;
                            IList<Coordinate> simplified = DouglasPeuckerLineSimplifier.Simplify(points, .00002);
                            resultLines.Add(new LineString(simplified));
                        }
                        IFeature resultFeature = result.AddFeature(new MultiLineString(resultLines));
                        resultFeature.CopyAttributes(feature);
                    }
                }
            }

            result.SaveAs(@"C:\Data\Rivers\RiverSimplified.shp", true);
            MessageBox.Show(@"Finished creating file: C:\Data\Rivers\RiverSimplified.shp");
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17840290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档