Пример #1
0
        public static SMGeometry ToSharpMapGeometry(SqlGeometry geometry, Factory factory)
        {
            if (geometry == null) return null;
            var fact = factory ?? Services.CreateGeometryFactory((int) geometry.STSrid);

            if (geometry.STIsEmpty())
                return fact.CreateGeometryCollection(null);

            if (!geometry.STIsValid())
                geometry.MakeValid();

            OpenGisGeometryType geometryType = (OpenGisGeometryType)Enum.Parse(typeof(OpenGisGeometryType), (string)geometry.STGeometryType());
            switch (geometryType)
            {
                case OpenGisGeometryType.Point:
                    return SqlGeometryToSharpMapPoint(geometry, fact);
                case OpenGisGeometryType.LineString:
                    return SqlGeometryToSharpMapLineString(geometry, fact);
                case OpenGisGeometryType.Polygon:
                    return SqlGeometryToSharpMapPolygon(geometry, fact);
                case OpenGisGeometryType.MultiPoint:
                    return SqlGeometryToSharpMapMultiPoint(geometry, fact);
                case OpenGisGeometryType.MultiLineString:
                    return SqlGeometryToSharpMapMultiLineString(geometry, fact);
                case OpenGisGeometryType.MultiPolygon:
                    return SqlGeometryToSharpMapMultiPolygon(geometry, fact);
                case OpenGisGeometryType.GeometryCollection:
                    return SqlGeometryToSharpMapGeometryCollection(geometry, fact);
            }
            throw new ArgumentException(string.Format("Cannot convert SqlServer '{0}' to Sharpmap.Geometry", geometry.STGeometryType()), "geometry");
        }
Пример #2
0
        public static IEnumerable<SMGeometry> ToSharpMapGeometries(IEnumerable<SqlGeometry> sqlGeometries, Factory factory)
        {
            var fact = factory ?? Services.CreateGeometryFactory((int)sqlGeometries.First().STSrid);

            foreach (var sqlGeometry in sqlGeometries)
                yield return ToSharpMapGeometry(sqlGeometry, fact);
        }
Пример #3
0
        public static SMGeometry ToSharpMapGeometry(SqlGeometry geometry, Factory factory)
        {
            if (geometry == null)
            {
                return(null);
            }
            if (geometry.IsNull)
            {
                return(null);
            }

            var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);

            if (geometry.STIsEmpty())
            {
                return(fact.CreateGeometryCollection(null));
            }

            if (!geometry.STIsValid())
            {
                geometry = geometry.MakeValid();
            }

            OpenGisGeometryType geometryType = (OpenGisGeometryType)Enum.Parse(typeof(OpenGisGeometryType), (string)geometry.STGeometryType());

            switch (geometryType)
            {
            case OpenGisGeometryType.Point:
                return(SqlGeometryToSharpMapPoint(geometry, fact));

            case OpenGisGeometryType.LineString:
                return(SqlGeometryToSharpMapLineString(geometry, fact));

            case OpenGisGeometryType.Polygon:
                return(SqlGeometryToSharpMapPolygon(geometry, fact));

            case OpenGisGeometryType.MultiPoint:
                return(SqlGeometryToSharpMapMultiPoint(geometry, fact));

            case OpenGisGeometryType.MultiLineString:
                return(SqlGeometryToSharpMapMultiLineString(geometry, fact));

            case OpenGisGeometryType.MultiPolygon:
                return(SqlGeometryToSharpMapMultiPolygon(geometry, fact));

            case OpenGisGeometryType.GeometryCollection:
                return(SqlGeometryToSharpMapGeometryCollection(geometry, fact));
            }
            throw new ArgumentException(string.Format("Cannot convert SqlServer '{0}' to Sharpmap.Geometry", geometry.STGeometryType()), "geometry");
        }
Пример #4
0
 private static Geometry ParseOgrGeometry(OgrGeometry ogrGeometry, GeoAPI.Geometries.IGeometryFactory factory)
 {
     if (ogrGeometry != null)
     {
         //Just in case it isn't 2D
         ogrGeometry.FlattenTo2D();
         var wkbBuffer = new byte[ogrGeometry.WkbSize()];
         ogrGeometry.ExportToWkb(wkbBuffer);
         var geom = GeometryFromWKB.Parse(wkbBuffer, factory);
         if (geom == null)
         {
             Debug.WriteLine("Failed to parse '{0}'", ogrGeometry.GetGeometryType());
         }
         return(geom);
     }
     return(null);
 }
