Пример #1
0
        public void TestDiscussionNtsAndBaffeled()
        {
            var reader = new NetTopologySuite.IO.WKTReader();
            var poly   = reader.Read(
                @"POLYGON ((428999.76819468878 360451.93329044303, 428998.25517286535 360420.80827007542,
429023.1119599645 360406.75878171506, 429004.52340613387 360451.71714446822, 
429004.52340613387 360451.71714446822, 428999.76819468878 360451.93329044303))");

            var points = new System.Collections.Generic.List <GeoAPI.Geometries.IGeometry>(new []
            {
                reader.Factory.CreatePoint(new GeoAPI.Geometries.Coordinate(429012.5, 360443.18)),
                reader.Factory.CreatePoint(new GeoAPI.Geometries.Coordinate(429001.59, 360446.98)),
                reader.Factory.CreatePoint(new GeoAPI.Geometries.Coordinate(429003.31, 360425.45)),
                reader.Factory.CreatePoint(new GeoAPI.Geometries.Coordinate(429016.9, 360413.04))
            });

            var inside = new System.Collections.Generic.List <bool>(new[] { false, true, true, true });

            for (var i = 0; i < points.Count; i++)
            {
                NUnit.Framework.Assert.AreEqual(inside[i], poly.Intersects(points[i]));
            }

            var prepPoly = NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory.Prepare(poly);

            for (var i = 0; i < points.Count; i++)
            {
                NUnit.Framework.Assert.AreEqual(inside[i], prepPoly.Intersects(points[i]));
            }
        }
Пример #2
0
        public void TestDiscussionNtsAndBaffeled()
        {
            var reader = new NetTopologySuite.IO.WKTReader();
            var poly = reader.Read(
                @"POLYGON ((428999.76819468878 360451.93329044303, 428998.25517286535 360420.80827007542,
429023.1119599645 360406.75878171506, 429004.52340613387 360451.71714446822, 
429004.52340613387 360451.71714446822, 428999.76819468878 360451.93329044303))");

            var points = new System.Collections.Generic.List<GeoAPI.Geometries.IGeometry>(new []
                {
                    reader.Factory.CreatePoint(new GeoAPI.Geometries.Coordinate(429012.5, 360443.18)),
                    reader.Factory.CreatePoint(new GeoAPI.Geometries.Coordinate(429001.59, 360446.98)),
                    reader.Factory.CreatePoint(new GeoAPI.Geometries.Coordinate(429003.31, 360425.45)),
                    reader.Factory.CreatePoint(new GeoAPI.Geometries.Coordinate(429016.9, 360413.04))
                });

            var inside = new System.Collections.Generic.List<bool>(new[] {false, true, true, true});

            for (var i = 0; i < points.Count; i++)
            {
                NUnit.Framework.Assert.AreEqual(inside[i], poly.Intersects(points[i]));
            }

            var prepPoly = NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory.Prepare(poly);
            for (var i = 0; i < points.Count; i++)
            {
                NUnit.Framework.Assert.AreEqual(inside[i], prepPoly.Intersects(points[i]));
            }
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="self"></param>
        /// <param name="wkt"></param>
        /// <returns></returns>
        public static Geometry GeomFromText(this Geometry self, string wkt)
        {
            var factory = self == null ? new NetTopologySuite.Geometries.GeometryFactory() : self.Factory;
            var reader  = new NetTopologySuite.IO.WKTReader(factory);

            return(reader.Read(wkt));
        }
Пример #4
0
        public static void GeometryContainsPoint()
        {
            var      reader = new NetTopologySuite.IO.WKTReader();
            Geometry poly   = reader.Read(
                @"POLYGON ((428999.76819468878 360451.93329044303, 428998.25517286535 360420.80827007542,
429023.1119599645 360406.75878171506, 429004.52340613387 360451.71714446822, 
429004.52340613387 360451.71714446822, 428999.76819468878 360451.93329044303))");


            var points = new List <NetTopologySuite.Geometries.Geometry>(new[]
            {
                reader.Factory.CreatePoint(new Coordinate(429012.5, 360443.18)),
                reader.Factory.CreatePoint(new NetTopologySuite.Geometries.Coordinate(429001.59, 360446.98)),
                reader.Factory.CreatePoint(new NetTopologySuite.Geometries.Coordinate(429003.31, 360425.45)),
                reader.Factory.CreatePoint(new NetTopologySuite.Geometries.Coordinate(429016.9, 360413.04))
            });


            var inside = new List <bool>(new[] { false, true, true, true });

            for (var i = 0; i < points.Count; i++)
            {
                var contains = poly.Contains(points[i]);
                System.Console.Write(poly.Contains(points[i]));
            }
        }
        public void TestExecuteIntersectionQueryAgainstGeometry()
        {
            using (var p = CreateProvider())
            {
                var reader = new NetTopologySuite.IO.WKTReader();
                var poly   = reader.Read(
                    @"POLYGON ((100 100, 170 100, 170 170, 100 170, 100 100))");

                var fds = new FeatureDataSet();
                Assert.DoesNotThrow(() => p.ExecuteIntersectionQuery(poly, fds));
                Assert.AreEqual(1, fds.Tables.Count);
                var table = fds.Tables[0];
                Assert.AreEqual(2, table.Rows.Count);
            }
        }
        public void TestExecuteIntersectionQueryAgainstGeometryIntersectionButNoPointsWithin()
        {
            using (var p = CreateProvider())
            {
                var reader = new NetTopologySuite.IO.WKTReader();
                var poly   = reader.Read(
                    @"POLYGON ((101 101, 169 101, 169 169, 101 169, 101 101))");

                var fds = new FeatureDataSet();
                Assert.DoesNotThrow(() => p.ExecuteIntersectionQuery(poly, fds));
                Assert.AreEqual(1, fds.Tables.Count);
                var table = fds.Tables[0];
                Assert.AreEqual(1, table.Rows.Count);
                Assert.AreEqual("T2", table.Rows[0][1].ToString());
            }
        }
