Ejemplo n.º 1
0
 private SpatialEnvelope calculateEnvelope(Geometry geom)
 {
     Envelope env = new Envelope();
     geom.GetEnvelope(env);
     SpatialEnvelope ogrenv = new SpatialEnvelope(env.MinX, env.MinY, env.MaxX, env.MaxY);
     return ogrenv;
 }
Ejemplo n.º 2
0
 public static Geometry BuildPolygonFromEdges(Geometry hLineCollection, int bBestEffort, int bAutoClose, double dfTolerance)
 {
     IntPtr cPtr = OgrPINVOKE.BuildPolygonFromEdges(Geometry.getCPtr(hLineCollection), bBestEffort, bAutoClose, dfTolerance);
     Geometry ret = (cPtr == IntPtr.Zero) ? null : new Geometry(cPtr, true, ThisOwn_true());
     if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Ejemplo n.º 3
0
 public static HandleRef getCPtrAndSetReference(Geometry obj, object parent) {
   if (obj != null)
   {
     obj.swigParentRef = parent;
     return obj.swigCPtr;
   }
   else
   {
     return new HandleRef(null, IntPtr.Zero);
   }
 }
Ejemplo n.º 4
0
 public static HandleRef getCPtrAndDisown(Geometry obj, object parent) {
   if (obj != null)
   {
     obj.swigCMemOwn = false;
     obj.swigParentRef = parent;
     return obj.swigCPtr;
   }
   else
   {
     return new HandleRef(null, IntPtr.Zero);
   }
 }
Ejemplo n.º 5
0
        public static OSGeo.OGR.Geometry ToGeometry(this Envelope envelope)
        {
            var ring = new OSGeo.OGR.Geometry(wkbGeometryType.wkbLinearRing);

            ring.AddPoint_2D(envelope.MinX, envelope.MinY);
            ring.AddPoint_2D(envelope.MaxX, envelope.MinY);
            ring.AddPoint_2D(envelope.MaxX, envelope.MaxY);
            ring.AddPoint_2D(envelope.MinX, envelope.MaxY);
            ring.CloseRings();
            var polygon = new OSGeo.OGR.Geometry(wkbGeometryType.wkbPolygon);

            polygon.AddGeometry(ring);
            return(polygon);
        }
Ejemplo n.º 6
0
 public static Geometry CreateFromWkb(byte[] wkb)
 {
     if (wkb.Length == 0)
     throw new ArgumentException("Buffer size is small (CreateFromWkb)");
      Geometry retval;
      IntPtr ptr = Marshal.AllocHGlobal(wkb.Length * Marshal.SizeOf(wkb[0]));
      try {
      Marshal.Copy(wkb, 0, ptr, wkb.Length);
      retval =  new Geometry(wkbGeometryType.wkbUnknown, null, wkb.Length, ptr, null);
       } finally {
       Marshal.FreeHGlobal(ptr);
       }
       return retval;
 }
Ejemplo n.º 7
0
        public static OSGeo.OGR.Geometry CurveToOgrRing(Curve curve)
        {
            Polyline pL = new Polyline();

            curve.TryGetPolyline(out pL);
            OSGeo.OGR.Geometry ring = new OSGeo.OGR.Geometry(wkbGeometryType.wkbLinearRing);

            foreach (Point3d pt in pL)
            {
                ring.AddPoint(pt.X, pt.Y, pt.Z);
            }

            return(ring);
        }
Ejemplo n.º 8
0
        public static Curve OgrLinestringToCurve(OSGeo.OGR.Geometry linestring, Transform transform)
        {
            List <Point3d> ptList = new List <Point3d>();

            for (int i = 0; i < linestring.GetPointCount(); i++)
            {
                Point3d pt = new Point3d(linestring.GetX(i), linestring.GetY(i), linestring.GetZ(i));
                pt.Transform(transform);
                ptList.Add(pt);
            }
            Polyline pL = new Polyline(ptList);

            return(pL.ToNurbsCurve());
        }
Ejemplo n.º 9
0
        public static List <Curve> OgrMultiLinestringToCurves(OSGeo.OGR.Geometry multilinestring, Transform transform)
        {
            List <Curve> cList = new List <Curve>();

            OSGeo.OGR.Geometry sub_geom;

            for (int i = 0; i < multilinestring.GetGeometryCount(); i++)
            {
                sub_geom = multilinestring.GetGeometryRef(i);
                cList.Add(Heron.Convert.OgrLinestringToCurve(sub_geom, transform));
                sub_geom.Dispose();
            }
            return(cList);
        }
Ejemplo n.º 10
0
        public static List <Point3d> OgrMultiPointToPoint3d(OSGeo.OGR.Geometry ogrMultiPoint)
        {
            List <Point3d> ptList = new List <Point3d>();

            OSGeo.OGR.Geometry sub_geom;
            for (int i = 0; i < ogrMultiPoint.GetGeometryCount(); i++)
            {
                sub_geom = ogrMultiPoint.GetGeometryRef(i);
                for (int ptnum = 0; ptnum < sub_geom.GetPointCount(); ptnum++)
                {
                    ptList.Add(Convert.ToXYZ(new Point3d(sub_geom.GetX(0), sub_geom.GetY(0), sub_geom.GetZ(0))));
                }
            }
            return(ptList);
        }
Ejemplo n.º 11
0
        public static OSGeo.OGR.Geometry CurveToOgrLinestring(Curve curve, Transform transform)
        {
            Polyline pL = new Polyline();

            curve.TryGetPolyline(out pL);
            OSGeo.OGR.Geometry linestring = new OSGeo.OGR.Geometry(wkbGeometryType.wkbLineString25D);

            foreach (Point3d pt in pL)
            {
                pt.Transform(transform);
                linestring.AddPoint(pt.X, pt.Y, pt.Z);
            }

            return(linestring);
        }
Ejemplo n.º 12
0
        public static List <Extrusion> OgrMultiPolyToExtrusions(OSGeo.OGR.Geometry multipoly, Transform transform, double height, double min_height, bool underground)
        {
            OSGeo.OGR.Geometry sub_geom;
            List <Extrusion>   eList = new List <Extrusion>();

            for (int i = 0; i < multipoly.GetGeometryCount(); i++)
            {
                sub_geom = multipoly.GetGeometryRef(i);
                Extrusion mP = Heron.Convert.OgrPolygonToExtrusion(sub_geom, transform, height, min_height, underground);
                eList.Add(mP);
                sub_geom.Dispose();
            }

            return(eList);
        }
Ejemplo n.º 13
0
        public static List <Point3d> OgrMultiPointToPoint3d(OSGeo.OGR.Geometry ogrMultiPoint, Transform transform)
        {
            List <Point3d> ptList = new List <Point3d>();

            OSGeo.OGR.Geometry sub_geom;
            for (int i = 0; i < ogrMultiPoint.GetGeometryCount(); i++)
            {
                sub_geom = ogrMultiPoint.GetGeometryRef(i);
                for (int ptnum = 0; ptnum < sub_geom.GetPointCount(); ptnum++)
                {
                    ptList.Add(Heron.Convert.OgrPointToPoint3d(sub_geom, transform));
                }
            }
            return(ptList);
        }
Ejemplo n.º 14
0
        public static OSGeo.OGR.Geometry MeshesToMultiPolygon(List <Mesh> meshes, Transform transform)
        {
            //OSGeo.OGR.Geometry ogrMultiPolygon = new OSGeo.OGR.Geometry(wkbGeometryType.wkbMultiPolygon25D);
            OSGeo.OGR.Geometry ogrMultiPolygon = new OSGeo.OGR.Geometry(wkbGeometryType.wkbMultiSurfaceZ);

            Mesh m = new Mesh();

            foreach (var mesh in meshes)
            {
                m.Append(mesh);
                //ogrMultiPolygon.AddGeometry(Heron.Convert.MeshToMultiPolygon(mesh, transform));
            }
            ogrMultiPolygon.AddGeometry(Heron.Convert.MeshToMultiPolygon(m, transform));

            return(ogrMultiPolygon);
        }
Ejemplo n.º 15
0
        public static void selectFromPoint(string point, string slopPoly)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver     dr        = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
            OSGeo.OGR.DataSource poiDS     = dr.Open(point, 0);
            OSGeo.OGR.DataSource slopDS    = dr.Open(slopPoly, 1);
            OSGeo.OGR.Layer      poiLayer  = poiDS.GetLayerByIndex(0);
            OSGeo.OGR.Layer      slopLayer = slopDS.GetLayerByIndex(0);

            List <int> staIds    = new List <int>();
            int        poiCount  = WorkFlow.pointIds.Count;
            int        slopCount = slopLayer.GetFeatureCount(0);

            for (int i = 0; i < poiCount; i++)
            {
                OSGeo.OGR.Feature  poiFeat = poiLayer.GetFeature(WorkFlow.pointIds[i]);
                OSGeo.OGR.Geometry poiGeom = poiFeat.GetGeometryRef();
                for (int t = 0; t < slopCount; t++)
                {
                    OSGeo.OGR.Feature  slopFeat = slopLayer.GetFeature(t);
                    OSGeo.OGR.Geometry slopGeom = slopFeat.GetGeometryRef();
                    if (poiGeom.Within(slopGeom))
                    {
                        staIds.Add(t);
                        WorkFlow.pointIds[i] = -1;
                    }
                    slopFeat.Dispose();
                }
                poiFeat.Dispose();
            }
            Console.WriteLine("【本次提取到{0}个要素】", staIds.Count);
            while (WorkFlow.pointIds.IndexOf(-1) > -1)
            {
                WorkFlow.pointIds.Remove(-1);
            }

            for (int i = 0; i < slopCount; i++)
            {
                if (staIds.IndexOf(i) == -1)
                {
                    slopLayer.DeleteFeature(i);
                }
            }
            slopDS.deleteFeatUpdate();
            slopDS.Dispose();
            poiDS.Dispose();
        }
