Example #1
0
        public static void ReprojectPoints(double[] xy, double[] z, ProjectionInfo source, double srcZtoMeter, ProjectionInfo dest, double dstZtoMeter, IDatumTransform idt, int startIndex, int numPoints)
        {
            if (xy == null || source?.Equals(dest) == true || numPoints <= 0)
            {
                return;
            }
            double toMeter = source.Unit.Meters;

            // Geocentric coordinates are centered at the core of the earth.  Z is up toward the north pole.
            // The X axis goes from the center of the earth through Greenwich.
            // The Y axis passes through 90E.
            // This section converts from geocentric coordinates to geodetic ones if necessary.
            if (source.IsGeocentric)
            {
                if (z == null)
                {
                    throw new ProjectionException(45);
                }
                for (int i = startIndex; i < numPoints; i++)
                {
                    if (toMeter != 1)
                    {
                        xy[i * 2]     *= toMeter;
                        xy[i * 2 + 1] *= toMeter;
                    }
                }
                GeocentricGeodetic g = new GeocentricGeodetic(source.GeographicInfo.Datum.Spheroid);
                g.GeocentricToGeodetic(xy, z, startIndex, numPoints);
            }

            // Transform source points to lam/phi if they are not already
            ConvertToLatLon(source, xy, z, srcZtoMeter, startIndex, numPoints);

            double fromGreenwich = source.GeographicInfo.Meridian.Longitude * source.GeographicInfo.Unit.Radians;

            if (fromGreenwich != 0)
            {
                for (int i = startIndex; i < numPoints; i++)
                {
                    if (xy[2 * i] != double.PositiveInfinity)
                    {
                        xy[2 * i] += fromGreenwich;
                    }
                }
            }
            // DATUM TRANSFORM IF NEEDED
            if (idt == null)
            {
                if (!source.GeographicInfo.Datum.Matches(dest.GeographicInfo.Datum))
                {
                    DatumTransform(source, dest, xy, z, startIndex, numPoints);
                }
            }
            else
            {
                idt.Transform(source, dest, xy, z, startIndex, numPoints);
            }

            // Adjust to new prime meridian if there is one in the destination cs
            fromGreenwich = dest.GeographicInfo.Meridian.Longitude * dest.GeographicInfo.Unit.Radians;
            if (fromGreenwich != 0)
            {
                for (int i = startIndex; i < numPoints; i++)
                {
                    if (xy[i * 2] != double.PositiveInfinity)
                    {
                        xy[i * 2] -= fromGreenwich;
                    }
                }
            }

            if (dest.IsGeocentric)
            {
                if (z == null)
                {
                    throw new ProjectionException(45);
                }
                GeocentricGeodetic g = new GeocentricGeodetic(dest.GeographicInfo.Datum.Spheroid);
                g.GeodeticToGeocentric(xy, z, startIndex, numPoints);
                double frmMeter = 1 / dest.Unit.Meters;
                if (frmMeter != 1)
                {
                    for (int i = startIndex; i < numPoints; i++)
                    {
                        if (xy[i * 2] != double.PositiveInfinity)
                        {
                            xy[i * 2]     *= frmMeter;
                            xy[i * 2 + 1] *= frmMeter;
                        }
                    }
                }
            }
            else
            {
                ConvertToProjected(dest, xy, z, dstZtoMeter, startIndex, numPoints);
            }
        }