Exemple #1
0
        public IPolygon BoundingPolygon(IList <IPoint> pList, ISpatialReference spatialrefrence)
        {
            try
            {
                IGeometryBridge2  pGeoBrg    = new GeometryEnvironment() as IGeometryBridge2;
                IPointCollection4 pPointColl = (IPointCollection4) new Multipoint(); // edited here

                int        numPoints       = pList.Count;
                WKSPoint[] aWKSPointBuffer = new WKSPoint[numPoints];
                for (int i = 0; i < pList.Count; i++)
                {
                    WKSPoint A = new WKSPoint();
                    A.X = pList[i].X;
                    A.Y = pList[i].Y;
                    aWKSPointBuffer[i] = A;
                }
                pGeoBrg.SetWKSPoints(pPointColl, ref aWKSPointBuffer);

                // edits here
                IGeometry pGeom = (IMultipoint)pPointColl;
                pGeom.SpatialReference = spatialrefrence;
                ITopologicalOperator pTopOp      = (ITopologicalOperator)pGeom;
                IPolygon             pPointColl2 = (IPolygon)pTopOp.ConvexHull();

                pPointColl2.SpatialReference = spatialrefrence;
                // OutputPolygon = pPointColl2; maybe you don't need this line as the object is not used
                return(pPointColl2);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
        /// <summary>Convert a GeoJSON Polygon geometry to Arcgis Geometry </summary>
        /// <param name="JSpoint">The deserialised GeoJson Object</param>
        /// <param name="epsg">The EPSG-code of the spatial reference, -1 is unknown</param>
        /// <returns>A Arcgis Polygon goemetry</returns>
        public static IPolygon geojson2esriPolygon(datacontract.geojsonPolygon JSPolygon, int epsg = -1)
        {
            Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
            System.Object obj = Activator.CreateInstance(factoryType);
            ISpatialReferenceFactory3 spatialReferenceFactory = obj as ISpatialReferenceFactory3;

            IGeometryBridge2 pGeoBrg = new GeometryEnvironment() as IGeometryBridge2;

            IGeometryCollection esriGeometryCol = new PolygonClass();

            for (int n = 0; n < JSPolygon.coordinates.Count; n++)
            {
                List<List<double>> JSring = JSPolygon.coordinates[n];
                IPointCollection4 ring = new RingClass();
                ESRI.ArcGIS.esriSystem.WKSPoint[] aWKSPointBuffer = new ESRI.ArcGIS.esriSystem.WKSPoint[JSring.Count];
                for (int i = 0; i < JSring.Count; i++)
                {
                    double[] xy = JSring[i].ToArray();
                    aWKSPointBuffer[i].X = xy[0];
                    aWKSPointBuffer[i].Y = xy[1];
                }
                pGeoBrg.SetWKSPoints(ring , aWKSPointBuffer);
                esriGeometryCol.AddGeometry(ring as IGeometry, Type.Missing, Type.Missing);
            }

            IPolygon esriPolygon = esriGeometryCol as IPolygon;

            if (epsg != -1)
            {
                ISpatialReference srs = spatialReferenceFactory.CreateSpatialReference(epsg);
                esriPolygon.SpatialReference = srs;
            }
            return esriPolygon;
        }
Exemple #3
0
        private static void CreateFeatures(List<GdbFromKmlFeature> gdbFeaturesFromKML)
        {
            License lic = new License();
            IWorkspaceEdit wse = null;
            try
            {
                bool ok = lic.GetLicenses();
                IFeatureWorkspace fws = (IFeatureWorkspace)GetWorkspace(); // cast this as an IWorkspace
                wse = (IWorkspaceEdit)fws;
                wse.StartEditing(false);
                wse.StartEditOperation();
                IFeatureClass waypointFC = fws.OpenFeatureClass("WAYPOINT");
                IFeatureClass trailFC = fws.OpenFeatureClass("TRAIL");
                ISpatialReference spatRef = ((IGeoDataset)waypointFC.FeatureDataset).SpatialReference;
                int counter = 0;
                foreach (GdbFromKmlFeature gdbFe in gdbFeaturesFromKML)
                {

                    if (gdbFe.IsPoint)
                    {
                        /*var newWayPoint = waypointFC.CreateFeature();
                        newWayPoint.set_Value(newWayPoint.Fields.FindField("NAME"), gdbFe.Name);
                        newWayPoint.set_Value(newWayPoint.Fields.FindField("SHORTDESCRIPTION"), gdbFe.Description);
                        newWayPoint.set_Value(newWayPoint.Fields.FindField("SUBTYPE"), 4);
                        double lat = Convert.ToDouble(gdbFe.Coordinates[0][1]);
                        double lon = Convert.ToDouble(gdbFe.Coordinates[0][0]);
                        double elev = Convert.ToDouble(gdbFe.Coordinates[0][2]);
                        newWayPoint.set_Value(newWayPoint.Fields.FindField("LATITUDE"), lat);
                        newWayPoint.set_Value(newWayPoint.Fields.FindField("LONGITUDE"), lon);
                        newWayPoint.set_Value(newWayPoint.Fields.FindField("ELEVATION"), elev);
                        ToWebMercator(ref lon, ref lat);
                        IPoint pnt = new PointClass();
                        pnt.PutCoords(lon, lat);
                        pnt.SpatialReference = spatRef;
                        newWayPoint.Shape = pnt;
                        newWayPoint.Store();*/
                    }
                    else
                    {
                        Console.WriteLine(counter);
                        var newTrail = trailFC.CreateFeature();
                        newTrail.set_Value(newTrail.Fields.FindField("SEGMENT"), gdbFe.Name);
                        //newTrail.set_Value(newTrail.Fields.FindField("SHORTDESCRIPTION"), gdbFe.Description);
                        newTrail.set_Value(newTrail.Fields.FindField("SUBTYPE"), 1);
                        //Define shape
                        IGeometryBridge2 pGeoBrg = new GeometryEnvironment() as IGeometryBridge2;
                        IPointCollection4 pPointColl = new PolylineClass();
                        ((IGeometry)pPointColl).SpatialReference = spatRef;
                        int vertexCount = gdbFe.Coordinates.Count;
                        WKSPoint[] aWKSPointBuffer = new WKSPoint[vertexCount];
                        int i = 0;
                        foreach(string[] vertex in gdbFe.Coordinates)
                        {
                            double lat = Convert.ToDouble(vertex[1]);
                            double lon = Convert.ToDouble(vertex[0]);
                            double elev = Convert.ToDouble(vertex[2]);
                            ToWebMercator(ref lon, ref lat);
                            aWKSPointBuffer[i] = new WKSPoint() { X = lon, Y = lat };
                            i++;
                        }
                        pGeoBrg.SetWKSPoints(pPointColl, ref aWKSPointBuffer);
                        newTrail.Shape = (IGeometry) pPointColl;
                        newTrail.Store();

                    }
                    counter++;
                    if (counter % 100 == 0)
                    {
                        wse.StopEditOperation();
                        wse.StopEditing(true);
                        wse.StartEditing(false);
                        wse.StartEditOperation();
                    }
                }

            }
            finally
            {
                wse.StopEditOperation();
                wse.StopEditing(true);
                lic.ReleaseLicenses();
            }
        }
        /// <summary>Convert a GeoJSON Line geometry to Arcgis Geometry </summary>
        /// <param name="JSpoint">The deserialised GeoJson Object</param>
        /// <param name="epsg">The EPSG-code of the spatial reference, -1 is unknown</param>
        /// <returns>A Arcgis PolyLine goemetry</returns>
        public static IPolyline geojson2esriLine(datacontract.geojsonLine JSline, int epsg = -1)
        {
            Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
            System.Object obj = Activator.CreateInstance(factoryType);
            ISpatialReferenceFactory3 spatialReferenceFactory = obj as ISpatialReferenceFactory3;

            IGeometryBridge2 pGeoBrg = new GeometryEnvironment() as IGeometryBridge2;

            ESRI.ArcGIS.esriSystem.WKSPoint[] aWKSPointBuffer = new ESRI.ArcGIS.esriSystem.WKSPoint[ JSline.coordinates.Count ];
            for (int n = 0; n < JSline.coordinates.Count; n++)
            {
                double[] xy = JSline.coordinates[n].ToArray();
                aWKSPointBuffer[n].X = xy[0];
                aWKSPointBuffer[n].Y = xy[1];
            }

            IPolyline esriLine = new PolylineClass();
            pGeoBrg.SetWKSPoints(esriLine as IPointCollection4, aWKSPointBuffer);

            if (epsg != -1)
            {
                ISpatialReference srs = spatialReferenceFactory.CreateSpatialReference(epsg);
                esriLine.SpatialReference = srs;
            }

            return esriLine;
        }