Ejemplo n.º 16
0
        public static Mesh OgrPolygonToMesh(OSGeo.OGR.Geometry polygon, Transform transform)
        {
            double       tol   = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance;
            List <Curve> pList = new List <Curve>();

            OSGeo.OGR.Geometry sub_geom;

            for (int i = 0; i < polygon.GetGeometryCount(); i++)
            {
                sub_geom = polygon.GetGeometryRef(i);
                Curve crv = Heron.Convert.OgrRingToCurve(sub_geom, transform);
                //possible cause of viewport issue, try not forcing a close.  Other possibility would be trying to convert to (back to) polyline
                //crv.MakeClosed(tol);

                if (!crv.IsClosed && sub_geom.GetPointCount() > 2)
                {
                    Curve   closingLine = new Line(crv.PointAtEnd, crv.PointAtStart).ToNurbsCurve();
                    Curve[] result      = Curve.JoinCurves(new Curve[] { crv, closingLine });
                    crv = result[0];
                }

                pList.Add(crv);
                sub_geom.Dispose();
            }

            //need to catch if not closed polylines
            Mesh mPatch = new Mesh();

            if (pList[0] != null && pList[0].IsClosed)
            {
                Polyline pL = null;
                pList[0].TryGetPolyline(out pL);
                pList.RemoveAt(0);
                mPatch = Rhino.Geometry.Mesh.CreatePatch(pL, tol, null, pList, null, null, true, 1);

                ///Adds ngon capability
                ///https://discourse.mcneel.com/t/create-ngon-mesh-rhinocommon-c/51796/12
                mPatch.Ngons.AddPlanarNgons(tol);
                mPatch.FaceNormals.ComputeFaceNormals();
                mPatch.Normals.ComputeNormals();
                mPatch.Compact();
                mPatch.UnifyNormals();
            }

            return(mPatch);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// 清理原文件
        /// </summary>
        /// <param name="filePath"></param>
        public static void claenPoint(string filePath, double jiaodu, int cishu)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver     dr       = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
            OSGeo.OGR.DataSource oriDs    = dr.Open(filePath, 1);
            OSGeo.OGR.Layer      oriLayer = oriDs.GetLayerByIndex(0);

            int featCount = oriLayer.GetFeatureCount(0);

            for (int i = 0; i < featCount; i++)
            {
                OSGeo.OGR.Feature  oriFeat = oriLayer.GetFeature(i);
                OSGeo.OGR.Geometry oriGeom = oriFeat.GetGeometryRef();
                OSGeo.OGR.Geometry subGeom = oriGeom.GetGeometryRef(0);

                int pointCount = subGeom.GetPointCount();

                Point[] aFeat = new Point[pointCount];

                for (int c = 0; c < pointCount; c++)
                {
                    aFeat[c].X = subGeom.GetX(c);
                    aFeat[c].Y = subGeom.GetY(c);
                    aFeat[c].Z = subGeom.GetZ(c);
                }

                OSGeo.OGR.Geometry newGeom = null;
                if (aFeat.Length > cishu * 3)
                {
                    newGeom = JID(aFeat, jiaodu, cishu);
                }
                else
                {
                    oriFeat.Dispose();
                    continue;
                }
                if (newGeom != null)
                {
                    oriFeat.SetGeometry(newGeom);
                    oriLayer.SetFeature(oriFeat);
                }
                Console.WriteLine("cleanPoint已完成{0}/{1}", i, featCount);
                oriFeat.Dispose();
            }
            oriDs.Dispose();
        }