Пример #5
0
        private static SMGeometry SqlGeometryToSharpMapPolygon(SqlGeometry geometry, Factory factory)
        {
            var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
            //exterior ring
            var exterior = fact.CreateLinearRing(GetPoints(geometry.STExteriorRing()));

            SMLinearRing[] interior = null;
            if (geometry.STNumInteriorRing() > 0)
            {
                interior = new SMLinearRing[(int)geometry.STNumInteriorRing()];
                for (var i = 1; i <= geometry.STNumInteriorRing(); i++)
                {
                    interior[i - 1] = fact.CreateLinearRing(GetPoints(geometry.STInteriorRingN(i)));
                }
            }
            return(Services.CreateGeometryFactory((int)geometry.STSrid).CreatePolygon(exterior, interior));
        }
Пример #6
0
        private static SMGeometry SqlGeometryToSharpMapLineString(SqlGeometry geometry, Factory factory)
        {
            var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);

            return(fact.CreateLineString(GetPoints(geometry)));
        }
Пример #7
0
        private static SMGeometry SqlGeometryToSharpMapPoint(SqlGeometry geometry, Factory factory)
        {
            var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);

            return(fact.CreatePoint(new Coordinate((double)geometry.STX, (double)geometry.STY)));
        }
Пример #8
0
        private static SMGeometry SqlGeometryToSharpMapMultiPoint(SqlGeometry geometry, Factory factory)
        {
            var fact   = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
            var points = new SMPoint[(int)geometry.STNumGeometries()];

            for (var i = 1; i <= geometry.STNumGeometries(); i++)
            {
                points[i - 1] = (SMPoint)SqlGeometryToSharpMapPoint(geometry.STGeometryN(i), fact);
            }
            return(fact.CreateMultiPoint(points));
        }
Пример #9
0
        private static SMMultiLineString SqlGeometryToSharpMapMultiLineString(SqlGeometry geometry, Factory factory)
        {
            var fact        = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
            var lineStrings = new SMLineString[(int)geometry.STNumGeometries()];

            for (var i = 1; i <= geometry.STNumGeometries(); i++)
            {
                lineStrings[i - 1] = (SMLineString)SqlGeometryToSharpMapLineString(geometry.STGeometryN(i), fact);
            }
            return(fact.CreateMultiLineString(lineStrings));
        }
Пример #10
0
        /*
         * private static OpenGisGeometryType ParseGeometryType(string stGeometryType)
         * {
         *  switch (stGeometryType.ToUpper())
         *  {
         *      case "POINT":
         *          return OpenGisGeometryType.Point;
         *      case "LINESTRING":
         *          return OpenGisGeometryType.LineString;
         *      case "POLYGON":
         *          return OpenGisGeometryType.Polygon;
         *      case "MULTIPOINT":
         *          return OpenGisGeometryType.MultiPoint;
         *      case "MULTILINESTRING":
         *          return OpenGisGeometryType.MultiLineString;
         *      case "MULTIPOLYGON":
         *          return OpenGisGeometryType.MultiPolygon;
         *      case "GEOMETRYCOLLECTION":
         *          return OpenGisGeometryType.GeometryCollection;
         *  }
         *  throw new ArgumentException(String.Format("Invalid geometrytype '{0}'!", stGeometryType), "stGeometryType");
         * }
         */

        private static SMGeometryCollection SqlGeometryToSharpMapGeometryCollection(SqlGeometry geometry, Factory factory)
        {
            var fact  = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
            var geoms = new SMGeometry[(int)geometry.STNumGeometries()];

            for (var i = 1; i <= geometry.STNumGeometries(); i++)
            {
                geoms[i - 1] = ToSharpMapGeometry(geometry.STGeometryN(i), fact);
            }
            return(fact.CreateGeometryCollection(geoms));
        }
