コード例 #1
0
        public static void Run()
        {
            //ExStart: WriteFeaturesToTopoJson
            var outputPath = RunExamples.GetDataDir() + "sample_out.topojson";

            using (VectorLayer layer = VectorLayer.Create(outputPath, Drivers.TopoJson))
            {
                // add attributes that we are going to set
                layer.Attributes.Add(new FeatureAttribute("name", AttributeDataType.String));
                layer.Attributes.Add(new FeatureAttribute("measurement", AttributeDataType.Double));
                layer.Attributes.Add(new FeatureAttribute("id", AttributeDataType.Integer));

                var feature0 = layer.ConstructFeature();
                feature0.SetValue("name", "name_0");
                feature0.SetValue("measurement", 1.03);
                feature0.SetValue("id", 0);
                feature0.Geometry = new Point(1.3, 2.3);
                layer.Add(feature0);

                var feature1 = layer.ConstructFeature();
                feature1.SetValue("name", "name_1");
                feature1.SetValue("measurement", 10.03);
                feature1.SetValue("id", 1);
                feature1.Geometry = new Point(241.32, 23.2);
                layer.Add(feature1);
            }
            //ExEnd: WriteFeaturesToTopoJson
        }
        public static void Run()
        {
            //ExStart: ConvertGeoJsonLayerToLayerInFileGdbDataset
            var geoJsonPath = RunExamples.GetDataDir() + "ConvertGeoJsonLayerToLayerInFileGdbDataset_out.json";

            using (VectorLayer layer = VectorLayer.Create(geoJsonPath, Drivers.GeoJson))
            {
                layer.Attributes.Add(new FeatureAttribute("name", AttributeDataType.String));
                layer.Attributes.Add(new FeatureAttribute("age", AttributeDataType.Integer));

                Feature firstFeature = layer.ConstructFeature();
                firstFeature.Geometry = new Point(33.97, -118.25);
                firstFeature.SetValue("name", "John");
                firstFeature.SetValue("age", 23);
                layer.Add(firstFeature);

                Feature secondFeature = layer.ConstructFeature();
                secondFeature.Geometry = new Point(35.81, -96.28);
                secondFeature.SetValue("name", "Mary");
                secondFeature.SetValue("age", 54);
                layer.Add(secondFeature);
            }

            // --

            // -- copy test dataset, to avoid modification of test data.

            var sourceFile      = RunExamples.GetDataDir() + "ThreeLayers.gdb";
            var destinationFile = RunExamples.GetDataDir() + "ThreeLayersCopy_out.gdb";

            RunExamples.CopyDirectory(sourceFile, destinationFile);

            // --

            using (var geoJsonLayer = VectorLayer.Open(geoJsonPath, Drivers.GeoJson))
            {
                using (var fileGdbDataset = Dataset.Open(destinationFile, Drivers.FileGdb))
                    using (var fileGdbLayer = fileGdbDataset.CreateLayer("new_layer", SpatialReferenceSystem.Wgs84))
                    {
                        fileGdbLayer.CopyAttributes(geoJsonLayer);
                        foreach (var feature in geoJsonLayer)
                        {
                            fileGdbLayer.Add(feature);
                        }
                    }
            }
            //ExEnd: ConvertGeoJsonLayerToLayerInFileGdbDataset
        }