Пример #7
0
        public void TestExecuteIntersectionQueryAgainstGeometry()
        {
            using (var p = CreateProvider())
            {
                var reader = new NetTopologySuite.IO.WKTReader();
                var poly   = reader.Read(
                    @"POLYGON ((428999.76819468878 360451.93329044303, 428998.25517286535 360420.80827007542,
429023.1119599645 360406.75878171506, 429004.52340613387 360451.71714446822, 
429004.52340613387 360451.71714446822, 428999.76819468878 360451.93329044303))");

                var fds = new FeatureDataSet();
                Assert.DoesNotThrow(() => p.ExecuteIntersectionQuery(poly, fds));
                Assert.AreEqual(1, fds.Tables.Count);
                var table = fds.Tables[0];
                Assert.AreEqual(3, table.Rows.Count);
            }
        }
Пример #8
0
        public static GeometryObject SplitWorldExtent(Polygon poly)
        {
            MultiPolygon newpoly = new MultiPolygon();

            List <LineString> OuterlineStrings = new List <LineString>();

            List <LineString> InnerlineStrings = new List <LineString>();

            OuterlineStrings.AddRange(SplitWorldExtent(poly.LineStrings.First()));

            foreach (LineString lineString in poly.LineStrings.Skip(1))
            {
                InnerlineStrings.AddRange(SplitWorldExtent(lineString));
            }

            if (OuterlineStrings.Count == 1)
            {
                return(poly);
            }

            List <Polygon> newpolygons = new List <Polygon>();

            if (OuterlineStrings.Count > 0)
            {
                foreach (LineString ls in OuterlineStrings)
                {
                    Polygon curpoly = new Polygon();
                    newpoly.Polygons.Add(curpoly);
                    curpoly.LineStrings.Add(ls);
                    foreach (var innerls in InnerlineStrings)
                    {
                        NetTopologySuite.IO.WKTReader wktreader = new NetTopologySuite.IO.WKTReader();
                        var innerlr = wktreader.Read(innerls.ToWkt());
                        NetTopologySuite.Geometries.Polygon polygon = (NetTopologySuite.Geometries.Polygon)wktreader.Read(curpoly.ToWkt());

                        if (polygon.Contains(innerlr))
                        {
                            curpoly.LineStrings.Add(innerls);
                        }
                    }
                    newpolygons.Add(curpoly);
                }
            }

            return(newpoly);
        }
Пример #9
0
        public void TestDiscussionNtsAndBaffeledOleDb()
        {
            var reader = new NetTopologySuite.IO.WKTReader();
            var poly   = reader.Read(
                @"POLYGON ((428999.76819468878 360451.93329044303, 428998.25517286535 360420.80827007542,
429023.1119599645 360406.75878171506, 429004.52340613387 360451.71714446822, 
429004.52340613387 360451.71714446822, 428999.76819468878 360451.93329044303))");

            var table = WriteCsv();

            var p = new SharpMap.Data.Providers.OleDbPoint(
                "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + System.IO.Path.GetTempPath() + "\";" +
                "Extended Properties=\"text;HDR=Yes;FMT=Delimited\"", table, "ID", "X", "Y");

            var extents = p.GetExtents();

            NUnit.Framework.Assert.AreEqual(4, p.GetFeatureCount());
            p.DefinitionQuery = "Name='One'";
            NUnit.Framework.Assert.AreEqual(1, p.GetFeatureCount());
            var fdr = p.GetFeatureByOid(1);

            NUnit.Framework.Assert.AreEqual(1, (int)fdr.Attributes[0]);
            p.DefinitionQuery = string.Empty;

            var fds = new SharpMap.Data.FeatureDataSet();

            p.ExecuteIntersectionQuery(extents, fds);
            NUnit.Framework.Assert.AreEqual(1, fds.Tables.Count);
            NUnit.Framework.Assert.AreEqual(4, fds.Tables[0].Rows.Count);
            fds.Tables.Remove(fds.Tables[0]);

            p.ExecuteIntersectionQuery(poly, fds);
            NUnit.Framework.Assert.AreEqual(1, fds.Tables.Count);
            NUnit.Framework.Assert.AreEqual(3, fds.Tables[0].Rows.Count);

            var inside = new System.Collections.Generic.List <bool>(new[] { false, true, true, true });

            NUnit.Framework.Assert.AreEqual(System.Linq.Enumerable.Count(inside, b => b), fds.Tables[0].Rows.Count);

            var ext  = p.GetExtents();
            var oids = p.GetOidsInView(ext);

            NUnit.Framework.Assert.AreEqual(4, System.Linq.Enumerable.Count(oids));

            System.IO.File.Delete(System.IO.Path.Combine(System.IO.Path.GetTempPath(), table));
        }
Пример #10
0
        public void TestExecuteIntersectionQueryAgainstGeometry()
        {
            using (var p = CreateProvider())
            {
                var reader = new NetTopologySuite.IO.WKTReader();
                var poly = reader.Read(
                    @"POLYGON ((428999.76819468878 360451.93329044303, 428998.25517286535 360420.80827007542,
            429023.1119599645 360406.75878171506, 429004.52340613387 360451.71714446822,
            429004.52340613387 360451.71714446822, 428999.76819468878 360451.93329044303))");

                var fds = new FeatureDataSet();
                Assert.DoesNotThrow(() => p.ExecuteIntersectionQuery(poly, fds));
                Assert.AreEqual(1, fds.Tables.Count);
                var table = fds.Tables[0];
                Assert.AreEqual(_tableName, table.TableName);
                Assert.AreEqual(3, table.Rows.Count);
            }
        }
