Esempio n. 1
0
        public void UseShapefileFeaturesAdFeatureCoverageSource()
        {
            string path =
                Path.Combine(sharpMapGisShapeFilePath, "rivers.shp");
            ShapeFile shapeFile = new ShapeFile(path);

            // select only some features from shapefile
            Envelope coverageFeatureEnvelope = shapeFile.GetExtents();

            coverageFeatureEnvelope.ExpandBy(0.5, 0.5);

            var coverage = new FeatureCoverage();

            coverage.Components.Add(new Variable <double>("value"));
            coverage.Arguments.Add(new Variable <Feature>("feature"));
            coverage.Features = new EventedList <IFeature>(shapeFile.Features.Cast <IFeature>());
            coverage.FeatureVariable.AddValues(coverage.Features);

            double[] values = new double[coverage.FeatureVariable.Values.Count];
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = i;
            }

            coverage.SetValues(values);
        }
Esempio n. 2
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();

            this.shapeSRID    = sf.SRID;
            this.shapeExtends = sf.GetExtents();

            _sRID.Text = this.shapeSRID.ToString();

            FeatureDataSet ds = new FeatureDataSet();

            sf.ExecuteIntersectionQuery(this.shapeExtends, ds);
            //ds.Tables[0].Columns.Remove("Oid");
            return(ds.Tables[0]);
        }
Esempio n. 3
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]);
        }
Esempio n. 4
0
        public void FeatureCount()
        {
            string           path       = TestHelper.GetTestDataPath(@"DeltaShell\DeltaShell.Plugins.SharpMapGis.Tests\", "Europe_Lakes.shp");
            IFeatureProvider dataSource = new ShapeFile(path);

            Assert.AreEqual(37, dataSource.Features.Count);

            Assert.IsTrue(dataSource.Features.Count == dataSource.GetFeatures(dataSource.GetExtents()).Count);
        }
Esempio n. 5
0
        /// <summary>
        /// Open a TileIndex shapefile
        ///
        /// A tileindex is a shapefile that ties together several datasets into a single layer. Therefore, you don’t need to create separate layers for each piece of imagery or each county’s road data; make a tileindex and let SharpMap piece the mosaic together on the fly.
        /// Making a tileindex is easy using gdaltindex for GDAL data sources (rasters). You just run the tool, specifying the index file to create and the list of data sources to add to the index.
        ///
        /// For example, to make a mosaic of several TIFFs:
        ///
        /// gdaltindex imagery.shp imagery/*.tif

        /// See: http://mapserver.org/optimization/tileindex.html
        /// </summary>
        /// <param name="layerName">Name of the layer</param>
        /// <param name="fileName">Path to the ShapeFile containing tile-indexes</param>
        /// <param name="fieldName">FieldName in the shapefile storing full or relative path-names to the datasets</param>
        public GdalTileIndexRasterLayer(string layerName, string fileName, string fieldName) : base(layerName)
        {
            _fileName  = fileName;
            _shapeFile = new ShapeFile(fileName, true);
            _shapeFile.Open();
            _extents = _shapeFile.GetExtents();
            _shapeFile.Close();
            _fieldName = fieldName;
        }
Esempio n. 6
0
        /// <summary>
        /// Open a TileIndex shapefile
        ///
        /// A tileindex is a shapefile that ties together several datasets into a single layer. Therefore, you don’t need to create separate layers for each piece of imagery or each county’s road data; make a tileindex and let SharpMap piece the mosaic together on the fly.
        /// Making a tileindex is easy using gdaltindex for GDAL data sources (rasters). You just run the tool, specifying the index file to create and the list of data sources to add to the index.
        ///
        /// For example, to make a mosaic of several TIFFs:
        ///
        /// gdaltindex imagery.shp imagery/*.tif

        /// See: http://mapserver.org/optimization/tileindex.html
        /// </summary>
        /// <param name="layerName">Name of the layer</param>
        /// <param name="fileName">Path to the ShapeFile containing tile-indexes</param>
        /// <param name="fieldName">FieldName in the shapefile storing full or relative path-names to the datasets</param>
        public GdalTileIndexRasterLayer(string layerName, string fileName, string fieldName) : base(layerName)
        {
            _fileName  = fileName;
            _shapeFile = new ShapeFile(fileName, true);
            _shapeFile.Open();
            _extents = _shapeFile.GetExtents();
            _shapeFile.Close();
            _fieldName    = fieldName;
            _openDatasets = new Dictionary <string, CacheHolder>();
        }
Esempio n. 7
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]));
                    }
                }
            }
        }
Esempio n. 8
0
        public static int ReadShapefile(string file, out List <Geometry> features, out List <double> intervals, out List <int> types, out List <double> heights)
        {
            int flag = 0;

            features  = new List <Geometry>();
            types     = new List <int>();
            heights   = new List <double>();
            intervals = new List <double>();
            ShapeFile shp = null;

            try
            {
                shp = new ShapeFile(file);
            }
            catch (Exception e)
            {
                MainConsole.Instance.Output("Shapefile open failed!");
                throw new Exception("ShapeFile not exists! " + e.Message + e.StackTrace);
            }
            shp.Open();
            switch (shp.ShapeType)
            {
            case ShapeType.Point:
                flag = 0;
                break;

            case ShapeType.PolyLine:
                flag = 1;
                break;

            case ShapeType.Polygon:
                flag = 2;
                break;

            default:
                flag = -1;
                return(flag);
            }

            FeatureDataSet ds   = new FeatureDataSet();
            BoundingBox    bbox = shp.GetExtents();

            shp.ExecuteIntersectionQuery(bbox, ds);
            FeatureDataTable table = ds.Tables[0] as FeatureDataTable;

            try
            {
                foreach (FeatureDataRow row in table.Rows)
                {
                    features.Add(row.Geometry);
                    heights.Add(Convert.ToDouble(row["Height"]));
                    types.Add(Convert.ToInt32(row["Type"]));
                    if (flag != 0)
                    {
                        intervals.Add(Convert.ToDouble(row["Interval"]));
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("Attribute in shapefile incorrect with " + e.Message + e.StackTrace);
            }

            shp.Close();
            return(flag);
        }