/// <summary> /// Try to get value from WKT format /// SRID=0;POINT (x y) or POINT (x y) /// </summary> /// <param name="value">WKT string format</param> public static bool TryParse(string value, out MyCatGeometry mySqlGeometryValue) { string[] arrayResult = new string[0]; string strResult = string.Empty; bool hasX = false; bool hasY = false; Double xVal = 0; Double yVal = 0; int sridValue = 0; try { if (value.Contains(";")) { arrayResult = value.Split(';'); } else { strResult = value; } if (arrayResult.Length > 1 || strResult != String.Empty) { string point = strResult != String.Empty ? strResult : arrayResult[1]; point = point.Replace("POINT (", "").Replace("POINT(", "").Replace(")", ""); var coord = point.Split(' '); if (coord.Length > 1) { hasX = Double.TryParse(coord[0], out xVal); hasY = Double.TryParse(coord[1], out yVal); } if (arrayResult.Length >= 1) { Int32.TryParse(arrayResult[0].Replace("SRID=", ""), out sridValue); } } if (hasX && hasY) { mySqlGeometryValue = new MyCatGeometry(xVal, yVal, sridValue); return(true); } } catch { } mySqlGeometryValue = new MyCatGeometry(true); return(false); }
/// <summary> /// Get value from WKT format /// SRID=0;POINT (x y) or POINT (x y) /// </summary> /// <param name="value">WKT string format</param> public static MyCatGeometry Parse(string value) { if (String.IsNullOrEmpty(value)) { throw new ArgumentNullException("value"); } if (!(value.Contains("SRID") || value.Contains("POINT(") || value.Contains("POINT ("))) { throw new FormatException("String does not contain a valid geometry value"); } MyCatGeometry result = new MyCatGeometry(0, 0); MyCatGeometry.TryParse(value, out result); return(result); }
IMyCatValue IMyCatValue.ReadValue(MyCatPacket packet, long length, bool nullVal) { MyCatGeometry g; if (nullVal) { g = new MyCatGeometry(_type, true); } else { if (length == -1) { length = (long)packet.ReadFieldLength(); } byte[] newBuff = new byte[length]; packet.Read(newBuff, 0, (int)length); g = new MyCatGeometry(_type, newBuff); } return(g); }
void IMyCatValue.WriteValue(MyCatPacket packet, bool binary, object val, int length) { byte[] buffToWrite = null; try { buffToWrite = ((MyCatGeometry)val)._valBinary; } catch { buffToWrite = val as Byte[]; } if (buffToWrite == null) { MyCatGeometry v = new MyCatGeometry(0, 0); MyCatGeometry.TryParse(val.ToString(), out v); buffToWrite = v._valBinary; } byte[] result = new byte[GEOMETRY_LENGTH]; for (int i = 0; i < buffToWrite.Length; i++) { if (buffToWrite.Length < GEOMETRY_LENGTH) { result[i + 4] = buffToWrite[i]; } else { result[i] = buffToWrite[i]; } } packet.WriteStringNoNull("_binary "); packet.WriteByte((byte)'\''); EscapeByteArray(result, GEOMETRY_LENGTH, packet); packet.WriteByte((byte)'\''); }