Example #1
0
        public string Write(ISpatial4nShape geometry)
        {
            if (ReferenceEquals(null, geometry))
            {
                return(null);
            }

            string result;

            if (TryWritePoint(geometry, out result))
            {
                return(result);
            }

            if (TryWriteCircle(geometry, out result))
            {
                return(result);
            }

            if (TryWriteEnvelope(geometry, out result))
            {
                return(result);
            }

            var wkt = geometry as IOgcGeometry;

            if (wkt != null)
            {
                return(wkt.ToWktString(_wktWriterSettings));
            }

            throw new SerializationException("Object of type " + geometry.GetType().Name + " is not supported by Spatial4n.");
        }
Example #2
0
 private bool TryWriteEnvelope(ISpatial4nShape shape, out string result)
 {
     var envelope = shape as Envelope;
     if (envelope != null)
     {
         result = string.Format(CultureInfo.InvariantCulture, "{0:F6} {1:F6} {2:F6} {3:F6}", envelope.MinLon,
                              envelope.MinLat, envelope.MaxLon, envelope.MaxLat);
         return true;
     }
     result = default(string);
     return false;
 }
Example #3
0
        private bool TryWriteEnvelope(ISpatial4nShape shape, out string result)
        {
            var envelope = shape as Envelope;

            if (envelope != null)
            {
                result = string.Format(CultureInfo.InvariantCulture, "{0:F6} {1:F6} {2:F6} {3:F6}", envelope.MinLon,
                                       envelope.MinLat, envelope.MaxLon, envelope.MaxLat);
                return(true);
            }
            result = default(string);
            return(false);
        }
Example #4
0
 private bool TryWritePoint(ISpatial4nShape shape, out string result)
 {
     var point = shape as Point;
     if (point != null)
     {
         if (point.IsEmpty)
             result = default(string);
         else
             result = string.Format(CultureInfo.InvariantCulture, "{0:F6} {1:F6}", point.Coordinate.Longitude, point.Coordinate.Latitude);
         return true;
     }
     result = default(string);
     return false;
 }
Example #5
0
 private bool TryWriteCircle(ISpatial4nShape shape, out string result)
 {
     var circle = shape as Circle;
     if (circle != null)
     {
         if (circle.IsEmpty)
             result = default(string);
         else
             result = string.Format(CultureInfo.InvariantCulture, "CIRCLE({0:F6} {1:F6} d={2:F6})", circle.Center.Longitude, circle.Center.Latitude, ConvertCircleRadius(circle.Radius));
         return true;
     }
     result = default(string);
     return false;
 }
Example #6
0
 private bool TryReadGeoPoint(string value, out ISpatial4nShape result)
 {
     var match = Regex.Match(value,
                 @"^ \s* ([+-]?(?:\d+\.?\d*|\d*\.?\d+)) \s* , \s* ([+-]?(?:\d+\.?\d*|\d*\.?\d+)) \s* $",
                 RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
     if (match.Success)
     {
         result = new Point(
             double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture),
             double.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)
             );
         return true;
     }
     result = null;
     return false;
 }
Example #7
0
 private bool TryReadCircle(string value, out ISpatial4nShape result)
 {
     var match = Regex.Match(value,
                 @"Circle \s* \( \s* ([+-]?(?:\d+\.?\d*|\d*\.?\d+)) \s+ ([+-]?(?:\d+\.?\d*|\d*\.?\d+)) \s+ d=([+-]?(?:\d+\.?\d*|\d*\.?\d+)) \s* \)",
                 RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
     if (match.Success)
     {
         result = new Circle(new Coordinate(
             double.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture),
             double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture)),
             ConvertCircleRadius(double.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture)));
         return true;
     }
     result = null;
     return false;
 }
Example #8
0
        private bool TryReadPoint(string value, out ISpatial4nShape result)
        {
            var match = Regex.Match(value,
                                    @"^ \s* (-?\d+.\d+?) \s+ (-?\d+.\d+?) \s* $",
                                    RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);

            if (match.Success)
            {
                result = new Point(
                    double.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture),
                    double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture)
                    );
                return(true);
            }
            result = null;
            return(false);
        }
Example #9
0
        private bool TryReadCircle(string value, out ISpatial4nShape result)
        {
            var match = Regex.Match(value,
                                    @"Circle \s* \( \s* ([+-]?(?:\d+\.?\d*|\d*\.?\d+)) \s+ ([+-]?(?:\d+\.?\d*|\d*\.?\d+)) \s+ d=([+-]?(?:\d+\.?\d*|\d*\.?\d+)) \s* \)",
                                    RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);

            if (match.Success)
            {
                result = new Circle(new Coordinate(
                                        double.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture),
                                        double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture)),
                                    ConvertCircleRadius(double.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture)));
                return(true);
            }
            result = null;
            return(false);
        }
Example #10
0
        private bool TryWriteCircle(ISpatial4nShape shape, out string result)
        {
            var circle = shape as Circle;

            if (circle != null)
            {
                if (circle.IsEmpty)
                {
                    result = default(string);
                }
                else
                {
                    result = string.Format(CultureInfo.InvariantCulture, "CIRCLE({0:F6} {1:F6} d={2:F6})", circle.Center.Longitude, circle.Center.Latitude, ConvertCircleRadius(circle.Radius));
                }
                return(true);
            }
            result = default(string);
            return(false);
        }
Example #11
0
        private bool TryWritePoint(ISpatial4nShape shape, out string result)
        {
            var point = shape as Point;

            if (point != null)
            {
                if (point.IsEmpty)
                {
                    result = default(string);
                }
                else
                {
                    result = string.Format(CultureInfo.InvariantCulture, "{0:F6} {1:F6}", point.Coordinate.Longitude, point.Coordinate.Latitude);
                }
                return(true);
            }
            result = default(string);
            return(false);
        }
Example #12
0
        private bool TryReadEnvelope(string value, out ISpatial4nShape result)
        {
            var match = Regex.Match(value,
                        @"^ \s* (-?\d+.\d+?) \s+ (-?\d+.\d+?) \s+ (-?\d+.\d+?) \s+ (-?\d+.\d+?) \s* $",
                        RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);

            if (match.Success)
            {
                result = new Envelope(
                    double.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture),
                    double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture),
                    double.Parse(match.Groups[4].Value, CultureInfo.InvariantCulture),
                    double.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture)
                    );
                return true;
            }
            result = null;
            return false;
        }
Example #13
0
        public string Write(ISpatial4nShape geometry)
        {
            if (ReferenceEquals(null, geometry))
                return null;

            string result;

            if (TryWritePoint(geometry, out result))
                return result;

            if (TryWriteCircle(geometry, out result))
                return result;

            if (TryWriteEnvelope(geometry, out result))
                return result;

            var wkt = geometry as IOgcGeometry;
            if (wkt != null)
                return wkt.ToWktString(_wktWriterSettings);

            throw new SerializationException("Object of type " + geometry.GetType().Name + " is not supported by Spatial4n.");
        }