コード例 #3
0
        public static void Run()
        {
            string dataDir = RunExamples.GetDataDir();

            //ExStart: ExtractFeaturesFromShapeFileToGeoJSON
            using (VectorLayer inputLayer = VectorLayer.Open(dataDir + "InputShapeFile.shp", Drivers.Shapefile))
            {
                using (VectorLayer outputLayer = VectorLayer.Create(dataDir + "ExtractFeaturesFromShapeFileToGeoJSON_out.json", Drivers.GeoJson))
                {
                    outputLayer.CopyAttributes(inputLayer);
                    foreach (Feature inputFeature in inputLayer)
                    {
                        DateTime?date = inputFeature.GetValue <DateTime?>("dob");
                        if (date == null || date < new DateTime(1982, 1, 1))
                        {
                            continue;
                        }

                        //Construct a new feature
                        Feature outputFeature = outputLayer.ConstructFeature();
                        outputFeature.Geometry = inputFeature.Geometry;
                        outputFeature.CopyValues(inputFeature);
                        outputLayer.Add(outputFeature);
                    }
                }
            }
            //ExEnd: ExtractFeaturesFromShapeFileToGeoJSON
        }
        public static void Run()
        {
            //ExStart: LimitPrecisionWhenReadingGeometries
            string path = RunExamples.GetDataDir() + "LimitPrecisionWhenReadingGeometries_out.shp";

            using (VectorLayer layer = VectorLayer.Create(path, Drivers.Shapefile))
            {
                var feature = layer.ConstructFeature();
                feature.Geometry = new Point(1.10234, 2.09743);
                layer.Add(feature);
            }

            var options = new ShapefileOptions();

            // read data as-is.
            options.XYPrecisionModel = PrecisionModel.Exact;

            using (VectorLayer layer = VectorLayer.Open(path, Drivers.Shapefile, options))
            {
                var point = (IPoint)layer[0].Geometry;
                // 1.10234, 2.09743
                Console.WriteLine("{0}, {1}", point.X, point.Y);
            }

            // truncate all X and Y, so only two fractional digits are left.
            options.XYPrecisionModel = PrecisionModel.Rounding(2);

            using (VectorLayer layer = VectorLayer.Open(path, Drivers.Shapefile, options))
            {
                var point = (IPoint)layer[0].Geometry;
                // 1.1, 2.1
                Console.WriteLine("{0}, {1}", point.X, point.Y);
            }
            //ExEnd: LimitPrecisionWhenReadingGeometries
        }
        public static void Run()
        {
            //ExStart: LimitPrecisionWhenWritingGeometries
            var options = new GeoJsonOptions
            {
                // write only 3 fractional digits of X and Y coordinates.
                XYPrecisionModel = PrecisionModel.Rounding(3),

                // write all fractional digits of Z coordinate (the default, you don't have to specify it)
                ZPrecisionModel = PrecisionModel.Exact
            };

            var path = RunExamples.GetDataDir() + "LimitPrecisionWhenWritingGeometries_out.json";

            using (VectorLayer layer = VectorLayer.Create(path, Drivers.GeoJson, options))
            {
                var point = new Point();
                point.X = 1.8888888;
                point.Y = 1.00123;
                point.Z = 1.123456789;

                Feature feature = layer.ConstructFeature();
                feature.Geometry = point;
                layer.Add(feature);
            }

            using (VectorLayer layer = VectorLayer.Open(path, Drivers.GeoJson))
            {
                var point = (IPoint)layer[0].Geometry;

                // 1.889, 1.001, 1.123456789
                Console.WriteLine("{0}, {1}, {2}", point.X, point.Y, point.Z);
            }
            //ExEnd: LimitPrecisionWhenWritingGeometries
        }
コード例 #6
0
        public static void Run()
        {
            //ExStart: CreateCompoundCurve
            string path = RunExamples.GetDataDir() + "CreateCompoundCurve_out.shp";

            using (VectorLayer layer = VectorLayer.Create(path, Drivers.Shapefile))
            {
                var feature = layer.ConstructFeature();
                // create an 'S' letter (starts at bottom left end)
                var compoundCurve = new CompoundCurve();

                var bottom    = (ILineString)Geometry.FromText("LineString (0 0, 3 0)");
                var firstArc  = (ICircularString)Geometry.FromText("CircularString (3 0, 4 1, 3 2)");
                var middle    = (ILineString)Geometry.FromText("LineString (3 2, 1 2)");
                var secondArc = (ICircularString)Geometry.FromText("CircularString (1 2, 0 3, 1 4)");
                var top       = (ILineString)Geometry.FromText("LineString (1 4, 4 4)");

                compoundCurve.AddCurve(bottom);
                compoundCurve.AddCurve(firstArc);
                compoundCurve.AddCurve(middle);
                compoundCurve.AddCurve(secondArc);
                compoundCurve.AddCurve(top);
                feature.Geometry = compoundCurve;

                layer.Add(feature);
            }
            //ExEnd: CreateCompoundCurve
        }
