Example #1
0
        public Geospatial(String value)
        {
            String s = value.ToString();

            initInstanceVars();
            this.FromString(s);
            this.setGXGeoType(SQLGeographyWrapper.STGeometryType(_innerValue).ToString());
        }
Example #2
0
        public Geospatial(object value)
        {
            Geospatial geo = value as Geospatial;

            if (geo != null)
            {
                this.InnerValue = geo.InnerValue;
                this.setGXGeoType(SQLGeographyWrapper.STGeometryType(_innerValue).ToString());
                this.srid = geo.srid;
            }
            else
            {
                String s = value.ToString();
                new Geospatial(s);
            }
        }
Example #3
0
        public void FromString(String s)
        {
            if (IsGeoNull(s))
            {
                geoText = EMPTY_GEOMETRY;
            }
            else
            {
                geoText = s.Trim();
            }


            try
            {
                // Sql Server Text
                _innerValue = SQLGeographyWrapper.Parse(geoText);
                if ((!SQLGeographyWrapper.IsValid(_innerValue)) && _innerValue != null)
                {
                    _innerValue = SQLGeographyWrapper.MakeValid(_innerValue);
                }

                this.srid = ((SqlInt32)ClassLoader.GetPropValue(_innerValue, "STSrid")).Value;

                this.setGXGeoType(SQLGeographyWrapper.STGeometryType(_innerValue).ToString());
                if (GeographicType == GeoGraphicTypeValue.Point)
                {
                    this.Point.Longitude = SQLGeographyWrapper.Long(_innerValue);
                    this.Point.Latitude  = SQLGeographyWrapper.Lat(_innerValue);
                }
            }
            catch (ArgumentException ex)
            {
                if (ex.ToString().Contains("24144")) // makevalid didnĀ“t work
                {
                    _innerValue = null;
                }
                if (ex.ToString().Contains("24200"))                 // Error code for invalid Geo.
                {
                    _innerValue = SQLGeographyWrapper.STGeomFromText(geoText, srid);
                }
                else
                {
                    // Cannot parse value
                    this.InnerValue      = SQLGeographyWrapper.NullSQLGeography;
                    this.geoText         = "";
                    this.Point.Longitude = 0;
                    this.Point.Latitude  = 0;
                }
            }
            catch (FormatException ex)
            {
                String exText = ex.ToString();
                if (!String.IsNullOrEmpty(exText) && (ex.ToString().Contains("24114") || ex.ToString().Contains("24141")))
                {
                    if (GeographicType == GeoGraphicTypeValue.Point && !String.IsNullOrEmpty(geoText))
                    {
                        int commas = geoText.Split(',').Length - 1;
                        if ((commas == 1) && !geoText.Contains(" "))
                        {
                            // has . as decimal separator and "," as value sep
                            geoText = geoText.Replace(',', ' ');
                        }
                        else
                        {
                            if (geoText.Contains(",") && geoText.Contains(" "))
                            {
                                geoText = geoText.Replace(',', '.');
                            }
                        }
                        try
                        {
                            String[] coord = geoText.Split(new char[] { ' ' }, 2);
                            this.Point.Longitude = Convert.ToDouble(coord[1].Trim(), CultureInfo.InvariantCulture.NumberFormat);
                            this.Point.Latitude  = Convert.ToDouble(coord[0].Trim(), CultureInfo.InvariantCulture.NumberFormat);
                            this.srid            = 4326;
                            // Latitude and Longitud parameters are reversed in the 'Point' constructor:
                            _innerValue = ClassLoader.InvokeStatic(SQLGeographyWrapper.GeoAssembly, SQLGeographyWrapper.SqlGeographyClass, "Point", new object[] { this.Point.Latitude, this.Point.Longitude, this.Srid });
                        }
                        catch (Exception)
                        {
                            // Can't convert to geography set as null.
                            _innerValue          = SQLGeographyWrapper.NullSQLGeography;
                            this.geoText         = "";
                            this.Point.Longitude = 0;
                            this.Point.Latitude  = 0;
                        }
                    }
                    else
                    {
                        // Cannot parse value
                        _innerValue          = SQLGeographyWrapper.NullSQLGeography;
                        this.geoText         = "";
                        this.Point.Longitude = 0;
                        this.Point.Latitude  = 0;
                    }
                }
                else
                {
                    // Cannot parse value
                    _innerValue          = SQLGeographyWrapper.NullSQLGeography;
                    this.geoText         = "";
                    this.Point.Longitude = 0;
                    this.Point.Latitude  = 0;
                }
            }
        }