Ejemplo n.º 1
0
        public FeatureDataTable GetFeatureTableFromShapefile(string shapeFilePath)
        {
            GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();
            int cd;

            if (!int.TryParse(_enconding.Text, out cd))
            {
                _status.Text = "Bad code page";
            }

            ShapeFile sf = new ShapeFile(shapeFilePath);

            sf.Encoding = Encoding.GetEncoding(int.Parse(_enconding.Text));
            sf.Open();

            FeatureDataSet ds = new FeatureDataSet();

            importExtent = sf.GetExtents();
            Debug.WriteLine("Loading " + sf.GetFeatureCount() + " features from shapefile...");

            //sf.ExecuteIntersectionQuery(importExtent, ds);
            ExecuteRobustIntersectionQuery(sf, importExtent, ds);
            //ds.Tables[0].Columns.Remove("Oid");
            return(ds.Tables[0]);
        }
Ejemplo n.º 2
0
        public VectorLayer getPolygonPoints(PointF points, VectorLayer veclayer)
        {
            //VectorLayer veclayer = (VectorLayer)m_viewBox.Map.GetLayerByName("province");
            SharpMap.Layers.VectorLayer laySelected = new SharpMap.Layers.VectorLayer("Selection");;
            CustomTheme myTheme = new CustomTheme(FeatureColoured);
            ShapeFile   vecshp  = (ShapeFile)veclayer.DataSource;
            ShapeFile   shp     = vecshp;

            if (!shp.IsOpen)
            {
                shp.Open();
            }

            FeatureDataSet   featDataSet   = new FeatureDataSet();
            FeatureDataTable featDataTable = null;
            //将point的大地坐标转为经纬度
            Projection pj = new Projection();

            points = pj.GetLatLonFromXY(points, cfg.pjPara);


            //   获取feature数量
            uint featCount = (uint)shp.GetFeatureCount();

            for (uint index = 0; index < featCount; index++)
            {
                FeatureDataRow r = shp.GetFeature(index);
                GeoAPI.Geometries.Coordinate[] geomes = r.Geometry.Coordinates;
                double[] geomsX = new double[geomes.Length];
                double[] geomsY = new double[geomes.Length];
                for (int j = 0; j < geomes.Length; j++)
                {
                    geomsX[j] = geomes[j].X;
                    geomsY[j] = geomes[j].Y;
                }

                if ((points.X < geomsX.Min()) || (points.X > geomsX.Max()) || (points.Y < geomsY.Min()) || (points.Y > geomsY.Max()))
                {
                    continue;
                }

                PointF p1 = new PointF();
                p1.X = points.X;
                p1.Y = points.Y;
                if (InPolygon(geomes, p1))
                {
                    //首先把geomes传出去,供其他使用
                    ContourGeomes = geomes;
                    //如果在某区域内,选中某个区域,放入新图层
                    laySelected.DataSource = new SharpMap.Data.Providers.GeometryProvider(shp.GetFeature(index));
                    polygon = ((NetTopologySuite.Geometries.Polygon)r.Geometry);
                    laySelected.Style.Fill = new System.Drawing.SolidBrush(Color.HotPink);
                    laySelected.CoordinateTransformation = veclayer.CoordinateTransformation;
                }
            }
            return(laySelected);
        }
Ejemplo n.º 3
0
        public void TestShapeFile(SqlServerSpatialObjectType spatialType)
        {
            using (var p = new ShapeFile(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp"), true))
            {
                p.Open();

                var env = p.GetExtents();
                if (spatialType == SqlServerSpatialObjectType.Geography && (env.MaxY > 90 || env.MaxY < -90))
                {
                    Assert.Ignore("Test file Y values exceed valid latitudes");
                }

                for (uint i = 0; i < p.GetFeatureCount(); i++)
                {
                    var fdr = p.GetFeature(i);
                    if (fdr.Geometry == null)
                    {
                        continue;
                    }

                    try
                    {
                        fdr.Geometry.SRID = 4326;
                        var res = ToSqlServerAndBack(fdr.Geometry, spatialType);
                        Assert.AreEqual(fdr.Geometry, res);
                        System.Diagnostics.Trace.WriteLine(string.Format("Feature {0} ({1}) converted!", i, fdr[0]));
                    }
                    catch (SqlGeometryConverterException)
                    {
                        System.Diagnostics.Trace.WriteLine(string.Format("Feature {0} ({1}) conversion failed!", i, fdr[0]));
                    }
                    catch (SqlGeographyConverterException)
                    {
                        System.Diagnostics.Trace.WriteLine(string.Format("Feature {0} ({1}) conversion failed!", i, fdr[0]));
                    }
                }
            }
        }
        public void ShowFeature(Predicate <System.Data.DataRow> p = null)
        {
            List <SharpMap.Geometries.Geometry> geometries = new List <SharpMap.Geometries.Geometry>();

            m_shapeFileData.Open();
            for (uint i = 0; i < m_shapeFileData.GetFeatureCount(); ++i)
            {
                SharpMap.Data.FeatureDataRow featureDataRow = m_shapeFileData.GetFeature(i);
                if (p == null || p(featureDataRow))
                {
                    geometries.Add(m_shapeFileData.GetGeometryByID(i));
                }
            }
            m_shapeFileData.Close();

            m_shapeLayer.DataSource = new GeometryFeatureProvider(geometries);
            if (myMap.Layers.IndexOf(m_shapeLayer) == -1)
            {
                myMap.Layers.Add(m_shapeLayer);
            }

            //myMap.ZoomToExtents();
        }