Пример #11
0
        public void TestDiscussionNtsAndBaffeledOleDb()
        {
            var reader = new NetTopologySuite.IO.WKTReader();
            var poly = reader.Read(
                @"POLYGON ((428999.76819468878 360451.93329044303, 428998.25517286535 360420.80827007542,
429023.1119599645 360406.75878171506, 429004.52340613387 360451.71714446822, 
429004.52340613387 360451.71714446822, 428999.76819468878 360451.93329044303))");

            var table = WriteCsv();

            var p = new SharpMap.Data.Providers.OleDbPoint(
                "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + System.IO.Path.GetTempPath() + "\";" +
                "Extended Properties=\"text;HDR=Yes;FMT=Delimited\"", table, "ID", "X", "Y");

            var extents = p.GetExtents();
            NUnit.Framework.Assert.AreEqual(4, p.GetFeatureCount());
            p.DefinitionQuery = "Name='One'";
            NUnit.Framework.Assert.AreEqual(1, p.GetFeatureCount());
            var fdr = p.GetFeature(1);
            NUnit.Framework.Assert.AreEqual(1, (int)fdr[0]);
            p.DefinitionQuery = string.Empty;

            var fds = new SharpMap.Data.FeatureDataSet();
            p.ExecuteIntersectionQuery(extents, fds);
            NUnit.Framework.Assert.AreEqual(1, fds.Tables.Count);
            NUnit.Framework.Assert.AreEqual(4, fds.Tables[0].Rows.Count);
            fds.Tables.Clear();

            p.ExecuteIntersectionQuery(poly, fds);
            NUnit.Framework.Assert.AreEqual(1, fds.Tables.Count);
            NUnit.Framework.Assert.AreEqual(3, fds.Tables[0].Rows.Count);

            var inside = new System.Collections.Generic.List<bool>(new[] { false, true, true, true });
            NUnit.Framework.Assert.AreEqual(System.Linq.Enumerable.Count(inside, (b) => b == true), fds.Tables[0].Rows.Count);

            var ext = p.GetExtents();
            var oids = p.GetObjectIDsInView(ext);
            NUnit.Framework.Assert.AreEqual(4, oids.Count);
            
            System.IO.File.Delete(System.IO.Path.Combine(System.IO.Path.GetTempPath(), table));
        }
Пример #12
0
        public static GeometryObject SplitWorldExtent(Polygon poly)
        {
            MultiPolygon newpoly = new MultiPolygon();

            List<LineString> OuterlineStrings = new List<LineString>();

            List<LineString> InnerlineStrings = new List<LineString>();

            OuterlineStrings.AddRange(SplitWorldExtent(poly.LineStrings.First()));

            foreach (LineString lineString in poly.LineStrings.Skip(1)) {
                InnerlineStrings.AddRange(SplitWorldExtent(lineString));
            }

            if (OuterlineStrings.Count == 1) {
                return poly;
            }

            List<Polygon> newpolygons = new List<Polygon>();

            if (OuterlineStrings.Count > 0) {
                foreach (LineString ls in OuterlineStrings) {

                    Polygon curpoly = new Polygon();
                    newpoly.Polygons.Add(curpoly);
                    curpoly.LineStrings.Add(ls);
                    foreach (var innerls in InnerlineStrings) {
                        NetTopologySuite.IO.WKTReader wktreader = new NetTopologySuite.IO.WKTReader();
                        var innerlr = wktreader.Read(innerls.ToWkt());
                        NetTopologySuite.Geometries.Polygon polygon = (NetTopologySuite.Geometries.Polygon)wktreader.Read(curpoly.ToWkt());

                        if (polygon.Contains(innerlr))
                            curpoly.LineStrings.Add(innerls);
                    }
                    newpolygons.Add(curpoly);
                }

            }

            return newpoly;
        }