Пример #11
0
        public static IEnumerable <SMGeometry> ToSharpMapGeometries(IEnumerable <SqlGeometry> sqlGeometries, Factory factory)
        {
            var fact = factory ?? Services.CreateGeometryFactory((int)sqlGeometries.First().STSrid);

            foreach (var sqlGeometry in sqlGeometries)
            {
                yield return(ToSharpMapGeometry(sqlGeometry, fact));
            }
        }
Пример #12
0
 private static SMGeometry SqlGeometryToSharpMapLineString(SqlGeometry geometry, Factory factory)
 {
     var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
     return fact.CreateLineString(GetPoints(geometry));
 }
Пример #13
0
        private static SMGeometry SqlGeographyToSharpMapPolygon(SqlGeography geography, Factory factory)
        {
            var fact = factory ?? Services.CreateGeometryFactory((int)geography.STSrid);

            // courtesy of NetTopologySuite.Io.SqlServerBytes
            var rings = new List <ILinearRing>();

            for (var i = 1; i <= geography.NumRings(); i++)
            {
                rings.Add(fact.CreateLinearRing(GetPoints(geography.RingN(i))));
            }

            var shellCCW = rings.FirstOrDefault(r => r.IsCCW);
            // NB: reverse exterio ring orientation
            var shellCW = fact.CreateLinearRing(shellCCW.Reverse().Coordinates);

            return(fact.CreatePolygon(shellCW, Enumerable.ToArray(rings.Where(r => r != shellCCW))));
        }
Пример #14
0
        private static FeatureDataRow LoadOgrFeatureToFeatureDataRow(FeatureDataTable table, OSGeo.OGR.Feature ogrFeature, GeoAPI.Geometries.IGeometryFactory factory)
        {
            var values = new object[ogrFeature.GetFieldCount()];

            for (var iField = 0; iField < ogrFeature.GetFieldCount(); iField++)
            {
                // No need to get field value if there's no value available...
                if (!ogrFeature.IsFieldSet(iField))
                {
                    continue;
                }

                int count;
                switch (ogrFeature.GetFieldType(iField))
                {
                case OgrFieldType.OFTString:
                case OgrFieldType.OFTWideString:
                    values[iField] = ogrFeature.GetFieldAsString(iField);
                    break;

                case OgrFieldType.OFTStringList:
                case OgrFieldType.OFTWideStringList:
                    values[iField] = ogrFeature.GetFieldAsStringList(iField);
                    break;

                case OgrFieldType.OFTInteger:
                    values[iField] = ogrFeature.GetFieldAsInteger(iField);
                    break;

                case OgrFieldType.OFTIntegerList:
                    values[iField] = ogrFeature.GetFieldAsIntegerList(iField, out count);
                    break;

                case OgrFieldType.OFTReal:
                    values[iField] = ogrFeature.GetFieldAsDouble(iField);
                    break;

                case OgrFieldType.OFTRealList:
                    values[iField] = ogrFeature.GetFieldAsDoubleList(iField, out count);
                    break;

                case OgrFieldType.OFTDate:
                case OgrFieldType.OFTDateTime:
                case OgrFieldType.OFTTime:
                    Int32 y, m, d, h, mi, s, tz;
                    ogrFeature.GetFieldAsDateTime(iField, out y, out m, out d, out h, out mi, out s, out tz);
                    try
                    {
                        if (y == 0 && m == 0 && d == 0)
                        {
                            values[iField] = DateTime.MinValue.AddMinutes(h * 60 + mi);
                        }
                        else
                        {
                            values[iField] = new DateTime(y, m, d, h, mi, s);
                        }
                    }
// ReSharper disable once EmptyGeneralCatchClause
                    catch { }
                    break;

                default:
                    Debug.WriteLine("Cannot handle Ogr DataType '{0}'", ogrFeature.GetFieldType(iField));
                    break;
                }
            }

            var fdr = (FeatureDataRow)table.LoadDataRow(values, true);

            using (var gr = ogrFeature.GetGeometryRef())
            {
                fdr.Geometry = ParseOgrGeometry(gr, factory);
                gr.Dispose();
            }
            return(fdr);
        }