コード例 #7
0
        public static void Run()
        {
            //ExStart: CreateCurvePolygon
            string path = RunExamples.GetDataDir() + "CreateCurvePolygon_out.shp";

            using (VectorLayer layer = VectorLayer.Create(path, Drivers.Shapefile))
            {
                var feature = layer.ConstructFeature();
                // create a torus with center at (0,0), radius equal to 2 and hole radius equal to 1
                var curvePolygon = new CurvePolygon();

                var exterior = new CircularString();
                exterior.AddPoint(-2, 0);
                exterior.AddPoint(0, 2);
                exterior.AddPoint(2, 0);
                exterior.AddPoint(0, -2);
                exterior.AddPoint(-2, 0);

                curvePolygon.ExteriorRing = exterior;

                var interior = new CircularString();
                interior.AddPoint(-1, 0);
                interior.AddPoint(0, 1);
                interior.AddPoint(1, 0);
                interior.AddPoint(0, -1);
                interior.AddPoint(-1, 0);

                curvePolygon.AddInteriorRing(interior);
                feature.Geometry = curvePolygon;

                layer.Add(feature);
            }
            //ExEnd: CreateCurvePolygon
        }
コード例 #8
0
        public static void Run()
        {
            string dataDir = RunExamples.GetDataDir();

            //ExStart: CreateNewShapeFile
            using (VectorLayer layer = VectorLayer.Create(dataDir + "NewShapeFile_out.shp", Drivers.Shapefile))
            {
                // add attributes before adding features
                layer.Attributes.Add(new FeatureAttribute("name", AttributeDataType.String));
                layer.Attributes.Add(new FeatureAttribute("age", AttributeDataType.Integer));
                layer.Attributes.Add(new FeatureAttribute("dob", AttributeDataType.DateTime));

                // case 1: sets values
                Feature firstFeature = layer.ConstructFeature();
                firstFeature.Geometry = new Point(33.97, -118.25);
                firstFeature.SetValue("name", "John");
                firstFeature.SetValue("age", 23);
                firstFeature.SetValue("dob", new DateTime(1982, 2, 5, 16, 30, 0));
                layer.Add(firstFeature);

                Feature secondFeature = layer.ConstructFeature();
                secondFeature.Geometry = new Point(35.81, -96.28);
                secondFeature.SetValue("name", "Mary");
                secondFeature.SetValue("age", 54);
                secondFeature.SetValue("dob", new DateTime(1984, 12, 15, 15, 30, 0));
                layer.Add(secondFeature);

                // case 2: sets new values for all of the attributes.
                Feature thirdFeature = layer.ConstructFeature();
                secondFeature.Geometry = new Point(34.81, -92.28);
                object[] data = new object[3] {
                    "Alex", 25, new DateTime(1989, 4, 15, 15, 30, 0)
                };
                secondFeature.SetValues(data);
                layer.Add(thirdFeature);
            }
            //ExEnd: CreateNewShapeFile
        }
コード例 #9
0
        public static void Run()
        {
            string dataDir = RunExamples.GetDataDir();

            //ExStart: SpecifyAttributeValueLength
            using (VectorLayer layer = VectorLayer.Create(dataDir + "SpecifyAttributeValueLength_out.shp", Drivers.Shapefile))
            {
                // add attributes before adding features
                FeatureAttribute attribute = new FeatureAttribute("wide", AttributeDataType.String);
                attribute.Width = 120;
                layer.Attributes.Add(attribute);

                Feature feature = layer.ConstructFeature();
                feature.SetValue("wide", "this string can be up to 120 characters long now.");
                layer.Add(feature);
            }
            //ExEnd: SpecifyAttributeValueLength
        }
コード例 #10
0
        public static void Run()
        {
            //ExStart: CreateMultiCurve
            string path = RunExamples.GetDataDir() + "CreateMultiCurve_out.shp";

            using (VectorLayer layer = VectorLayer.Create(path, Drivers.Shapefile))
            {
                var feature    = layer.ConstructFeature();
                var multiCurve = new MultiCurve();
                multiCurve.Add(Geometry.FromText("LineString (0 0, 1 0)"));
                multiCurve.Add(Geometry.FromText("CircularString (2 2, 3 3, 4 2)"));
                multiCurve.Add(Geometry.FromText("CompoundCurve ((0 1, 0 0), CircularString (0 0, 3 3, 6 0))"));
                feature.Geometry = multiCurve;

                layer.Add(feature);
            }
            //ExEnd: CreateMultiCurve
        }
