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
        }
示例#2
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
        }
示例#3
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
        }
示例#4
0
        private static void ModifyFeaturesInSingleLayer()
        {
            //ExStart: ModifyFeaturesInSingleLayer
            string sourcePath = Path.Combine(dataDir, "InputShapeFile.shp");
            string resultPath = Path.Combine(dataDir, "modified_out.shp");

            using (var source = VectorLayer.Open(sourcePath, Drivers.Shapefile))
                using (var result = VectorLayer.Create(resultPath,
                                                       Drivers.Shapefile,
                                                       source.SpatialReferenceSystem))
                {
                    result.CopyAttributes(source);

                    foreach (var feature in source)
                    {
                        // modify the geometry
                        var modifiedGeometry = feature.Geometry.GetBuffer(2.0);
                        feature.Geometry = modifiedGeometry;

                        // modify a feature attribute
                        var attributeValue         = feature.GetValue <string>("name");
                        var modifiedAttributeValue = attributeValue.ToUpper();
                        feature.SetValue("name", modifiedAttributeValue);

                        result.Add(feature);
                    }
                }

            //ExEnd: ModifyFeaturesInSingleLayer
        }
        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: 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
        }
示例#7
0
        public static void Run()
        {
            //ExStart: CreateFileGdbDatasetWithSingleLayer
            var path    = RunExamples.GetDataDir() + "CreateFileGdbDatasetWithSingleLayer_out.gdb";
            var options = new FileGdbOptions();

            using (var layer = VectorLayer.Create(path, Drivers.FileGdb, options, SpatialReferenceSystem.Wgs84))
            // this 'using' is equivalent to
            // using (var dataset = Dataset.Create(path, Drivers.FileGdb))
            // using (var layer = Dataset.CreateLayer("layer"))
            {
                var feature = layer.ConstructFeature();
                feature.Geometry = new LineString(new[]
                {
                    new Point(1, 2),
                    new Point(3, 4),
                });
                layer.Add(feature);
            }

            using (var dataset = Dataset.Open(path, Drivers.FileGdb))
                using (var layer = dataset.OpenLayer("layer"))
                {
                    Console.WriteLine("Features count: {0}", layer.Count); // 1
                }
            //ExEnd: CreateFileGdbDatasetWithSingleLayer
        }
示例#8
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: WriteGeoJsonToStream
            using (var memoryStream = new MemoryStream())
            {
                using (var layer = VectorLayer.Create(AbstractPath.FromStream(memoryStream), 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);
                }
                Console.WriteLine(Encoding.UTF8.GetString(memoryStream.ToArray()));
            }
            //ExEnd: WriteGeoJsonToStream
        }
        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
        }
示例#11
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
        }
        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
        }
示例#13
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
        }
        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()
        {
            //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
        }
        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
        }
        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
        }
示例#18
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
        }
示例#19
0
        public static void Main(string[] args)
        {
            // This program manipulates the GIS data on the Azure Blob storage.
            // It uses an inheritor of "Aspose.GIS.AbstractPath". If you need to implement your own inheritor of
            // "Aspose.GIS.AbstractPath" to support some external file storage, refer to "AzurePath" class.
            // This program:
            // - crates a GeoJSON layer on the Azure Blob storage;
            // - reads a Shapefile from the Azure Blob storage and prints its geometries;
            // - converts the Shapefile located on the Azure Blob Storage to the KML file located on the same storage.

            // First - we need to get the blob container
            var blobContainer = GetBlobContainer("azureintegrationcontainer");

            // -- Create a GeoJSON Layer in the Azure Blob Storage --
            // Create an instance of 'AzurePath'. 'AzurePath' is an inheritor of 'AbstratPath'
            // and we can use it to make Aspose.GIS work with Azure Blob storage.
            // See the AzurePath class for detailed description on how it works and how you can implement
            // your own inheritor of 'AbstractPath'.
            var geoJsonAzurePath = new AzurePath(blobContainer, "directory/file.json");

            // Delete a geojson file if it exists.
            geoJsonAzurePath.Delete();
            // Create the layer.
            using (var layer = VectorLayer.Create(geoJsonAzurePath, Drivers.GeoJson))
            {
                layer.Attributes.Add(new FeatureAttribute("id", AttributeDataType.Integer));
                var feature = layer.ConstructFeature();
                feature.SetValue("id", 1);
                feature.Geometry = new Point(1, 2);
                layer.Add(feature);

                feature = layer.ConstructFeature();
                feature.SetValue("id", 2);
                feature.Geometry = new Point(3, 4);
                layer.Add(feature);
            }
            var geoJsonText = blobContainer.GetBlockBlobReference("directory/file.json").DownloadText();

            Console.WriteLine("Created GeoJSON:");
            Console.WriteLine(geoJsonText);
            // --

            // There is no way to create a Shapefile via AzurePath, since Shapefile requires write streams to support
            // seeking. We upload the Shapefile from the local file system.
            blobContainer.GetBlockBlobReference("shapefile/point.shp").UploadFromFile("shapefile/point.shp");
            blobContainer.GetBlockBlobReference("shapefile/point.shx").UploadFromFile("shapefile/point.shx");
            blobContainer.GetBlockBlobReference("shapefile/point.dbf").UploadFromFile("shapefile/point.dbf");

            Console.WriteLine("Shapefile Geometries:");
            // -- Read the Shapefile From the Azure Blob Storage --
            var shapefileAzurePath = new AzurePath(blobContainer, "shapefile/point.shp");

            using (var layer = VectorLayer.Open(shapefileAzurePath, Drivers.Shapefile))
            {
                foreach (var feature in layer)
                {
                    Console.WriteLine(feature.Geometry.AsText());
                }
            }
            // --
            Console.WriteLine();

            // -- Convert the Shapefile on the Azure Blob Storage to the KML on the Same Storage --
            var kmlAzurePath = new AzurePath(blobContainer, "kml.kml");

            // Delete destination if it already exists
            kmlAzurePath.Delete();
            VectorLayer.Convert(shapefileAzurePath, Drivers.Shapefile, kmlAzurePath, Drivers.Kml);
            // --

            var kmlText = blobContainer.GetBlockBlobReference("kml.kml").DownloadText();

            Console.WriteLine("Converted KML:");
            Console.WriteLine(kmlText);
        }