Пример #15
0
 private static SMGeometry SqlGeometryToSharpMapPolygon(SqlGeometry geometry, Factory factory)
 {
     var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
     //exterior ring
     var exterior = fact.CreateLinearRing(GetPoints(geometry.STExteriorRing()));
     SMLinearRing[] interior = null;
     if (geometry.STNumInteriorRing()>0)
     {
         interior = new SMLinearRing[(int)geometry.STNumInteriorRing()];
         for (var i = 1; i <= geometry.STNumInteriorRing(); i++)
             interior[i - 1] = fact.CreateLinearRing(GetPoints(geometry.STInteriorRingN(i)));
     }
     return Services.CreateGeometryFactory((int)geometry.STSrid).CreatePolygon(exterior, interior);
 }
Пример #16
0
 private static SMGeometry SqlGeometryToSharpMapPoint(SqlGeometry geometry, Factory factory)
 {
     var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
     return fact.CreatePoint(new Coordinate( (double)geometry.STX, (double)geometry.STY));
 }
Пример #17
0
 private static SMMultiPolygon SqlGeometryToSharpMapMultiPolygon(SqlGeometry geometry, Factory factory)
 {
     var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
     var polygons = new SMPolygon[(int)geometry.STNumGeometries()];
     for (var i = 1; i <= geometry.STNumGeometries(); i++)
         polygons[i-1] = (SMPolygon)SqlGeometryToSharpMapPolygon(geometry.STGeometryN(i), fact);
     return fact.CreateMultiPolygon(polygons);
 }
Пример #18
0
 private static SMMultiLineString SqlGeometryToSharpMapMultiLineString(SqlGeometry geometry, Factory factory)
 {
     var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
     var lineStrings = new SMLineString[(int)geometry.STNumGeometries()];
     for (var i = 1; i <= geometry.STNumGeometries(); i++)
         lineStrings[i-1] = (SMLineString)SqlGeometryToSharpMapLineString(geometry.STGeometryN(i), fact);
     return fact.CreateMultiLineString(lineStrings);
 }
Пример #19
0
        private static SMMultiPolygon SqlGeographyToSharpMapMultiPolygon(SqlGeography geography, Factory factory)
        {
            var fact     = factory ?? Services.CreateGeometryFactory((int)geography.STSrid);
            var polygons = new SMPolygon[(int)geography.STNumGeometries()];

            for (var i = 1; i <= geography.STNumGeometries(); i++)
            {
                polygons[i - 1] = (SMPolygon)SqlGeographyToSharpMapPolygon(geography.STGeometryN(i), fact);
            }
            return(fact.CreateMultiPolygon(polygons));
        }
Пример #20
0
 /// <summary>
 /// Constructs a <c>MultiPoint</c>.
 /// </summary>
 /// <param name="points">
 /// The <c>Point</c>s for this <c>MultiPoint</c>
 /// , or <c>null</c> or an empty array to create the empty point.
 /// Elements may be empty <c>Point</c>s, but not <c>null</c>s.
 /// </param>
 /// <param name="factory"></param>
 public MultiPoint(GeoAPI.Geometries.IPoint[] points, GeoAPI.Geometries.IGeometryFactory factory) : base(points, factory)
 {
 }
Пример #21
0
 /*
 private static OpenGisGeometryType ParseGeometryType(string stGeometryType)
 {
     switch (stGeometryType.ToUpper())
     {
         case "POINT":
             return OpenGisGeometryType.Point;
         case "LINESTRING":
             return OpenGisGeometryType.LineString;
         case "POLYGON":
             return OpenGisGeometryType.Polygon;
         case "MULTIPOINT":
             return OpenGisGeometryType.MultiPoint;
         case "MULTILINESTRING":
             return OpenGisGeometryType.MultiLineString;
         case "MULTIPOLYGON":
             return OpenGisGeometryType.MultiPolygon;
         case "GEOMETRYCOLLECTION":
             return OpenGisGeometryType.GeometryCollection;
     }
     throw new ArgumentException(String.Format("Invalid geometrytype '{0}'!", stGeometryType), "stGeometryType");
 }
 */
 private static SMGeometryCollection SqlGeometryToSharpMapGeometryCollection(SqlGeometry geometry, Factory factory)
 {
     var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
     var geoms = new SMGeometry[(int)geometry.STNumGeometries()];
     for (var i = 1; i <= geometry.STNumGeometries(); i++)
         geoms[i-1] = ToSharpMapGeometry(geometry.STGeometryN(i), fact);
     return fact.CreateGeometryCollection(geoms);
 }