コード例 #11
0
        public static void Run()
        {
            //ExStart: CreateCircularString
            string path = RunExamples.GetDataDir() + "CreateCircularString_out.shp";

            using (VectorLayer layer = VectorLayer.Create(path, Drivers.Shapefile))
            {
                var feature = layer.ConstructFeature();
                // create a circle with center at (1,0) and radius 1.
                var circularString = new CircularString();
                circularString.AddPoint(0, 0);
                circularString.AddPoint(1, 1);
                circularString.AddPoint(2, 0);
                circularString.AddPoint(1, -1);
                circularString.AddPoint(0, 0);
                feature.Geometry = circularString;

                layer.Add(feature);
            }
            //ExEnd: CreateCircularString
        }
コード例 #12
0
        public static void Run()
        {
            string dataDir = RunExamples.GetDataDir();

            //ExStart: ConvertPolygonShapeFileToLineStringShapeFile
            using (VectorLayer source = VectorLayer.Open(dataDir + "PolygonShapeFile.shp", Drivers.Shapefile))
            {
                using (VectorLayer destination = VectorLayer.Create(dataDir + "PolygonShapeFileToLineShapeFile_out.shp", Drivers.Shapefile))
                {
                    foreach (Feature sourceFeature in source)
                    {
                        Polygon    polygon            = (Polygon)sourceFeature.Geometry;
                        LineString line               = new LineString(polygon.ExteriorRing);
                        Feature    destinationFeature = destination.ConstructFeature();
                        destinationFeature.Geometry = line;
                        destination.Add(destinationFeature);
                    }
                }
            }
            //ExEnd: ConvertPolygonShapeFileToLineStringShapeFile
        }
コード例 #13
0
        public static void Run()
        {
            //ExStart: CreateMultiSurface
            string path = RunExamples.GetDataDir() + "CreateMultiSurface_out.json";

            using (VectorLayer layer = VectorLayer.Create(path, Drivers.GeoJson))
            {
                var feature      = layer.ConstructFeature();
                var multiSurface = new MultiSurface();

                var polygon = Geometry.FromText("Polygon ((0 0, 0 1, 1 1, 1 0, 0 0))");
                multiSurface.Add(polygon);

                var curvePolygon = Geometry.FromText("CurvePolygon (CircularString (-2 0, 0 2, 2 0, 0 -2, -2 0))");
                multiSurface.Add(curvePolygon);
                feature.Geometry = multiSurface;

                layer.Add(feature);
            }
            //ExEnd: CreateMultiSurface
        }
        public static void Run()
        {
            string dataDir = RunExamples.GetDataDir();
            string path    = dataDir + "SpecifyLayerSpatialReference_out.shp";

            //ExStart: SpecifyLayerSpatialReference
            var srs = SpatialReferenceSystem.CreateFromEpsg(26918);

            using (VectorLayer layer = VectorLayer.Create(path, Drivers.Shapefile, srs))
            {
                var feature = layer.ConstructFeature();
                feature.Geometry = new Point(60, 24);
                layer.Add(feature);
            }

            using (VectorLayer layer = VectorLayer.Open(path, Drivers.Shapefile))
            {
                Console.WriteLine(layer.SpatialReferenceSystem.EpsgCode); // 26918
                Console.WriteLine(layer.SpatialReferenceSystem.Name);     // NAD83_UTM_zone_18N
            }

            //ExEnd: SpecifyLayerSpatialReference
        }
コード例 #15
0
        public static void Run()
        {
            //ExStart: SpecifyLinearizationTolerance
            // If file format does not support curve geometries, we linearize them on write.
            // This example shows how to specify tolerance of the linearization.

            var options = new GeoJsonOptions
            {
                // linearized geometry must be within 1e-4 from curve geometry
                LinearizationTolerance = 1e-4,
            };

            string path = RunExamples.GetDataDir() + "SpecifyLinearizationTolerance_out.json";

            using (VectorLayer layer = VectorLayer.Create(path, Drivers.GeoJson, options))
            {
                var curveGeometry = Geometry.FromText("CircularString (0 0, 1 1, 2 0)");
                var feature       = layer.ConstructFeature();
                feature.Geometry = curveGeometry;
                // geometry is linearized with tolerance 1e-4
                layer.Add(feature);
            }
            //ExEnd: SpecifyLinearizationTolerance
        }