Ejemplo n.º 18
0
 public static OSGeo.OGR.Geometry ToOgrPolygon(this IEnumerable <IEnumerable <ICoordinate> > ringList)
 {
     OSGeo.OGR.Geometry geometry = null;
     if (ringList?.Any() == true)
     {
         geometry = new OSGeo.OGR.Geometry(wkbGeometryType.wkbPolygon);
         foreach (var ring in ringList)
         {
             var ogrRing = GetOgrGeometry(wkbGeometryType.wkbLinearRing, ring);
             if (ogrRing == null)
             {
                 return(null);
             }
             geometry.AddGeometry(ogrRing);
         }
     }
     return(geometry);
 }
Ejemplo n.º 19
0
        public static OSGeo.OGR.Geometry MeshToMultiPolygon(Mesh mesh, Transform transform)
        {
            //OSGeo.OGR.Geometry ogrMultiPolygon = new OSGeo.OGR.Geometry(wkbGeometryType.wkbMultiPolygon25D);
            OSGeo.OGR.Geometry ogrMultiPolygon = new OSGeo.OGR.Geometry(wkbGeometryType.wkbMultiSurfaceZ);


            foreach (var face in mesh.GetNgonAndFacesEnumerable())
            {
                Polyline pL = new Polyline();
                foreach (var index in face.BoundaryVertexIndexList())
                {
                    pL.Add(mesh.Vertices.Point3dAt(System.Convert.ToInt32(index)));
                }

                ogrMultiPolygon.AddGeometry(Heron.Convert.CurveToOgrPolygon(pL.ToNurbsCurve(), transform));
            }
            return(ogrMultiPolygon);
        }
Ejemplo n.º 20
0
        public static Curve OgrRingToCurve(OSGeo.OGR.Geometry ring, Transform transform)
        {
            double         tol    = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance;
            List <Point3d> ptList = new List <Point3d>();

            for (int i = 0; i < ring.GetPointCount(); i++)
            {
                Point3d pt = new Point3d(ring.GetX(i), ring.GetY(i), ring.GetZ(i));
                pt.Transform(transform);
                ptList.Add(pt);
            }
            //ptList.Add(ptList[0]);
            Polyline pL  = new Polyline(ptList);
            Curve    crv = pL.ToNurbsCurve();

            //crv.MakeClosed(tol);
            return(crv);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// 获取简单多边形
        /// </summary>
        /// <param name="geo"></param>
        /// <returns></returns>
        private static SimplePolygon GetSimplePolygon(OSGeo.OGR.Geometry geometry)
        {
            List <SimplePolyline> rings = new List <SimplePolyline>();
            int ringCount = geometry.GetGeometryCount();//子图形对象数目

            for (int i = 0; i < ringCount; ++i)
            {
                List <PointD> pointlist = new List <PointD>();
                //获取第i个子对象
                OSGeo.OGR.Geometry ring = geometry.GetGeometryRef(i);
                int pointcount2         = ring.GetPointCount();
                for (int k = 0; k < pointcount2; k++)
                {
                    pointlist.Add(new PointD(ring.GetX(k), ring.GetY(k)));
                }
                rings.Add(new SimplePolyline(pointlist));
            }
            return(new SimplePolygon(rings));
        }
Ejemplo n.º 22
0
 public ShpEditor(string outputFileName)
 {
     _outputFileName = outputFileName;
     //点
     OSGeo.OGR.Geometry point = new OSGeo.OGR.Geometry(wkbGeometryType.wkbPoint);
     //线
     OSGeo.OGR.Geometry linepoint = new OSGeo.OGR.Geometry(wkbGeometryType.wkbMultiLineString);
     //圆
     OSGeo.OGR.Geometry circlepoint = new OSGeo.OGR.Geometry(wkbGeometryType.wkbLinearRing);
     //椭圆
     OSGeo.OGR.Geometry arcpoint = new OSGeo.OGR.Geometry(wkbGeometryType.wkbMultiPoint);
     //圆弧
     // 为了支持中文路径,请添加下面这句代码
     OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
     // 为了使属性表字段支持中文,请添加下面这句
     OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
     // 注册所有的驱动
     Ogr.RegisterAll();
 }
Ejemplo n.º 23
0
        /// <summary>
        /// 获取复杂线
        /// </summary>
        /// <param name="geometry"></param>
        /// <returns></returns>
        private static Polyline GetPolyline(OSGeo.OGR.Geometry geometry)
        {
            List <SimplePolyline> lines = new List <SimplePolyline>();
            int lineCount = geometry.GetGeometryCount();    //子图形对象数目

            for (int i = 0; i < lineCount; ++i)
            {
                List <PointD> line = new List <PointD>();
                //获取第i个子对象
                OSGeo.OGR.Geometry sline = geometry.GetGeometryRef(i);
                int pointcount2          = sline.GetPointCount();
                for (int j = 0; j < pointcount2; ++j)
                {
                    line.Add(new PointD(sline.GetX(j), sline.GetY(j)));
                }
                lines.Add(new SimplePolyline(line));
            }
            return(new Polyline(lines));
        }
Ejemplo n.º 24
0
        public static OSGeo.OGR.Geometry CurveToOgrRing(Curve curve, Transform transform)
        {
            Polyline pL = new Polyline();

            curve.TryGetPolyline(out pL);
            OSGeo.OGR.Geometry ring = new OSGeo.OGR.Geometry(wkbGeometryType.wkbLinearRing);

            if (pL[0] != pL[pL.Count - 1])
            {
                pL.Add(pL[0]);
            }

            foreach (Point3d pt in pL)
            {
                pt.Transform(transform);
                ring.AddPoint(pt.X, pt.Y, pt.Z);
            }

            return(ring);
        }
Ejemplo n.º 25
0
 public static OSGeo.OGR.Geometry ToOgrPoint(double x, double y, double z = double.NaN, double m = double.NaN)
 {
     OSGeo.OGR.Geometry ogrGeometry = new OSGeo.OGR.Geometry(wkbGeometryType.wkbPoint);
     if (double.IsNaN(z))
     {
         ogrGeometry.AddPoint_2D(x, y);
     }
     else
     {
         if (double.IsNaN(m))
         {
             ogrGeometry.AddPoint(x, y, z);
         }
         else
         {
             ogrGeometry.AddPointZM(x, y, z, m);
         }
     }
     return(ogrGeometry);
 }
Ejemplo n.º 26
0
        private byte[] GetGeometry()
        {
            Geometry ogrGeometry = _currentFeature.GetGeometryRef();

            ogrGeometry.FlattenTo2D();
            Byte[] wkbGeometry = new Byte[ogrGeometry.WkbSize()];

            ogrGeometry.ExportToWkb(wkbGeometry, wkbByteOrder.wkbXDR);

            return(wkbGeometry);

            ////            System.Data.SqlTypes.SqlBytes x = new System.Data.SqlTypes.SqlBytes(wkbGeometry);
            ////            Microsoft.SqlServer.Types.SqlGeometry c = Microsoft.SqlServer.Types.SqlGeometry.STGeomFromWKB(x, 1111);
            ////             Microsoft.SqlServer.Types.SqlGeometry q = c.STExteriorRing();
            ////            MemoryStream ms = new MemoryStream( wkbGeometry );
            ////            BinaryReader rdr = new BinaryReader(ms);
            ////            Microsoft.SqlServer.Types.SqlGeometry g = new Microsoft.SqlServer.Types.SqlGeometry();
            ////            g.Read( rdr );
            //return geometry;
        }
Ejemplo n.º 27
0
        public static void selectDZXFromPoint(string point, string dzx, string savePath)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Driver     dr    = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
            OSGeo.OGR.DataSource poiDS = dr.Open(point, 0);
            OSGeo.OGR.DataSource dzxDS = dr.Open(dzx, 0);
            if (File.Exists(savePath))
            {
                File.Delete(savePath);
            }
            OSGeo.OGR.DataSource newDS    = dr.CreateDataSource(savePath, null);
            OSGeo.OGR.Layer      poiLayer = poiDS.GetLayerByIndex(0);
            OSGeo.OGR.Layer      dzxLayer = dzxDS.GetLayerByIndex(0);
            OSGeo.OGR.Layer      newLayer = newDS.CreateLayer("", dzxLayer.GetSpatialRef(), dzxLayer.GetGeomType(), null);

            int poiCount = poiLayer.GetFeatureCount(0);
            int dzxCount = dzxLayer.GetFeatureCount(0);

            for (int i = 0; i < poiCount; i++)
            {
                OSGeo.OGR.Feature  poiFeat = poiLayer.GetFeature(i);
                OSGeo.OGR.Geometry poiGeom = poiFeat.GetGeometryRef();
                for (int t = 0; t < dzxCount; t++)
                {
                    OSGeo.OGR.Feature  dzxFeat = dzxLayer.GetFeature(t);
                    OSGeo.OGR.Geometry dzxGeom = dzxFeat.GetGeometryRef();

                    if (poiGeom.Within(dzxGeom))
                    {
                        newLayer.CreateFeature(dzxFeat);
                    }
                    dzxFeat.Dispose();
                    Console.WriteLine("getFeatureByPoint:{0}/{1}", i, poiCount);
                }
                poiFeat.Dispose();
            }
            Console.WriteLine("【本次提取到{0}个要素】", newLayer.GetFeatureCount(0));
            newDS.Dispose();
            dzxDS.Dispose();
            poiDS.Dispose();
        }
