public void Release()
 {
     lock (LockThis1)
     {
         //try
         //{
         //    if (_preToID > 0) Proj4Wrapper.pj_free(_preToID);
         //}
         //catch { }
         //try
         //{
         //    if (_preFromID > 0) Proj4Wrapper.pj_free(_preFromID);
         //}
         //catch { }
         try
         {
             if (_fromID != IntPtr.Zero)
             {
                 Proj4Wrapper.pj_free(_fromID);
             }
         }
         catch { }
         try
         {
             if (_toID != IntPtr.Zero)
             {
                 Proj4Wrapper.pj_free(_toID);
             }
         }
         catch { }
         _fromSRef = _toSRef = null;
         _fromID   = _toID = IntPtr.Zero;
         //_preFrom = _preTo = -1;
     }
 }
        public void SetSpatialReferences(ISpatialReference from, ISpatialReference to)
        {
            if (from == null)
            {
                lock (LockThis1)
                {
                    try
                    {
                        if (_fromID != IntPtr.Zero)
                        {
                            Proj4Wrapper.pj_free(_fromID);
                        }
                    }
                    catch { }
                    _fromID   = IntPtr.Zero;
                    _fromSRef = null;
                }
            }

            if (to == null)
            {
                lock (LockThis1)
                {
                    try
                    {
                        if (_toID != IntPtr.Zero)
                        {
                            Proj4Wrapper.pj_free(_toID);
                        }
                    }
                    catch { }
                    _toID   = IntPtr.Zero;
                    _toSRef = null;
                }
            }

            if ((from != null && from.Datum == null) && (to != null && to.Datum != null))
            {
                ISpatialReference toSRef = (ISpatialReference)to.Clone();
                toSRef.Datum              = null;
                this.ToSpatialReference   = toSRef;
                this.FromSpatialReference = from;
            }
            else if ((from != null && from.Datum != null) && (to != null && to.Datum == null))
            {
                this.ToSpatialReference = to;
                ISpatialReference fromSRef = (ISpatialReference)from.Clone();
                fromSRef.Datum            = null;
                this.FromSpatialReference = fromSRef;
            }
            else
            {
                this.ToSpatialReference   = to;
                this.FromSpatialReference = from;
            }
        }
        private object Transform2D_(object geometry, IntPtr from, IntPtr to, bool fromProjective, bool toProjektive)
        {
            if (geometry == null)
            {
                return(null);
            }

            if (from == IntPtr.Zero || to == IntPtr.Zero)
            {
                return(geometry);
            }
            if (geometry is PointCollection)
            {
                PointCollection pColl      = (PointCollection)geometry;
                int             pointCount = pColl.PointCount;
                if (pointCount == 0)
                {
                    return(geometry);
                }

                IntPtr buffer = Marshal.AllocHGlobal(pointCount * 2 * sizeof(double));

                lock (LockThis1)
                {
                    try
                    {
                        IntPtr xPtr = IntPtr.Zero, yPtr = IntPtr.Zero;
                        unsafe
                        {
                            double *b = (double *)buffer;

                            for (int i = 0; i < pointCount; i++)
                            {
                                b[i] = pColl[i].X;
                                b[pointCount + i] = pColl[i].Y;
                            }

                            if (!fromProjective)
                            {
                                ToRad(buffer, pointCount * 2);
                            }

                            xPtr = (IntPtr)(&b[0]);
                            yPtr = (IntPtr)(&b[pointCount]);
                        }
                        if (from != IntPtr.Zero && to != IntPtr.Zero)
                        {
                            //if (preTo > 0)
                            //{
                            //    Proj4Wrapper.pj_transform(from, preTo, pointCount, 0, xPtr, yPtr, (IntPtr)0);
                            //    Proj4Wrapper.pj_transform(preTo, to, pointCount, 0, xPtr, yPtr, (IntPtr)0);
                            //}
                            //else
                            //{
                            Proj4Wrapper.pj_transform(from, to, pointCount, 0, xPtr, yPtr, IntPtr.Zero);
                            //}
                        }
                        if (!toProjektive)
                        {
                            ToDeg(buffer, pointCount * 2);
                        }

                        IPointCollection target = null;
                        if (pColl is IRing)
                        {
                            target = new Ring();
                        }
                        else if (pColl is IPath)
                        {
                            target = new Path();
                        }
                        else if (pColl is IMultiPoint)
                        {
                            target = new MultiPoint();
                        }
                        else
                        {
                            target = new PointCollection();
                        }

                        unsafe
                        {
                            double *b = (double *)buffer;
                            for (int i = 0; i < pointCount; i++)
                            {
                                target.AddPoint(new Point(b[i], b[pointCount + i]));
                            }

                            return(target);
                        }
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(buffer);
                    }
                }
                //double [] x;
                //double [] y;
                //((PointCollection)geometry).getXY(out x,out y);
                //if (!fromProjective) ToRad(x, y);

                //if(from>0 && to>0)
                //    Proj4Wrapper.pj_transform(from,to,x.Length,0,x,y,null);
                //if (!toProjektive) ToDeg(x, y);

                //if(geometry is Ring)
                //{
                //    Ring ring=new Ring();
                //    ring.setXYZ(x,y,null);
                //    return ring;
                //}
                //if(geometry is Path)
                //{
                //    Path path=new Path();
                //    path.setXYZ(x,y,null);
                //    return path;
                //}
                //if (geometry is MultiPoint)
                //{
                //    MultiPoint multiPoint = new MultiPoint();
                //    multiPoint.setXYZ(x, y, null);
                //    return multiPoint;
                //}
                //return new PointCollection(x,y,null);
            }
            if (geometry is IPoint)
            {
                double[] x = { ((IPoint)geometry).X };
                double[] y = { ((IPoint)geometry).Y };
                if (!fromProjective)
                {
                    ToRad(x, y);
                }

                lock (LockThis1)
                {
                    if (from != IntPtr.Zero && to != IntPtr.Zero)
                    {
                        unsafe
                        {
                            fixed(double *xx = x)
                            fixed(double *yy = y)
                            {
                                IntPtr xPtr = (IntPtr)(xx);
                                IntPtr yPtr = (IntPtr)(yy);

                                //if (preTo > 0)
                                //{
                                //    Proj4Wrapper.pj_transform(from, preTo, x.Length, 0, xPtr, yPtr, (IntPtr)0);
                                //    Proj4Wrapper.pj_transform(preTo, to, x.Length, 0, xPtr, yPtr, (IntPtr)0);
                                //}
                                //else
                                //{
                                Proj4Wrapper.pj_transform(from, to, x.Length, 0, xPtr, yPtr, IntPtr.Zero);
                                //}
                            }
                        }
                    }
                }
                if (!toProjektive)
                {
                    ToDeg(x, y);
                }

                return(new Point(x[0], y[0]));
            }
            if (geometry is IEnvelope)
            {
                return(Transform2D_(((IEnvelope)geometry).ToPolygon(10), from, to, fromProjective, toProjektive));
            }
            if (geometry is IPolyline)
            {
                int       count    = ((IPolyline)geometry).PathCount;
                IPolyline polyline = new Polyline();
                for (int i = 0; i < count; i++)
                {
                    polyline.AddPath((IPath)Transform2D_(((IPolyline)geometry)[i], from, to, fromProjective, toProjektive));
                }
                return(polyline);
            }
            if (geometry is IPolygon)
            {
                int      count   = ((IPolygon)geometry).RingCount;
                IPolygon polygon = new Polygon();
                for (int i = 0; i < count; i++)
                {
                    polygon.AddRing((IRing)Transform2D_(((IPolygon)geometry)[i], from, to, fromProjective, toProjektive));
                }
                return(polygon);
            }

            if (geometry is IAggregateGeometry)
            {
                int count = ((IAggregateGeometry)geometry).GeometryCount;
                IAggregateGeometry aGeom = new AggregateGeometry();
                for (int i = 0; i < count; i++)
                {
                    aGeom.AddGeometry((IGeometry)Transform2D_(((IAggregateGeometry)geometry)[i], from, to, fromProjective, toProjektive));
                }
                return(aGeom);
            }

            return(null);
        }