ArrayList geoToArray(String geoType, Object geoInstance) { ArrayList ArrayOfPoints = new ArrayList(); if (geoType.Equals("MultiPoint") || geoType.Equals("LineString")) { SqlInt32 points = (SqlInt32)ClassLoader.Invoke(geoInstance, "STNumPoints", null); for (int i = 1; i <= points.Value; i++) { double[] CurrentPoint = new double[2]; object currentPoint = ClassLoader.Invoke(geoInstance, "STPointN", new object[] { i }); CurrentPoint[0] = SQLGeographyWrapper.Long(currentPoint); CurrentPoint[1] = SQLGeographyWrapper.Lat(currentPoint); ArrayOfPoints.Add(CurrentPoint); } } else if (geoType.Equals("Point")) { object currentPoint = ClassLoader.Invoke(geoInstance, "STPointN", new object[] { 1 }); ArrayOfPoints.Add(SQLGeographyWrapper.Long(currentPoint)); ArrayOfPoints.Add(SQLGeographyWrapper.Lat(currentPoint)); } else if (geoType.Equals("Polygon")) { SqlInt32 rings = (SqlInt32)ClassLoader.Invoke(geoInstance, "NumRings", null); for (int j = 1; j <= rings.Value; j++) { ArrayList RingArray = new ArrayList(); object currentRing = ClassLoader.Invoke(geoInstance, "RingN", new object[] { j }); SqlInt32 p = (SqlInt32)ClassLoader.Invoke(currentRing, "STNumPoints", null); for (int i = 1; i < p.Value; i++) { double[] CurrentPoint = new double[2]; object currentPoint = ClassLoader.Invoke(currentRing, "STPointN", new object[] { i }); CurrentPoint[0] = SQLGeographyWrapper.Long(currentPoint); CurrentPoint[1] = SQLGeographyWrapper.Lat(currentPoint); RingArray.Add(CurrentPoint); } ArrayOfPoints.Add(RingArray); } } else if (geoType.Equals("MultiLineString")) { SqlInt32 geoms = (SqlInt32)ClassLoader.Invoke(geoInstance, "STNumGeometries", null); for (int j = 1; j <= geoms.Value; j++) { object currentgeom = ClassLoader.Invoke(geoInstance, "STGeometryN", new object[] { j }); ArrayOfPoints.Add(geoToArray("LineString", currentgeom)); } } else if (geoType.Equals("MultiPolygon")) { SqlInt32 geoms = (SqlInt32)ClassLoader.Invoke(geoInstance, "STNumGeometries", null); for (int j = 1; j <= geoms.Value; j++) { object currentgeom = ClassLoader.Invoke(geoInstance, "STGeometryN", new object[] { j }); ArrayOfPoints.Add(geoToArray("Polygon", currentgeom)); } } return(ArrayOfPoints); }
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; } } }