Пример #13
0
        static void Main(string[] args)
        {
            // Load up the sample farm/field/cropzone information
            var treeData = (JArray)(JObject.Parse(File.ReadAllText("tree.json"))["Results"]);

            // Load up the field/cropzone boundaries
            var boundaryData = (JArray)(JObject.Parse(File.ReadAllText("boundaries.json"))["Results"]);

            // Initialize a Well-Known-Text (WKT) reader for handling the sample boundary data
            GeometryFactory geometryFactory = new GeometryFactory();

            NetTopologySuite.IO.WKTReader wktReader = new NetTopologySuite.IO.WKTReader(geometryFactory);

            // In this console app the ADMPlugin is included as a NuGet package so the ADMPlugin.dll is always
            // copied directly in to the executable directory. That's why we tell the PluginFactory to look there
            // for the ADMPlugin.
            string applicationPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

            // The PluginFactory looks at all the DLLs in the target directory to find any that implement the IPlugin interface.
            var pluginFactory = new PluginFactory(applicationPath);

            // We're only interested in the ADMPlugin here, so I address it directly instead of looking through all the
            // available plugins that the PluginFactory found.
            var admPlugin = pluginFactory.GetPlugin("ADMPlugin");

            // The ADMPlugin doesn't require any initialization parameters.
            admPlugin.Initialize();

            // The ApplicationDataModel is the root object in ADAPT
            ApplicationDataModel export = new ApplicationDataModel();

            // The Catalog object (inside the ApplicationDataModel) holds all the items you would expect to find in a "pick list".
            // Alternatively, you could think of it as the place you put everything used "by reference" in any of the Documents
            // you are trying to send.
            export.Catalog = new Catalog();

            // The Documents object (inside the ApplicationDataModel) holds all the Plans, Recommendations, WorkOrders, and
            // WorkRecords (and their component parts). We won't be using this in this example.
            export.Documents = new Documents();

            // Create a "crop year" TimeScope to tag objects with.
            TimeScope cropYear = new TimeScope();

            cropYear.Description = "2017";
            cropYear.DateContext = DateContextEnum.CropSeason;
            export.Catalog.TimeScopes.Add(cropYear);

            // Create the Grower object. The constructor will automatically create the Id property and assign the
            // next available ReferenceId integer.
            Grower adaptGrower = new Grower();

            // Associate your internal, unique identifier to the Grower object by creating a UniqueId object
            // and adding it to the Grower object's CompoundIdentifier.
            UniqueId ourId = new UniqueId();

            ourId.Id = "7d2253f0-fce6-4740-b3c3-f9c8ab92bfaa";

            // Notice the available IdTypeEnum choices. Not everybody uses the same way of identifying things in their
            // system. As a result, we must support a number of identification schemes.
            ourId.IdType = IdTypeEnum.UUID;

            // Almost as important as the identifier is knowing who created it (or where it came from).
            ourId.Source     = "www.agconnections.com";
            ourId.SourceType = IdSourceTypeEnum.URI;

            // Each CompoundIdentifier that is used in ADAPT can have multiple unique identifiers associated with it.
            // Consider the possibilites here, not only can your identifier for something be peristed but also the
            // identifiers that your trading partner assigns to the same object. PLEASE CONSIDER PERSISTING AND RETURNING
            // IDENTIFIERS PASSED TO YOU IN THIS FASHION. This has the potential to result in a "frictionless" conversation
            // once the initial mapping is done, buy this benefit will only emerge if we all are "good neighbors".
            adaptGrower.Id.UniqueIds.Add(ourId);

            // You may notice that many of the objects in ADAPT have a minimal number of properties. Don't panic if you
            // can't find a place to put all your data. It may be in an associated object or intended to be expressed
            // as a ContextItem.
            adaptGrower.Name = "Ponderosa Farms";
            // Add the Grower object to the Catalog.
            export.Catalog.Growers.Add(adaptGrower);

            // Pull the farm objects out of the sample JSON test data
            var farms = (from c in treeData where ((string)c["type"] == "farm") select c).ToList();

            // Iterate over each farm
            foreach (var farm in farms)
            {
                // Create the Farm object. The constructor will automatically create the Id property and assign the
                // next available ReferenceId integer.
                Farm adaptFarm = new Farm();
                ourId            = new UniqueId();
                ourId.Id         = (string)farm["id"];
                ourId.IdType     = IdTypeEnum.UUID;
                ourId.Source     = "www.agconnections.com";
                ourId.SourceType = IdSourceTypeEnum.URI;
                adaptFarm.Id.UniqueIds.Add(ourId);
                adaptFarm.Description = (string)farm["text"];
                // Here we link this farm object to the grower. Note that this is the integer (ReferenceId) in the
                // Grower's CompountIdentifier object.
                adaptFarm.GrowerId = adaptGrower.Id.ReferenceId;
                // Add the Farm object to the Catalog.
                export.Catalog.Farms.Add(adaptFarm);
                // Pull the field objects out of the sample JSON test data that are part of this iteration's farm
                var fields = (from c in treeData where (((string)c["type"] == "field") && ((string)c["parent"] == (string)farm["id"])) select c).ToList();
                // Iterate over each field
                foreach (var field in fields)
                {
                    // Create the Field object. The constructor will automatically create the Id property and assign the
                    // next available ReferenceId integer.
                    Field adaptField = new Field();
                    ourId            = new UniqueId();
                    ourId.Id         = (string)field["id"];
                    ourId.IdType     = IdTypeEnum.UUID;
                    ourId.Source     = "www.agconnections.com";
                    ourId.SourceType = IdSourceTypeEnum.URI;
                    adaptField.Id.UniqueIds.Add(ourId);
                    adaptField.Description = (string)field["text"];
                    // Here we link this field object to the farm. Note that this is the integer (ReferenceId) in the
                    // Farm's CompountIdentifier object.
                    adaptField.FarmId = adaptFarm.Id.ReferenceId;
                    // Pull the boundary object out of the sample JSON test data (if it exists)
                    var fieldBoundary = (from c in boundaryData where (((string)c["FieldId"] == (string)field["id"]) && ((string)c["CropZoneId"] == null)) select c).FirstOrDefault();
                    if (fieldBoundary != null)
                    {
                        // This sample data has boundaries expressed as MultiPolygons in WKT so we need to transform that into the correlary ADAPT objects.
                        // Your data may use a different geometry (instead of MultiPolygon) to describe your boundaries so your code may differ at this point.
                        var boundary = wktReader.Read((string)fieldBoundary["MapData"]) as NetTopologySuite.Geometries.MultiPolygon;
                        AgGateway.ADAPT.ApplicationDataModel.Shapes.MultiPolygon adaptMultiPolygon = new AgGateway.ADAPT.ApplicationDataModel.Shapes.MultiPolygon();
                        adaptMultiPolygon.Polygons = new List <AgGateway.ADAPT.ApplicationDataModel.Shapes.Polygon>();
                        foreach (var geometry in boundary.Geometries)
                        {
                            var polygon = geometry as NetTopologySuite.Geometries.Polygon;
                            AgGateway.ADAPT.ApplicationDataModel.Shapes.Polygon adaptPolygon = new AgGateway.ADAPT.ApplicationDataModel.Shapes.Polygon();
                            adaptPolygon.ExteriorRing  = new AgGateway.ADAPT.ApplicationDataModel.Shapes.LinearRing();
                            adaptPolygon.InteriorRings = new List <AgGateway.ADAPT.ApplicationDataModel.Shapes.LinearRing>();
                            foreach (var coordinate in polygon.ExteriorRing.Coordinates)
                            {
                                var adaptPoint = new AgGateway.ADAPT.ApplicationDataModel.Shapes.Point();
                                adaptPoint.X = coordinate.X;
                                adaptPoint.Y = coordinate.Y;
                                adaptPolygon.ExteriorRing.Points.Add(adaptPoint);
                            }
                            foreach (var ring in polygon.InteriorRings)
                            {
                                var adaptRing = new AgGateway.ADAPT.ApplicationDataModel.Shapes.LinearRing();
                                adaptRing.Points = new List <AgGateway.ADAPT.ApplicationDataModel.Shapes.Point>();
                                foreach (var coordinate in ring.Coordinates)
                                {
                                    var adaptPoint = new AgGateway.ADAPT.ApplicationDataModel.Shapes.Point();
                                    adaptPoint.X = coordinate.X;
                                    adaptPoint.Y = coordinate.Y;
                                    adaptRing.Points.Add(adaptPoint);
                                }
                                adaptPolygon.InteriorRings.Add(adaptRing);
                            }
                            adaptMultiPolygon.Polygons.Add(adaptPolygon);
                        }
                        // Unlike the CropZone object (which holds its geomertry internally) a Field's boundary is held in a separate FieldBoundary object.
                        // Create the FieldBoundary object. The constructor will automatically create the Id property and assign the
                        // next available ReferenceId integer.
                        FieldBoundary adaptBoundary = new FieldBoundary();
                        // The FieldBoundary.SpatialData property is an ADAPT Shape object (which is an abastract class). What you actually attach here
                        // is one of the child classes of Shape (Polygon, MultiPolygon, etc.).
                        adaptBoundary.SpatialData = adaptMultiPolygon;
                        // Here we link this field boundary object to the field. Note that this is the integer (ReferenceId) in the
                        // Field's CompountIdentifier object.
                        adaptBoundary.FieldId = adaptField.Id.ReferenceId;
                        // Add the FieldBoundary object to the Catalog.
                        export.Catalog.FieldBoundaries.Add(adaptBoundary);
                        // It is possible for a given Field to have multiple FieldBounday objects associated with it, but we need to be able
                        // to indicate which one should be used by "default".
                        adaptField.ActiveBoundaryId = adaptBoundary.Id.ReferenceId;
                    }
                    // Add the Field object to the Catalog. *Note: We are adding this to the Catalog here so that we don't have to go
                    // back and fetch the object to set the ActiveBoundaryId property. Not required, just convenient.
                    export.Catalog.Fields.Add(adaptField);

                    // We're defining a CropZone as a spatial area within a field grown to a crop during a specific window of time.
                    // This is fundamentally different from the concept of a management zone (that might vary by plant population or soil type).
                    // Pull the cropzone objects out of the sample JSON test data that are part of this iteration's field
                    var cropzones = (from c in treeData where (((string)c["type"] == "cropzone") && ((string)c["parent"] == (string)field["id"])) select c).ToList();
                    // Iterate over each cropzone
                    foreach (var cropzone in cropzones)
                    {
                        // It's entirely possible that we have already added this Crop to the Catalog during a previous iteration. We need to check
                        // the Crop list in Catalog first and reuse that object if it exists.
                        Crop adaptCrop = null;
                        var  crops     = export.Catalog.Crops.Where(x => (x.Name == (string)cropzone["li_attr"]["CropName"])).ToList();
                        if (crops.Count > 0)
                        {
                            adaptCrop = crops[0];
                        }
                        else
                        {
                            // Create the Crop object. The constructor will automatically create the Id property and assign the
                            // next available ReferenceId integer.
                            adaptCrop        = new Crop();
                            ourId            = new UniqueId();
                            ourId.Id         = (string)cropzone["li_attr"]["CropId"];
                            ourId.IdType     = IdTypeEnum.UUID;
                            ourId.Source     = "www.agconnections.com";
                            ourId.SourceType = IdSourceTypeEnum.URI;
                            adaptCrop.Id.UniqueIds.Add(ourId);
                            adaptCrop.Name = (string)cropzone["li_attr"]["CropName"];

                            // Add EPPO code as ContextItem at some point in the future

                            // Add the Crop object to the Catalog.
                            export.Catalog.Crops.Add(adaptCrop);
                        }
                        // Create the CropZone object. The constructor will automatically create the Id property and assign the
                        // next available ReferenceId integer.
                        CropZone adaptCropZone = new CropZone();
                        ourId            = new UniqueId();
                        ourId.Id         = (string)cropzone["id"];
                        ourId.IdType     = IdTypeEnum.UUID;
                        ourId.Source     = "www.agconnections.com";
                        ourId.SourceType = IdSourceTypeEnum.URI;
                        adaptCropZone.Id.UniqueIds.Add(ourId);
                        adaptCropZone.Description = (string)cropzone["text"];
                        // Here we link this cropzone object to the field. Note that this is the integer (ReferenceId) in the
                        // Field's CompountIdentifier object.
                        adaptCropZone.FieldId = adaptField.Id.ReferenceId;
                        // Here we link this cropzone object to the crop. Note that this is the integer (ReferenceId) in the
                        // Crop's CompountIdentifier object.
                        adaptCropZone.CropId = adaptCrop.Id.ReferenceId;
                        // Here we link this cropzone object to the crop year TimeScope. Note that the TimeScope is used BY VALUE
                        // instead of BY REFERENCE (like the field and crop above).
                        adaptCropZone.TimeScopes.Add(cropYear);
                        string areaString = (string)cropzone["li_attr"]["AreaValue"];
                        if (!string.IsNullOrEmpty(areaString))
                        {
                            double area = Convert.ToDouble(areaString);
                            adaptCropZone.Area = new NumericRepresentationValue(RepresentationInstanceList.vrReportedFieldArea.ToModelRepresentation(), new NumericValue(UnitSystemManager.GetUnitOfMeasure("ac1"), area));
                        }
                        // As mentioned before, the CropZone (unlike Field) holds its boundary internally. Also unlike field, a CropZone is only expected
                        // to have a single boundary due to its scope in crop & time.
                        var czBoundary = (from c in boundaryData where ((string)c["CropZoneId"] == (string)cropzone["id"]) select c).FirstOrDefault();
                        if (czBoundary != null)
                        {
                            var boundary = wktReader.Read((string)czBoundary["MapData"]) as NetTopologySuite.Geometries.MultiPolygon;
                            AgGateway.ADAPT.ApplicationDataModel.Shapes.MultiPolygon adaptMultiPolygon = new AgGateway.ADAPT.ApplicationDataModel.Shapes.MultiPolygon();
                            adaptMultiPolygon.Polygons = new List <AgGateway.ADAPT.ApplicationDataModel.Shapes.Polygon>();
                            foreach (var geometry in boundary.Geometries)
                            {
                                var polygon = geometry as NetTopologySuite.Geometries.Polygon;
                                AgGateway.ADAPT.ApplicationDataModel.Shapes.Polygon adaptPolygon = new AgGateway.ADAPT.ApplicationDataModel.Shapes.Polygon();
                                adaptPolygon.ExteriorRing  = new AgGateway.ADAPT.ApplicationDataModel.Shapes.LinearRing();
                                adaptPolygon.InteriorRings = new List <AgGateway.ADAPT.ApplicationDataModel.Shapes.LinearRing>();
                                foreach (var coordinate in polygon.ExteriorRing.Coordinates)
                                {
                                    var adaptPoint = new AgGateway.ADAPT.ApplicationDataModel.Shapes.Point();
                                    adaptPoint.X = coordinate.X;
                                    adaptPoint.Y = coordinate.Y;
                                    adaptPolygon.ExteriorRing.Points.Add(adaptPoint);
                                }
                                foreach (var ring in polygon.InteriorRings)
                                {
                                    var adaptRing = new AgGateway.ADAPT.ApplicationDataModel.Shapes.LinearRing();
                                    adaptRing.Points = new List <AgGateway.ADAPT.ApplicationDataModel.Shapes.Point>();
                                    foreach (var coordinate in ring.Coordinates)
                                    {
                                        var adaptPoint = new AgGateway.ADAPT.ApplicationDataModel.Shapes.Point();
                                        adaptPoint.X = coordinate.X;
                                        adaptPoint.Y = coordinate.Y;
                                        adaptRing.Points.Add(adaptPoint);
                                    }
                                    adaptPolygon.InteriorRings.Add(adaptRing);
                                }
                                adaptMultiPolygon.Polygons.Add(adaptPolygon);
                            }
                            adaptCropZone.BoundingRegion = adaptMultiPolygon;
                        }
                        // Add the CropZone object to the Catalog.
                        export.Catalog.CropZones.Add(adaptCropZone);
                    }
                }
            }
            // At this point we have added all the Grower/Farm/Field objects to the Catalog and are ready to export.
            // Create an output path
            string outputPath = applicationPath + @"\Output";

            if (Directory.Exists(outputPath))
            {
                Directory.Delete(outputPath, true);
            }
            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }
            // Export to a local directory using the ADMPlugin
            admPlugin.Export(export, outputPath);

            // The ADMPlugin creates an "adm" subdirectory in the indicated local directory that contains the following items:
            //      An additional "documents" subdirectory that contains the protobuf-encoded document files.
            //      An AdmVersion.info file that contains version information.
            //      A ProprietaryValues.adm file
            //      A Catalog.adm file that contains the zipped JSON serialization of the ApplicationDataModel.Catalog object.

            // We've added logic here to zip that "adm" subdirectory into a single file, in case you want to email it to someone.
            string zipPath = applicationPath + @"\Zip";

            if (Directory.Exists(zipPath))
            {
                Directory.Delete(zipPath, true);
            }
            if (!Directory.Exists(zipPath))
            {
                Directory.CreateDirectory(zipPath);
            }
            // Delete the file if it already exists
            string zipFile = zipPath + @"\tree.zip";

            if (File.Exists(zipFile))
            {
                File.Delete(zipFile);
            }
            ZipFile.CreateFromDirectory(outputPath, zipFile);

            // This is logic to import the same data from the "adm" subdirectory we just created so you can compare it
            // in the debugger if you want.
            var pluginFactory2 = new PluginFactory(applicationPath);
            var admPlugin2     = pluginFactory.GetPlugin("ADMPlugin");

            admPlugin2.Initialize();
            // Note that when a plugin imports, the returned object is a list of ApplicationDataModel objects.
            var imports = admPlugin2.Import(outputPath);
        }
