private static CoordinateSystem ReadCoordinateSystem(string coordinateSystem, WktStreamTokenizer tokenizer, bool includeAuthority)
        {
            CoordinateSystem returnCS = null;

            switch (coordinateSystem)
            {
            case "VERT_CS":
                VerticalCoordinateSystem verticalCS = ReadVerticalCoordinateSystem(tokenizer, includeAuthority);
                returnCS = verticalCS;
                break;

            case "GEOGCS":
                GeographicCoordinateSystem geographicCS = ReadGeographicCoordinateSystem(tokenizer, includeAuthority);
                returnCS = geographicCS;
                break;

            case "PROJCS":
                ProjectedCoordinateSystem projectedCS = ReadProjectedCoordinateSystem(tokenizer, includeAuthority);
                returnCS = projectedCS;
                break;

            case "COMPD_CS":
                CompoundCoordinateSystem compoundCS = ReadCompoundCoordinateSystem(tokenizer, includeAuthority);
                returnCS = compoundCS;
                break;

            case "GEOCCS":
            case "FITTED_CS":
            case "LOCAL_CS":
                throw new InvalidOperationException(String.Format("{0} coordinate system is not recongized.", coordinateSystem));
            }
            return(returnCS);
        }
        private static void WriteProjectedCoordinateSystem(ProjectedCoordinateSystem projCoordSystem)
        {
            string projP4 = ProjDB.ProjectionP4ByName(projCoordSystem.Projection.Name);

            if (projP4 == String.Empty)
            {
                throw new NotImplementedException("Unknown projection " + projCoordSystem.Projection.Name);
            }

            _p4parameters.Add(new P4Parameter("+proj", projP4));

            WriteGeographicCoordinateSystem(projCoordSystem.GeographicCoordinateSystem);

            for (int i = 0; i < projCoordSystem.Projection.NumParameters; i++)
            {
                ProjectionParameter projParameter = projCoordSystem.Projection.GetParameter(i);

                switch (projParameter.Name.ToLower())
                {
                case "latitude_of_origin":
                case "latitude_of_center":
                    _p4parameters.Add(new P4Parameter("+lat_0", projParameter.Value.ToString(_nhi)));
                    break;

                case "central_meridian":
                case "longitude_of_origin":
                case "longitude_of_center":
                    _p4parameters.Add(new P4Parameter("+lon_0", projParameter.Value.ToString(_nhi)));
                    break;

                case "scale_factor":
                    _p4parameters.Add(new P4Parameter("+k", projParameter.Value.ToString(_nhi)));
                    break;

                case "false_easting":
                    _p4parameters.Add(new P4Parameter("+x_0", projParameter.Value.ToString(_nhi)));
                    break;

                case "false_northing":
                    _p4parameters.Add(new P4Parameter("+y_0", projParameter.Value.ToString(_nhi)));
                    break;

                case "azimuth":
                    _p4parameters.Add(new P4Parameter("+alpha", projParameter.Value.ToString(_nhi)));
                    break;

                case "standard_parallel_1":
                    _p4parameters.Add(new P4Parameter("+lat_1", projParameter.Value.ToString(_nhi)));
                    break;

                case "standard_parallel_2":
                    _p4parameters.Add(new P4Parameter("+lat_2", projParameter.Value.ToString(_nhi)));
                    break;
                }
            }
        }
 private static void WriteProjectedCoordinateSystem(ProjectedCoordinateSystem projectedCoordinateSystem, bool esri, IndentedTextWriter writer)
 {
     writer.WriteLine("PROJCS[");
     writer.Indent = writer.Indent + 1;
     writer.WriteLine(String.Format("\"{0}\",", projectedCoordinateSystem.Name));
     WriteGeographicCoordinateSystem(projectedCoordinateSystem.GeographicCoordinateSystem, esri, writer);
     //writer.WriteLine(",");
     WriteProjection(projectedCoordinateSystem.Projection, writer);
     WriteLinearUnit(projectedCoordinateSystem.LinearUnit, esri, writer);
     for (int dimension = 0; dimension < projectedCoordinateSystem.Dimension; dimension++)
     {
         WriteAxis(projectedCoordinateSystem.GetAxis(dimension), esri, writer);
     }
     //writer.WriteLine(String.Format("AUTHORITY[\"{0}\",\"{1}\"]", projectedCoordinateSystem.Authority, projectedCoordinateSystem.AuthorityCode));
     WriteAuthority(projectedCoordinateSystem, esri, writer);
     writer.Indent = writer.Indent - 1;
     writer.WriteLine("]");
 }
        private static ProjectedCoordinateSystem ReadProjectedCoordinateSystem(string[] parameters)
        {
            // +proj=tmerc +lat_0=0 +lon_0=-62 +k=0.999500 +x_0=400000 +y_0=0 +ellps=clrk80 +towgs84=725,685,536,0,0,0,0 +units=m +no_defs

            string projParam = ParameterValue(parameters, "+proj");
            string projName  = ProjDB.ProjectionNameByP4(projParam);

            List <ProjectionParameter> projParameters = new List <ProjectionParameter>();

            if (projParam.ToLower() == "utm" && projName.ToLower() == "transverse_mercator")
            {
                projParameters.Add(new ProjectionParameter("scale_factor", 0.9996));
                projParameters.Add(new ProjectionParameter("false_easting", 500000.0));
            }
            foreach (string p4param in parameters)
            {
                string val;
                string p4 = Parameter(p4param, out val);

                double v;
                if (!double.TryParse(val, NumberStyles.Number, _nhi, out v))
                {
                    continue;
                }

                switch (p4)
                {
                case "+lat_0":
                    projParameters.Add(new ProjectionParameter("latitude_of_origin", v));
                    break;

                case "+lon_0":
                    projParameters.Add(new ProjectionParameter("longitude_of_origin", v));
                    projParameters.Add(new ProjectionParameter("central_meridian", v));
                    break;

                case "+k":
                    projParameters.Add(new ProjectionParameter("scale_factor", v));
                    break;

                case "+x_0":
                    projParameters.Add(new ProjectionParameter("false_easting", v));
                    break;

                case "+y_0":
                    projParameters.Add(new ProjectionParameter("false_northing", v));
                    break;

                case "+alpha":
                    projParameters.Add(new ProjectionParameter("azimuth", v));
                    break;

                case "+lat_1":
                    projParameters.Add(new ProjectionParameter("standard_parallel_1", v));
                    break;

                case "+lat_2":
                    projParameters.Add(new ProjectionParameter("standard_parallel_2", v));
                    break;

                case "+zone":
                    projParameters.Add(new ProjectionParameter("Central_Meridian", -177.0 + 6 * (v - 1)));
                    break;
                }
            }

            Projection proj = new Projection(projName, projParameters.ToArray(), "", "", "", "");
            GeographicCoordinateSystem geogrCoordSystem = ReadGeographicCoordinateSystem(parameters);

            AxisInfo[] axis =
            {
                new AxisInfo("Easting",  AxisOrientation.East),
                new AxisInfo("Northing", AxisOrientation.North)
            };

            ProjectedCoordinateSystem projCoordSystem = new ProjectedCoordinateSystem(
                geogrCoordSystem.HorizontalDatum,
                axis,
                geogrCoordSystem,
                new LinearUnit(1.0, String.Empty, String.Empty, String.Empty, "metre", String.Empty, String.Empty),
                proj);

            projCoordSystem.Name = "Unknown";

            return(projCoordSystem);
        }
        private static ProjectedCoordinateSystem ReadProjectedCoordinateSystem(WktStreamTokenizer tokenizer, bool includeAuthority)
        {
            //PROJCS[
            //    "OSGB 1936 / British National Grid",
            //    GEOGCS[
            //        "OSGB 1936",
            //        DATUM[...]
            //        PRIMEM[...]
            //        AXIS["Geodetic latitude",NORTH]
            //        AXIS["Geodetic longitude",EAST]
            //        AUTHORITY["EPSG","4277"]
            //    ],
            //    PROJECTION["Transverse Mercator"],
            //    PARAMETER["latitude_of_natural_origin",49],
            //    PARAMETER["longitude_of_natural_origin",-2],
            //    PARAMETER["scale_factor_at_natural_origin",0.999601272],
            //    PARAMETER["false_easting",400000],
            //    PARAMETER["false_northing",-100000],
            //    AXIS["Easting",EAST],
            //    AXIS["Northing",NORTH],
            //    AUTHORITY["EPSG","27700"]
            //]

            tokenizer.ReadToken("[");
            string name = tokenizer.ReadDoubleQuotedWord();

            tokenizer.ReadToken(",");
            tokenizer.ReadToken("GEOGCS");
            GeographicCoordinateSystem geographicCS = ReadGeographicCoordinateSystem(tokenizer, includeAuthority);

            tokenizer.ReadToken(",");
            Projection projection = ReadProjection(tokenizer, includeAuthority);

            tokenizer.ReadToken("UNIT");
            Unit unit = ReadUnit(tokenizer, includeAuthority);

            AxisInfo axisInfo1 = null, axisInfo2 = null;

            if (tokenizer.TryReadToken(","))
            {
                if (tokenizer.TryReadToken("AXIS"))
                {
                    axisInfo1 = ReadAxisInfo(tokenizer);
                }
                if (tokenizer.TryReadToken(","))
                {
                    if (tokenizer.TryReadToken("AXIS"))
                    {
                        axisInfo2 = ReadAxisInfo(tokenizer);
                    }
                }
            }

            string authority     = String.Empty;
            string authorityCode = String.Empty;

            if (includeAuthority)
            {
                tokenizer.ReadToken(",");
                tokenizer.ReadAuthority(ref authority, ref authorityCode);
            }
            tokenizer.ReadToken("]");

            int axisInfoDim = 0;

            if (axisInfo1 != null)
            {
                axisInfoDim = 1;
            }
            if (axisInfo2 != null)
            {
                axisInfoDim = 2;
            }
            AxisInfo[] axisArray = new AxisInfo[axisInfoDim];
            if (axisInfo1 != null)
            {
                axisArray[0] = axisInfo1;
            }
            if (axisInfo2 != null)
            {
                axisArray[1] = axisInfo2;
            }

            ProjectedCoordinateSystem projectedCS = new ProjectedCoordinateSystem(geographicCS.HorizontalDatum, axisArray, geographicCS, unit as LinearUnit, projection, String.Empty, authority, authorityCode, name, String.Empty, String.Empty);

            return(projectedCS);
        }
        public static object Create(string wkt)
        {
            object             returnObject     = null;
            bool               includeAuthority = (wkt.ToLower().IndexOf("authority") != -1);
            StringReader       reader           = new StringReader(wkt);
            WktStreamTokenizer tokenizer        = new WktStreamTokenizer(reader);

            tokenizer.NextToken();
            string objectName = tokenizer.GetStringValue();

            switch (objectName)
            {
            case "UNIT":
                Unit unit = ReadUnit(tokenizer, includeAuthority);
                returnObject = unit;
                break;

            case "VERT_DATUM":
                VerticalDatum verticalDatum = ReadVerticalDatum(tokenizer, includeAuthority);
                returnObject = verticalDatum;
                break;

            case "SPHEROID":
                Ellipsoid ellipsoid = ReadEllipsoid(tokenizer, includeAuthority);
                returnObject = ellipsoid;
                break;

            case "TOWGS84":
                WGS84ConversionInfo wgsInfo = ReadWGS84ConversionInfo(tokenizer, includeAuthority);
                returnObject = wgsInfo;
                break;

            case "DATUM":
                HorizontalDatum horizontalDatum = ReadHorizontalDatum(tokenizer, includeAuthority);
                returnObject = horizontalDatum;
                break;

            case "PRIMEM":
                PrimeMeridian primeMeridian = ReadPrimeMeridian(tokenizer, includeAuthority);
                returnObject = primeMeridian;
                break;

            case "VERT_CS":
                VerticalCoordinateSystem verticalCS = ReadVerticalCoordinateSystem(tokenizer, includeAuthority);
                returnObject = verticalCS;
                break;

            case "GEOGCS":
                GeographicCoordinateSystem geographicCS = ReadGeographicCoordinateSystem(tokenizer, includeAuthority);
                returnObject = geographicCS;
                break;

            case "PROJCS":
                ProjectedCoordinateSystem projectedCS = ReadProjectedCoordinateSystem(tokenizer, includeAuthority);
                returnObject = projectedCS;
                break;

            case "COMPD_CS":
                CompoundCoordinateSystem compoundCS = ReadCompoundCoordinateSystem(tokenizer, includeAuthority);
                returnObject = compoundCS;
                break;

            case "GEOCCS":
            case "FITTED_CS":
            case "LOCAL_CS":
                throw new NotSupportedException(String.Format("{0} is not implemented.", objectName));

            default:
                throw new ParseException(String.Format("'{0'} is not recongnized.", objectName));
            }
            reader.Close();
            return(returnObject);
        }