Ejemplo n.º 28
0
 public static void GetPointCount(this OSGeo.OGR.Geometry geometry, ref int count)
 {
     if (geometry != null)
     {
         int geoCount = geometry.GetGeometryCount();
         if (geoCount > 0)
         {
             for (int i = 0; i < geoCount; i++)
             {
                 using (var childGeo = geometry.GetGeometryRef(i))
                 {
                     childGeo.GetPointCount(ref count);
                 }
             }
         }
         else
         {
             count += geometry.GetPointCount();
         }
     }
 }
Ejemplo n.º 29
0
        public static Mesh OgrMultiPolyToMesh(OSGeo.OGR.Geometry multipoly, Transform transform)
        {
            OSGeo.OGR.Geometry sub_geom;
            List <Mesh>        mList = new List <Mesh>();

            for (int i = 0; i < multipoly.GetGeometryCount(); i++)
            {
                sub_geom = multipoly.GetGeometryRef(i);
                Mesh mP = Heron.Convert.OgrPolygonToMesh(sub_geom, transform);
                mP.UnifyNormals();
                mList.Add(mP);
                sub_geom.Dispose();
            }
            Mesh m = new Mesh();

            m.Append(mList);
            m.RebuildNormals();
            m.UnifyNormals();

            if (m.DisjointMeshCount > 0)
            {
                Mesh[] mDis = m.SplitDisjointPieces();
                Mesh   mm   = new Mesh();
                foreach (Mesh mPiece in mDis)
                {
                    if (mPiece.SolidOrientation() < 0)
                    {
                        mPiece.Flip(false, false, true);
                    }
                    mm.Append(mPiece);
                }
                mm.RebuildNormals();
                mm.UnifyNormals();
                return(mm);
            }
            else
            {
                return(m);
            }
        }
Ejemplo n.º 30
0
        public static List <int> _平角点ID集(Feature aFeatuer, double userSet, int seleTime)
        {
            OSGeo.OGR.Geometry oriGeom = aFeatuer.GetGeometryRef();
            OSGeo.OGR.Geometry subGeom = oriGeom.GetGeometryRef(0);
            int pointCount             = subGeom.GetPointCount();

            Point[] pointList = new Point[pointCount];
            for (int c = 0; c < pointCount; c++)
            {
                pointList[c].X = subGeom.GetX(c);
                pointList[c].Y = subGeom.GetY(c);
                pointList[c].Z = subGeom.GetZ(c);
            }
            if (pointList.Length < seleTime * 3)
            {
                MessageBox.Show("这个图形点数小于重复次数~试试降低重复次数~或删除");
                return(null);
            }
            List <int> ids = new List <int>();

            for (int i = 0; i < pointList.Length; i++)
            {
                int    frontId, thisId, backId;
                bool[] yon = new bool[seleTime];
                for (int t = 1; t <= seleTime; t++)
                {
                    frontId = i < t ? pointList.Length - 1 + i - t : i - t;
                    thisId  = i;
                    backId  = i > pointList.Length - 1 - t ? i - (pointList.Length - 1) + t : i + t;
                    double jiaodu = cosCalculator(pointList[frontId], pointList[thisId], pointList[backId]);
                    yon[t - 1] = jiaodu > userSet;
                }
                if (yon.Contains(true))
                {
                    ids.Add(i);
                }
            }
            return(ids);
        }
Ejemplo n.º 31
0
        public static Mesh OgrMultiPolyToMesh(OSGeo.OGR.Geometry multipoly)
        {
            double tol = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance;

            OSGeo.OGR.Geometry sub_geom;
            List <Mesh>        mList = new List <Mesh>();

            for (int i = 0; i < multipoly.GetGeometryCount(); i++)
            {
                sub_geom = multipoly.GetGeometryRef(i);
                Mesh mP = Convert.OgrPolygonToMesh(sub_geom);
                mP.UnifyNormals();
                mList.Add(mP);
                sub_geom.Dispose();
            }
            Mesh m = new Mesh();

            m.Append(mList);
            m.RebuildNormals();
            m.UnifyNormals();

            if (m.DisjointMeshCount > 0)
            {
                Mesh[] mDis = m.SplitDisjointPieces();
                Mesh   mm   = new Mesh();
                foreach (Mesh mPiece in mDis)
                {
                    if (mPiece.SolidOrientation() < 0)
                    {
                        mPiece.Flip(false, false, true);
                    }
                    mm.Append(mPiece);
                }
                mm.RebuildNormals();
                mm.UnifyNormals();
                return(mm);
            }
            return(m);
        }