Пример #14
0
 private IGeometry GeomFromText(String Wkt)
 {
     NetTopologySuite.IO.WKTReader reader = new NetTopologySuite.IO.WKTReader();
     return(reader.Read(Wkt));
 }
Пример #15
0
        public static void SeedingDatabase()
        {
            using (var db = new AppDbContext())
            {
                if (db.Fields.Count() == 0 && db.People.Count() == 0)
                {
                    var geomfactory = new GeometryFactory(new PrecisionModel(), 4326);
                    var wktReader   = new NetTopologySuite.IO.WKTReader(geomfactory); // reading for wkt to geometry

                    var wkt1  = "MultiPolygon (((29.4695947 36.35849605, 29.46567075 36.37798056, 29.46567075 36.39061656, 29.47286465 36.40114498, 29.48594447 36.4079877, 29.50229425 36.41009302, 29.51537408 36.40167137, 29.5284539 36.39219591, 29.53041587 36.38377232, 29.520606 36.37640092, 29.50425623 36.37482124, 29.49183039 36.37482124, 29.47875057 36.37376811, 29.48202053 36.36323598, 29.48267452 36.35375584, 29.4695947 36.35849605)))";
                    var geom1 = wktReader.Read(wkt1);   // converting wkt to geom
                    var wkt2  = "MultiPolygon (((29.48267452 36.2694372, 29.49967829 36.27787317, 29.51799004 36.27734595, 29.52126 36.27681872, 29.5356478 36.2736553, 29.54022574 36.2694372, 29.53433982 36.26627348, 29.51864403 36.26680077, 29.5062182 36.26680077, 29.49379237 36.26310963, 29.48790645 36.26100032, 29.48267452 36.2694372)))";
                    var geom2 = wktReader.Read(wkt2);   // converting wkt to geom
                    var wkt3  = "MultiPolygon (((29.65990611 36.26047299, 29.65271221 36.26627348, 29.65598217 36.2694372, 29.66906199 36.26996448, 29.68148782 36.27312805, 29.69391365 36.27787317, 29.70045356 36.27418254, 29.70306953 36.26785536, 29.70045356 36.26047299, 29.69718361 36.25467207, 29.69325966 36.25256253, 29.68214181 36.25098035, 29.67167795 36.24992554, 29.65990611 36.26047299)))";
                    var geom3 = wktReader.Read(wkt3);   // converting wkt to geom
                    var wkt4  = "MultiPolygon (((29.79920622 36.33584576, 29.78089447 36.34216743, 29.78481842 36.35270242, 29.79659026 36.36270933, 29.80443815 36.36428925, 29.81424802 36.36218268, 29.82601986 36.35270242, 29.8305978 36.34480131, 29.82798183 36.33900666, 29.82405789 36.33795304, 29.79920622 36.33584576)))";
                    var geom4 = wktReader.Read(wkt4);   // converting wkt to geom
                    var wkt5  = "MultiPolygon (((29.87245323 36.3932488, 29.87310722 36.41114567, 29.8822631 36.4153561, 29.88880301 36.41325091, 29.89272695 36.41693495, 29.89599691 36.42009256, 29.90122884 36.4195663, 29.9038448 36.41167198, 29.90319081 36.4048296, 29.89992086 36.40272412, 29.89207296 36.39956581, 29.88553305 36.39851301, 29.88291709 36.39166946, 29.88160911 36.38745775, 29.87245323 36.3932488)))";
                    var geom5 = wktReader.Read(wkt5);   // converting wkt to geom
                    var wkt6  = "MultiPolygon (((30.03987496 36.36481589, 30.03987496 36.37218838, 30.05033882 36.38324581, 30.06341864 36.39272236, 30.06145667 36.38324581, 30.05622474 36.37060862, 30.05230079 36.36060272, 30.04968483 36.35480924, 30.04118294 36.35796937, 30.03987496 36.36481589)))";
                    var geom6 = wktReader.Read(wkt6);   // converting wkt to geom
                    var wkt7  = "MultiPolygon (((29.9411223 36.24306893, 29.94243028 36.25308992, 29.95616409 36.26100032, 29.96728194 36.26469157, 29.98363172 36.26627348, 29.99474957 36.26890992, 30.00259746 36.2726008, 30.01633128 36.26785536, 30.02417917 36.26574618, 30.03791298 36.26047299, 30.03595101 36.25572681, 30.02090922 36.24676103, 30.00782939 36.23884918, 29.99867352 36.23515672, 29.99017163 36.22882637, 29.97643782 36.22460585, 29.96401199 36.22144031, 29.95289414 36.22460585, 29.94439225 36.22935391, 29.94046831 36.2319916, 29.9411223 36.24306893)))";
                    var geom7 = wktReader.Read(wkt7);

                    var PersonData = new List <Person>()
                    {
                        new Person {
                            Id = 1, Name = "Burhan", Job = "Engineer", Gender = "Male", Age = 26
                        },
                        new Person {
                            Id = 2, Name = "Mehmet", Job = "Farmer", Gender = "Male", Age = 21
                        },
                        new Person {
                            Id = 3, Name = "Ayse", Job = "Baker", Gender = "Female", Age = 29
                        },
                        new Person {
                            Id = 4, Name = "Zekeriya", Job = "İssiz", Gender = "Male", Age = 54
                        }
                    };

                    var fieldData = new List <Field>()
                    {
                        new Field {
                            Id = 1, Name = "parsel-1", PersonId = 1, Geom = geom1, Wkt = wkt1
                        },
                        new Field {
                            Id = 2, Name = "parsel-2", PersonId = 1, Geom = geom2, Wkt = wkt2
                        },
                        new Field {
                            Id = 3, Name = "parsel-3", PersonId = 1, Geom = geom3, Wkt = wkt3
                        },
                        new Field {
                            Id = 4, Name = "parsel-4", PersonId = 2, Geom = geom4, Wkt = wkt4
                        },
                        new Field {
                            Id = 5, Name = "parsel-5", PersonId = 3, Geom = geom5, Wkt = wkt5
                        },
                        new Field {
                            Id = 6, Name = "parsel-6", PersonId = 3, Geom = geom6, Wkt = wkt6
                        },
                        new Field {
                            Id = 7, Name = "parsel-7", PersonId = 4, Geom = geom7, Wkt = wkt7
                        }
                    };

                    db.People.AddRange(PersonData);
                    db.Fields.AddRange(fieldData);

                    db.SaveChanges();

                    System.Console.WriteLine("veriler yuklenmistir....");
                }
                else
                {
                    System.Console.WriteLine("veriler onceden yuklenmistir ...");
                }
            }
        }
