private static TValue CreateWellKnownValue <TValue>(IDbSpatialValue spatialValue, Func <Exception> onMissingSrid, Func <Exception> onMissingWkbAndWkt, Func <int, byte[], string, TValue> onValidValue)
        {
            int?srid = spatialValue.CoordinateSystemId;

            if (!srid.HasValue)
            {
                throw onMissingSrid();
            }

            string wkt = spatialValue.WellKnownText;

            if (wkt != null)
            {
                return(onValidValue(srid.Value, null, wkt));
            }
            else
            {
                byte[] wkb = spatialValue.WellKnownBinary;
                if (wkb != null)
                {
                    return(onValidValue(srid.Value, wkb, null));
                }
            }

            throw onMissingWkbAndWkt();
        }
Ejemplo n.º 2
0
        private object GetSqlTypesSpatialValue(IDbSpatialValue spatialValue, Type requiredProviderValueType)
        {
            DebugCheck.NotNull(spatialValue);

            // If the specified value was created by this spatial services implementation, its underlying Microsoft.SqlServer.Types.SqlGeography value is available via the ProviderValue property.
            var providerValue = spatialValue.ProviderValue;

            if (providerValue != null &&
                providerValue.GetType() == requiredProviderValueType)
            {
                return(providerValue);
            }

            // Otherwise, attempt to retrieve a Well Known Binary, Well Known Text or GML (in descending order of preference) representation of the value that can be used to create an appropriate Microsoft.SqlServer.Types.SqlGeography/SqlGeometry value

            var srid = spatialValue.CoordinateSystemId;

            if (srid.HasValue)
            {
                // Well Known Binary (WKB)
                var binaryValue = spatialValue.WellKnownBinary;
                if (binaryValue != null)
                {
                    return(spatialValue.IsGeography
                                ? sqlGeographyFromWKBByteArray(binaryValue, srid.Value)
                                : sqlGeometryFromWKBByteArray(binaryValue, srid.Value));
                }

                // Well Known Text (WKT)
                var textValue = spatialValue.WellKnownText;
                if (textValue != null)
                {
                    return(spatialValue.IsGeography
                                ? sqlGeographyFromWKTString(textValue, srid.Value)
                                : sqlGeometryFromWKTString(textValue, srid.Value));
                }

                // Geography Markup Language (GML), as a string
                var gmlValue = spatialValue.GmlString;
                if (gmlValue != null)
                {
                    var xmlReader = XmlReaderFromString(gmlValue);
                    return(spatialValue.IsGeography
                                ? sqlGeographyFromGMLReader(xmlReader, srid.Value)
                                : sqlGeometryFromGMLReader(xmlReader, srid.Value));
                }
            }

            throw spatialValue.NotSqlCompatible();
        }