Ejemplo n.º 32
0
        public static void SetCoordinate(this OSGeo.OGR.Geometry geometry, int index, ICoordinate coordinate)
        {
            if (geometry != null && coordinate != null)
            {
                if (index >= 0 && index < geometry.GetPointCount())
                {
                    int dimension = geometry.GetCoordinateDimension();
                    switch (dimension)
                    {
                    case 2:
                        geometry.SetPoint_2D(index, coordinate.X, coordinate.Y);
                        break;

                    case 3:
                        geometry.SetPoint(index, coordinate.X, coordinate.Y, coordinate.Z);
                        break;

                    case 4:
                        geometry.SetPointZM(index, coordinate.X, coordinate.Y, coordinate.Z, coordinate.M);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 33
0
        /************************************* 判断两个Featuer是否重复 *************************************************/
        /// <summary>
        /// 判断两个Featuer是否重复,ori 当前Feat,next 目标Feat
        /// </summary>
        /// <param name="ori"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public static bool isSame(OSGeo.OGR.Feature ori, OSGeo.OGR.Feature next, double fanWei = 0.1)
        {
            OSGeo.OGR.Ogr.RegisterAll();
            OSGeo.OGR.Geometry oriGeom = ori.GetGeometryRef();
            OSGeo.OGR.Envelope oriEnve = new OSGeo.OGR.Envelope();
            oriGeom.GetEnvelope(oriEnve);
            OSGeo.OGR.Geometry nextGeom = next.GetGeometryRef();
            OSGeo.OGR.Envelope nextEnve = new OSGeo.OGR.Envelope();
            nextGeom.GetEnvelope(nextEnve);
            double oriArea  = oriGeom.GetArea();
            double nextArea = nextGeom.GetArea();
            bool   res      =
                Math.Abs(oriEnve.MaxX - nextEnve.MaxX) < fanWei && //外接矩形差
                Math.Abs(oriEnve.MaxY - nextEnve.MaxY) < fanWei &&
                Math.Abs(oriEnve.MinX - nextEnve.MinX) < fanWei &&
                Math.Abs(oriEnve.MinY - nextEnve.MinY) < fanWei;

            //面积?    && Math.Abs(oriArea - nextArea) < 0.1;
            oriGeom.Dispose();
            oriEnve.Dispose();
            nextGeom.Dispose();
            nextEnve.Dispose();
            return(res);
        }
Ejemplo n.º 34
0
        static void WriteComponentGeometryFragment(JsonTextWriter writer, Geometry geom, string name, DatedElectionResult[] results, DatedTPPResult[] tppResults, int? no = null)
        {
            if (results.Length == 0)
                return;

            int number = no.HasValue ? no.Value : 0;
            double area = geom.Area();

            //To keep the CZML payload to only the main areas (and not islands), don't write CZML packets for
            //island parts
            if (area < MIN_AREA)
                return;

            //Debug.WriteLine($"{name}: {number} - {area}");

            //if (!_areas.ContainsKey(name))
            //    _areas[name] = area;
            //else
            //    _areas[name] = Math.Max(_areas[name], area);

            // {
            writer.WriteStartObject();
            {
                var ring = geom.GetGeometryRef(0);
                var ptCount = ring.GetPointCount();

                // "id": "Electorate/<electorate>/<number>"
                writer.WritePropertyName("id");
                writer.WriteValue($"Electorate/{name}/{number}");
                // "name": "<electorate>"
                writer.WritePropertyName("name");
                writer.WriteValue("Federal Electorate");
                // "description": "<electorate>"
                writer.WritePropertyName("description");
                writer.WriteValue(name);
                // "parent": "Electorate/<electorate>"
                writer.WritePropertyName("parent");
                writer.WriteValue($"Electorate/{name}");
                
                // "polygon": {
                writer.WritePropertyName("polygon");
                writer.WriteStartObject();
                {
                    // "outline" {
                    writer.WritePropertyName("outline");
                    writer.WriteStartObject();
                    {
                        writer.WritePropertyName("boolean");
                        writer.WriteValue(true);
                    }
                    writer.WriteEndObject(); // }

                    // "outlineColor" {
                    writer.WritePropertyName("outlineColor");
                    writer.WriteStartObject();
                    {
                        // "rgba": [
                        writer.WritePropertyName("rgba");
                        writer.WriteStartArray();
                        {
                            //Black
                            writer.WriteValue(0);
                            writer.WriteValue(0);
                            writer.WriteValue(0);
                            writer.WriteValue(255);
                        }
                        writer.WriteEndArray(); // ]
                    }
                    writer.WriteEndObject(); // }

                    // "material": {
                    writer.WritePropertyName("material");
                    writer.WriteStartObject();
                    {
                        // "solidColor": {
                        writer.WritePropertyName("solidColor");
                        writer.WriteStartObject();
                        {
                            // "color": {
                            writer.WritePropertyName("color");
                            writer.WriteStartObject();
                            {
                                // "epoch": <start>
                                writer.WritePropertyName("epoch");
                                writer.WriteValue(_sceneStart);

                                // "rgba": [
                                writer.WritePropertyName("rgba");
                                writer.WriteStartArray();
                                {
                                    //HACK: This example doesn't take into account that electorate redistribution occurs
                                    //Meaning that our 2015 snapshot of Australian Federal Electorates may contain electorates
                                    //that either have changed or did not exist in earlier Federal Elections. Since the AEC does
                                    //not publicly provide such data, we'll denote such electorates as unknown (White)
                                    //
                                    //So peek at the first result, if it's dated on our first Federal Election we know the
                                    //electorate existed back then
                                    bool bDidNotExistIn2004 = !(results[0].ElectionDate.Year == _dtStart.Year &&
                                        results[0].ElectionDate.Month == _dtStart.Month &&
                                        results[0].ElectionDate.Day == _dtStart.Day);

                                    if (bDidNotExistIn2004)
                                    {
                                        //0 seconds since epoch
                                        writer.WriteValue(0);
                                        //White
                                        writer.WriteValue(255);
                                        writer.WriteValue(255);
                                        writer.WriteValue(255);
                                        writer.WriteValue(COLOR_ALPHA);
                                    }
                                    else
                                    {
                                        //0 seconds since epoch
                                        writer.WriteValue(0);

                                        int[] color = GetPartyColor(results[0].PartyNm);
                                        foreach (var c in color)
                                        {
                                            writer.WriteValue(c);
                                        }
                                    }

                                    foreach (var result in results.Skip(1))
                                    {
                                        int dt = (int)result.ElectionDate.Subtract(_dtStart).TotalSeconds;
                                        writer.WriteValue(dt);
                                        int[] color = GetPartyColor(result.PartyNm);
                                        foreach (var c in color)
                                        {
                                            writer.WriteValue(c);
                                        }
                                    }
                                }
                                writer.WriteEndArray(); // ]
                            }
                            writer.WriteEndObject(); // }
                        }
                        writer.WriteEndObject(); // }
                    }
                    writer.WriteEndObject(); // }

                    // "extrudedHeight": {
                    /*
                    writer.WritePropertyName("extrudedHeight");
                    writer.WriteStartObject();
                    {
                        // "epoch": <start>
                        writer.WritePropertyName("epoch");
                        writer.WriteValue(_sceneStart);

                        // "number": [
                        writer.WritePropertyName("number");
                        writer.WriteStartArray();
                        {
                            //HACK: This example doesn't take into account that electorate redistribution occurs
                            //Meaning that our 2015 snapshot of Australian Federal Electorates may contain electorates
                            //that either have changed or did not exist in earlier Federal Elections. Since the AEC does
                            //not publicly provide such data, we'll denote such electorates as unknown (White)
                            //
                            //So peek at the first result, if it's dated on our first Federal Election we know the
                            //electorate existed back then
                            bool bDidNotExistIn2004 = !(tppResults[0].ElectionDate.Year == _dtStart.Year &&
                                tppResults[0].ElectionDate.Month == _dtStart.Month &&
                                tppResults[0].ElectionDate.Day == _dtStart.Day);

                            if (bDidNotExistIn2004)
                            {
                                //0 seconds since epoch
                                writer.WriteValue(0);
                                //0 height
                                writer.WriteValue(0);
                            }
                            else
                            {
                                //0 seconds since epoch
                                writer.WriteValue(0);
                                //The winning party's percentage
                                writer.WriteValue(Math.Max(tppResults[0].LaborPc, tppResults[0].CoalitionPc) * EXTRUSION_MULTIPLIER);
                            }

                            foreach (var tppResult in tppResults.Skip(1))
                            {
                                int dt = (int)tppResult.ElectionDate.Subtract(_dtStart).TotalSeconds;
                                writer.WriteValue(dt);
                                //The winning party's percentage
                                writer.WriteValue(Math.Max(tppResults[0].LaborPc, tppResults[0].CoalitionPc) * EXTRUSION_MULTIPLIER);
                            }
                        }
                        writer.WriteEndArray(); // ]
                    }
                    writer.WriteEndObject(); // }
                    */
                    // "positions": {
                    writer.WritePropertyName("positions");
                    writer.WriteStartObject();
                    {
                        // "cartographicDegrees": [
                        writer.WritePropertyName("cartographicDegrees");
                        writer.WriteStartArray();
                        {
                            double[] coords = new double[3];
                            foreach (int idx in Enumerable.Range(0, ptCount - 1))
                            {
                                ring.GetPoint(idx, coords);
                                foreach (double coord in coords)
                                {
                                    writer.WriteValue(coord);
                                }
                            }
                        }
                        writer.WriteEndArray(); // ]
                    }
                    writer.WriteEndObject(); // }
                }
                writer.WriteEndObject(); // }
            }
            writer.WriteEndObject(); // }
        }
Ejemplo n.º 35
0
        //将内存中一个图层的数据另存为在文件中
        private Boolean SaveAsToFile(String name, String path)
        {
            Ogr.RegisterAll();
            //to support chinese path
            OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
            //to support chinese field name
            OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
            string strDriverName = "ESRI Shapefile";
            Driver oDriver       = Ogr.GetDriverByName(strDriverName);

            if (oDriver == null)
            {
                //说明driver不可用
                return(false);
            }
            String pathPrefix  = System.IO.Path.GetDirectoryName(path);
            String pathPostfix = System.Guid.NewGuid().ToString();

            if (File.Exists(pathPrefix + "\\" + pathPostfix))
            {
                File.Delete(pathPrefix + "\\" + pathPostfix);
            }

            //create a datasource, if successfully, a shp file would be created
            DataSource oDs       = oDriver.CreateDataSource(pathPrefix + "\\" + pathPostfix, null);
            Schema     tmpSchema = this.featureSources[name].schema;
            Layer      layer     = oDs.CreateLayer(name, tmpSchema.rs.spetialReference, tmpSchema.geometryType, null);

            //insert all the fields to layer
            //use index instead of field name to avoid the error caused by chinese words
            int fieldcount = tmpSchema.fields.Count;

            for (int i = 0; i < fieldcount; ++i)
            {
                layer.CreateField(tmpSchema.fields.ElementAt(i).Value, 1);
            }
            FeatureDefn fdef = layer.GetLayerDefn();

            FeatureCollection fc = this.featureSources[name].features;
            int fcount           = fc.count;

            for (int i = 0; i < fcount; ++i)
            {
                GisSmartTools.Data.Feature tmpFeature = fc.featureList[i];
                if (!tmpFeature.visible)
                {
                    continue;
                }
                //create a new feature
                OSGeo.OGR.Feature newFeature = new OSGeo.OGR.Feature(fdef);

                //set attribute
                newFeature.SetFID(tmpFeature.featureID);
                for (int j = 0; j < fieldcount; ++j)
                {
                    String    fieldname = tmpSchema.fields.ElementAt(j).Key;
                    FieldType ft        = tmpSchema.fields[fieldname].GetFieldType();
                    try{
                        switch (ft)
                        {
                        case FieldType.OFTString:
                            newFeature.SetField(j, (String)tmpFeature.attributes[fieldname]);
                            break;

                        case FieldType.OFTInteger:
                            newFeature.SetField(j, (int)tmpFeature.attributes[fieldname]);
                            break;

                        case FieldType.OFTReal:
                            newFeature.SetField(j, (double)tmpFeature.attributes[fieldname]);
                            break;

                        case FieldType.OFTWideString:
                            newFeature.SetField(j, (String)tmpFeature.attributes[fieldname]);
                            break;

                        default:
                            newFeature.SetField(j, (String)tmpFeature.attributes[fieldname]);
                            break;
                        }
                    }catch (Exception e)
                    {
                    }
                }

                //get geometry
                OSGeo.OGR.wkbGeometryType featType = tmpFeature.geometry.geometryType;
                OSGeo.OGR.Geometry        geo      = new OSGeo.OGR.Geometry(featType);
                if (geo != null)
                {
                    switch (featType)
                    {
                    case OSGeo.OGR.wkbGeometryType.wkbPoint:
                        GisSmartTools.Geometry.PointD tmpPoint = (GisSmartTools.Geometry.PointD)tmpFeature.geometry;
                        geo.AddPoint(tmpPoint.X, tmpPoint.Y, 0.00);
                        break;

                    case OSGeo.OGR.wkbGeometryType.wkbLineString:
                        GisSmartTools.Geometry.SimplePolyline tmpLine = (GisSmartTools.Geometry.SimplePolyline)tmpFeature.geometry;
                        foreach (GisSmartTools.Geometry.PointD po in tmpLine.points)
                        {
                            geo.AddPoint(po.X, po.Y, 0.00);
                        }
                        break;

                    case wkbGeometryType.wkbMultiLineString:
                        GisSmartTools.Geometry.Polyline lines = (GisSmartTools.Geometry.Polyline)tmpFeature.geometry;
                        foreach (SimplePolyline line in lines.childPolylines)
                        {
                            OSGeo.OGR.Geometry tmpgeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLineString);
                            foreach (GisSmartTools.Geometry.PointD point in line.points)
                            {
                                tmpgeo.AddPoint(point.X, point.Y, 0.00);
                            }
                            geo.AddGeometryDirectly(tmpgeo);
                        }
                        break;

                    case OSGeo.OGR.wkbGeometryType.wkbPolygon:
                        GisSmartTools.Geometry.SimplePolygon gon = (GisSmartTools.Geometry.SimplePolygon)tmpFeature.geometry;
                        foreach (SimplePolyline ring7 in gon.rings)
                        {
                            OSGeo.OGR.Geometry tmpgeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
                            foreach (GisSmartTools.Geometry.PointD point in ring7.points)
                            {
                                tmpgeo.AddPoint(point.X, point.Y, 0.00);
                            }
                            geo.AddGeometryDirectly(tmpgeo);
                        }
                        break;

                    case wkbGeometryType.wkbMultiPolygon:
                        GisSmartTools.Geometry.Polygon gons = (GisSmartTools.Geometry.Polygon)tmpFeature.geometry;
                        foreach (GisSmartTools.Geometry.SimplePolygon cgon in gons.childPolygons)
                        {
                            OSGeo.OGR.Geometry geogon = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPolygon);
                            foreach (GisSmartTools.Geometry.SimplePolyline ring6 in cgon.rings)
                            {
                                OSGeo.OGR.Geometry geoline = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
                                foreach (GisSmartTools.Geometry.PointD point6 in ring6.points)
                                {
                                    geoline.AddPoint(point6.X, point6.Y, 0.00);
                                }
                                geogon.AddGeometryDirectly(geoline);
                            }
                            geo.AddGeometryDirectly(geogon);
                        }
                        break;

                    default:
                        break;
                    }
                }

                //set feature
                newFeature.SetGeometry(geo);
                //add to layer
                layer.CreateFeature(newFeature);
            }

            //call Dispose method to save to file
            oDs.Dispose();
            DataSource tds;

            if (this.iDses.TryGetValue(path, out tds))
            {
                tds.Dispose();
                this.iDses.Remove(path);
            }

            //enumerate all the files in the temp file directory and move them to the output directory
            string[] tmpName = System.IO.Directory.GetFiles(pathPrefix + "\\" + pathPostfix);
            foreach (string file in tmpName)
            {
                string decFile = pathPrefix + "\\" + Path.GetFileName(file);
                if (File.Exists(decFile))
                {
                    File.Delete(decFile);
                }
                File.Move(file, decFile);
            }
            Directory.Delete(pathPrefix + "\\" + pathPostfix, true);
            return(true);
        }
Ejemplo n.º 36
0
 public static HandleRef getCPtr(Geometry obj)
 {
     return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
Ejemplo n.º 37
0
 private static IGeometry ParseOgrGeometry(Geometry ogrGeometry)
 {
     var wkbBuffer = new byte[ogrGeometry.WkbSize()];
     int i = ogrGeometry.ExportToWkb(wkbBuffer);
     return GeometryFromWKB.Parse(wkbBuffer);
 }
Ejemplo n.º 38
0
 public Geometry Union(Geometry other)
 {
     IntPtr cPtr = OgrPINVOKE.Geometry_Union(swigCPtr, Geometry.getCPtr(other));
     Geometry ret = (cPtr == IntPtr.Zero) ? null : new Geometry(cPtr, true, ThisOwn_true());
     if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Ejemplo n.º 39
0
 public bool Within(Geometry other)
 {
     bool ret = OgrPINVOKE.Geometry_Within(swigCPtr, Geometry.getCPtr(other));
     if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Ejemplo n.º 40
0
 public double Distance(Geometry other)
 {
     double ret = OgrPINVOKE.Geometry_Distance(swigCPtr, Geometry.getCPtr(other));
     if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Ejemplo n.º 41
0
 public static Geometry ForceTo(Geometry geom_in, wkbGeometryType eTargetType, string[] options)
 {
     IntPtr cPtr = OgrPINVOKE.ForceTo(Geometry.getCPtr(geom_in), (int)eTargetType, (options != null)? new OgrPINVOKE.StringListMarshal(options)._ar : null);
     Geometry ret = (cPtr == IntPtr.Zero) ? null : new Geometry(cPtr, true, ThisOwn_true());
     if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Ejemplo n.º 42
0
 public int AddGeometry(Geometry other)
 {
     int ret = OgrPINVOKE.Geometry_AddGeometry(swigCPtr, Geometry.getCPtr(other));
     if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Ejemplo n.º 43
0
 public int AddGeometryDirectly(Geometry other_disown)
 {
     int ret = OgrPINVOKE.Geometry_AddGeometryDirectly(swigCPtr, Geometry.getCPtrAndDisown(other_disown, ThisOwn_false()));
     if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Ejemplo n.º 44
0
 public Layer ExecuteSQL(string statement, Geometry spatialFilter, string dialect) {
   IntPtr cPtr = OgrPINVOKE.DataSource_ExecuteSQL(swigCPtr, statement, Geometry.getCPtr(spatialFilter), dialect);
   Layer ret = (cPtr == IntPtr.Zero) ? null : new Layer(cPtr, false, ThisOwn_false());
   if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
   return ret;
 }
Ejemplo n.º 45
0
        public static void WriteVectorFile(string strVectorFile)
        {
            Gdal.AllRegister();
            // 为了支持中文路径,请添加下面这句代码
            OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
            // 为了使属性表字段支持中文,请添加下面这句
            OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");

            //string strVectorFile = @"C:\Users\Administrator\Desktop\shptest\TestPolygon.shp";

            // 注册所有的驱动
            Ogr.RegisterAll();

            //创建数据,这里以创建ESRI的shp文件为例
            string strDriverName = "ESRI Shapefile";

            OSGeo.OGR.Driver oDriver = Ogr.GetDriverByName(strDriverName);
            if (oDriver == null)
            {
                MessageBox.Show("%s 驱动不可用!\n", strVectorFile);
                return;
            }

            // 创建数据源
            DataSource oDS = oDriver.CreateDataSource(strVectorFile, null);

            if (oDS == null)
            {
                MessageBox.Show("创建矢量文件【%s】失败!\n", strVectorFile);
                return;
            }

            // 创建图层,创建一个多边形图层,这里没有指定空间参考,如果需要的话,需要在这里进行指定
            Layer oLayer = oDS.CreateLayer("TestPolygon", null, wkbGeometryType.wkbPolygon, null);

            if (oLayer == null)
            {
                MessageBox.Show("图层创建失败!\n");
                return;
            }

            // 下面创建属性表
            // 先创建一个叫FieldID的整型属性
            FieldDefn oFieldID = new FieldDefn("FieldID", FieldType.OFTInteger);

            oLayer.CreateField(oFieldID, 1);

            // 再创建一个叫FeatureName的字符型属性,字符长度为50
            FieldDefn oFieldName = new FieldDefn("FieldName", FieldType.OFTString);

            oFieldName.SetWidth(100);
            oLayer.CreateField(oFieldName, 1);

            FeatureDefn oDefn = oLayer.GetLayerDefn();

            // 创建三角形要素
            Feature oFeatureTriangle = new Feature(oDefn);

            oFeatureTriangle.SetField(0, 0);
            oFeatureTriangle.SetField(1, "三角形");
            Geometry geomTriangle = Geometry.CreateFromWkt("POLYGON ((0 0,20 0,10 15,0 0))");

            oFeatureTriangle.SetGeometry(geomTriangle);

            oLayer.CreateFeature(oFeatureTriangle);

            // 创建矩形要素
            Feature oFeatureRectangle = new Feature(oDefn);

            oFeatureRectangle.SetField(0, 1);
            oFeatureRectangle.SetField(1, "矩形");
            Geometry geomRectangle = Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))");

            oFeatureRectangle.SetGeometry(geomRectangle);

            oLayer.CreateFeature(oFeatureRectangle);

            // 创建岛要素
            Feature oFeatureHole = new Feature(oDefn);

            oFeatureHole.SetField(0, 1);
            oFeatureHole.SetField(1, "环岛测试");
            //Geometry geomWYX = Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))");
            OSGeo.OGR.Geometry outGeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
            outGeo.AddPoint(40, -30, 0);
            outGeo.AddPoint(60, -30, 0);
            outGeo.AddPoint(60, -10, 0);
            outGeo.AddPoint(40, -10, 0);

            OSGeo.OGR.Geometry inGeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
            inGeo.AddPoint(45, -25, 0);
            inGeo.AddPoint(55, -25, 0);
            inGeo.AddPoint(55, -15, 0);
            inGeo.AddPoint(45, -15, 0);

            OSGeo.OGR.Geometry geo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPolygon);
            geo.AddGeometryDirectly(outGeo);
            geo.AddGeometryDirectly(inGeo);
            oFeatureHole.SetGeometry(geo);
            oLayer.CreateFeature(oFeatureHole);

            // 创建Multi要素
            Feature oFeatureMulty = new Feature(oDefn);

            oFeatureMulty.SetField(0, 1);
            oFeatureMulty.SetField(1, "MultyPart测试");
            OSGeo.OGR.Geometry geo1 = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
            geo1.AddPoint(25, -10, 0);
            geo1.AddPoint(5, -10, 0);
            geo1.AddPoint(5, -30, 0);
            geo1.AddPoint(25, -30, 0);
            OSGeo.OGR.Geometry poly1 = new Geometry(wkbGeometryType.wkbPolygon);
            poly1.AddGeometryDirectly(geo1);

            OSGeo.OGR.Geometry geo2 = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
            geo2.AddPoint(0, -15, 0);
            geo2.AddPoint(-5, -15, 0);
            geo2.AddPoint(-5, -20, 0);
            geo2.AddPoint(0, -20, 0);

            OSGeo.OGR.Geometry poly2 = new Geometry(wkbGeometryType.wkbPolygon);
            poly2.AddGeometryDirectly(geo2);

            OSGeo.OGR.Geometry geoMulty = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbMultiPolygon);
            geoMulty.AddGeometryDirectly(poly1);
            geoMulty.AddGeometryDirectly(poly2);
            oFeatureMulty.SetGeometry(geoMulty);

            oLayer.CreateFeature(oFeatureMulty);

            MessageBox.Show("\n数据集创建完成!\n");
        }
Ejemplo n.º 46
0
 public int SetGeomField(string name, Geometry geom)
 {
     int ret = OgrPINVOKE.Feature_SetGeomField__SWIG_1(swigCPtr, name, Geometry.getCPtr(geom));
     if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Ejemplo n.º 47
0
        public FeatureDataSet ExecuteQuery(string query, Geometry filter)
        {
            try
            {
                FeatureDataSet ds = new FeatureDataSet();
                FeatureDataTable myDt = new FeatureDataTable();

                Layer results = _OgrDataSource.ExecuteSQL(query, filter, "");

                //reads the column definition of the layer/feature
                ReadColumnDefinition(myDt, results);

                OSGeo.OGR.Feature _OgrFeature;
                results.ResetReading();
                while ((_OgrFeature = results.GetNextFeature()) != null)
                {
                    FeatureDataRow _dr = myDt.NewRow();
                    for (int iField = 0; iField < _OgrFeature.GetFieldCount(); iField++)
                    {
                        if (myDt.Columns[iField].DataType == System.Type.GetType("System.String"))
                            _dr[iField] = _OgrFeature.GetFieldAsString(iField);
                        else if (myDt.Columns[iField].GetType() == System.Type.GetType("System.Int32"))
                            _dr[iField] = _OgrFeature.GetFieldAsInteger(iField);
                        else if (myDt.Columns[iField].GetType() == System.Type.GetType("System.Double"))
                            _dr[iField] = _OgrFeature.GetFieldAsDouble(iField);
                        else
                            _dr[iField] = _OgrFeature.GetFieldAsString(iField);
                    }

                    _dr.Geometry = this.ParseOgrGeometry(_OgrFeature.GetGeometryRef());
                    myDt.AddRow(_dr);
                }
                ds.Tables.Add(myDt);
                _OgrDataSource.ReleaseResultSet(results);

                return ds;
            }
            catch (Exception exc)
            {
                System.Diagnostics.Debug.WriteLine(exc.ToString());
                return new FeatureDataSet();
            }
        }
Ejemplo n.º 48
0
 public int SetGeomFieldDirectly(string name, Geometry geom)
 {
     int ret = OgrPINVOKE.Feature_SetGeomFieldDirectly__SWIG_1(swigCPtr, name, Geometry.getCPtrAndDisown(geom, ThisOwn_false()));
     if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Ejemplo n.º 49
0
 public void SetSpatialFilter(int iGeomField, Geometry filter)
 {
     OgrPINVOKE.Layer_SetSpatialFilter__SWIG_1(swigCPtr, iGeomField, Geometry.getCPtr(filter));
     if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
 }
Ejemplo n.º 50
0
 public OGRBufferCacheRow(object[] bufferRow, Geometry geom)
 {
     this._bufferRow = bufferRow;
     this._geom = geom;
     this._envelope = this.calculateEnvelope(geom);
 }
Ejemplo n.º 51
-1
 public static Geometry ForceToMultiPoint(Geometry geom_in) {
   IntPtr cPtr = OgrPINVOKE.ForceToMultiPoint(Geometry.getCPtr(geom_in));
   Geometry ret = (cPtr == IntPtr.Zero) ? null : new Geometry(cPtr, true, ThisOwn_true());
   if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
   return ret;
 }