Пример #16
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="self"></param>
 /// <param name="wkt"></param>
 /// <returns></returns>
 public static IGeometry GeomFromText(this IGeometry self, string wkt)
 {
     var factory = self == null ? new NetTopologySuite.Geometries.GeometryFactory() : self.Factory;
     var reader = new NetTopologySuite.IO.WKTReader(factory);
     return reader.Read(wkt);
 }
Пример #17
0
        public static void Test()
        {
            string s1 = "POLYGON((7.5999034 47.5506347,7.5997595 47.5507183,7.5998959 47.5508256,7.5999759 47.5508885,7.6001195 47.550805,7.5999034 47.5506347))";
            string s2 = "POLYGON((7.6003356 47.5509754,7.6001195 47.550805,7.5999759 47.5508885,7.6000322 47.5509328,7.6001926 47.551059,7.6003356 47.5509754))";

            s1 = "POLYGON((7.5999034 47.5506347,7.6001195 47.550805,7.5999759 47.5508885,7.5998959 47.5508256,7.5997595 47.5507183,7.5999034 47.5506347))";
            s2 = "POLYGON((7.6003356 47.5509754,7.6001926 47.551059,7.6000322 47.5509328,7.5999759 47.5508885,7.6001195 47.550805,7.6003356 47.5509754))";


            // NetTopologySuite.Geometries.Implementation.CoordinateArraySequenceFactory
            // GeoAPI.Geometries.IGeometryFactory geoFactory = new NetTopologySuite.Geometries.GeometryFactory();


            NetTopologySuite.IO.WKTReader wr = new NetTopologySuite.IO.WKTReader();

            Wgs84Coordinates[] coords1 = PolygonParsingExtensions.PolygonStringToCoordinates(s1);
            Wgs84Coordinates[] coords2 = PolygonParsingExtensions.PolygonStringToCoordinates(s2);

            var lr = new NetTopologySuite.Geometries.LinearRing(coords1.ToNetTopologyCoordinates());

            System.Console.WriteLine(lr.IsValid);

            var x = new NetTopologySuite.Geometries.Polygon(new NetTopologySuite.Geometries.LinearRing(coords1.ToNetTopologyCoordinates()));

            System.Console.WriteLine(x.IsValid);

            NetTopologySuite.Geometries.GeometryFactory geomFactory = new NetTopologySuite.Geometries.GeometryFactory();

            GeoAPI.Geometries.IPolygon poly1 = geomFactory.CreatePolygon(coords1.ToNetTopologyCoordinates());
            GeoAPI.Geometries.IPolygon poly2 = geomFactory.CreatePolygon(coords2.ToNetTopologyCoordinates());



            /*
             * GeoAPI.Geometries.IPolygon poly1 = (GeoAPI.Geometries.IPolygon)wr.Read(s1);
             * GeoAPI.Geometries.IPolygon poly2 = (GeoAPI.Geometries.IPolygon)wr.Read(s2);
             */

            poly1.SRID = 4326;
            poly2.SRID = 4326;



            CalculateArea2(coords1);
            CalculateArea2(coords2);


            System.Console.WriteLine(poly1.Area);
            System.Console.WriteLine(poly2.Area);


            GeoAPI.Geometries.IPolygon poly3quick = (GeoAPI.Geometries.IPolygon)poly1.Union(poly2);
            System.Console.WriteLine(poly3quick.IsValid);

            // https://gis.stackexchange.com/questions/209797/how-to-get-geometry-points-using-geo-api
            System.Console.Write(poly1.IsValid);
            System.Console.Write(poly2.IsValid);


            System.Collections.Generic.List <GeoAPI.Geometries.IGeometry> lsPolygons =
                new System.Collections.Generic.List <GeoAPI.Geometries.IGeometry>();

            lsPolygons.Add(poly1);
            lsPolygons.Add(poly2);


            GeoAPI.Geometries.IGeometry ig = NetTopologySuite.Operation.Union.CascadedPolygonUnion.Union(lsPolygons);
            System.Console.WriteLine(ig.GetType().FullName);

            GeoAPI.Geometries.IPolygon poly3 = (GeoAPI.Geometries.IPolygon)ig;
            System.Console.WriteLine(poly3);

            // POLYGON ((7.5997595 47.5507183, 7.5999034 47.5506347, 7.6001195 47.550805, 7.6003356 47.5509754
            // , 7.6001926 47.551059, 7.6000322 47.5509328, 7.5999759 47.5508885
            // , 7.5998959 47.5508256, 7.5997595 47.5507183))

            System.Console.WriteLine(poly3.Shell.Coordinates);


            /*
             * // GeoAPI.Geometries.IPolygon poly3 = (GeoAPI.Geometries.IPolygon)ig;
             * NetTopologySuite.Geometries.MultiPolygon poly3a = (NetTopologySuite.Geometries.MultiPolygon)ig;
             * GeoAPI.Geometries.IGeometry ig2 = poly3a.ConvexHull();
             * System.Console.WriteLine(ig2.GetType().FullName);
             */

            // GeoAPI.Geometries.IPolygon poly4 = (GeoAPI.Geometries.IPolygon)ig2;
            // System.Console.WriteLine(poly4);


            System.Console.WriteLine("--- Press any key to continue --- ");
            System.Console.ReadKey();
        } // End Sub Test