Esempio n. 1
0
        public override FeatureDataRow GetFeature(uint rowId)
        {
            GetNonSpatialColumns();
            using (var conn = GetConnection(ConnectionString))
            {
                var strSql = "SELECT " + _columns + ", \"" + GeometryColumn + "\" AS \"_smtmp_\" ";
                strSql += "FROM " + Table + " WHERE \"" + ObjectIdColumn +"\" = " + rowId;

                using (var cmd = new SQLiteCommand(strSql, conn))
                {
                    using (var reader = cmd.ExecuteReader())
                    {
                        var geomIndex = reader.FieldCount - 1;
                        var fdt = CreateTableFromReader(reader, geomIndex);

                        var dataTransfer = new object[geomIndex];
                        var geoReader = new GaiaGeoReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel,
                                                          _ordinates);
                        fdt.BeginLoadData();
                        while (reader.Read())
                        {
                            IGeometry g = null;
                            if (!reader.IsDBNull(geomIndex))
                                g = geoReader.Read((byte[])reader.GetValue(geomIndex));

                            //No geometry, no feature!
                            if (g == null)
                                continue;

                            //Get all the attribute data
                            var count = reader.GetValues(dataTransfer);
                            System.Diagnostics.Debug.Assert(count == dataTransfer.Length);

                            var fdr = (FeatureDataRow)fdt.LoadDataRow(dataTransfer, true);
                            fdr.Geometry = g;
                        }
                        reader.Close();
                        fdt.EndLoadData();
                        return (FeatureDataRow)fdt.Rows[0];
                    }
                }
            }
        }
Esempio n. 2
0
        public override Envelope GetExtents()
        {
            if (_cachedExtents != null)
                return new Envelope(_cachedExtents);

            Envelope box = null;

            if (_spatiaLiteIndex == SpatiaLiteIndex.RTree)
            {
                using (var conn = GetConnection(ConnectionString))
                {
                    var strSQL = string.Format("SELECT \"data\" FROM \"idx_{0}_{1}_node\" WHERE \"nodeno\"=1;",
                                                _table, _geometryColumn);

                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = strSQL;
                        var result = cmd.ExecuteScalar();
                        if (result != null && result != DBNull.Value)
                        {
                            var buffer = (byte[])result;
                            var node = new RTreeNode(buffer);
                            _cachedExtents = float.IsNaN(node.XMin) 
                                ? new Envelope() 
                                : new Envelope(node.XMin, node.XMax, node.YMin, node.YMax);
                            return new Envelope(_cachedExtents);
                        }
                        throw new Exception();
                    }
                }
            }
            using (var conn = GetConnection(ConnectionString))
            {
                var strSQL = string.Format("SELECT \"{1}\" AS \"_smtmp_\" FROM \"{0}\"",
                                           _table, _geometryColumn);

                using (var command = new SQLiteCommand(strSQL, conn))
                {
                    using (var dr = command.ExecuteReader())
                    {
                        if (dr.HasRows)
                        {
                            double minx = double.MaxValue,
                                   miny = double.MaxValue,
                                   maxx = double.MinValue,
                                   maxy = double.MinValue;
                            var geoReader = new GaiaGeoReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel,
                                                              _ordinates);

                            while (dr.Read())
                            {
                                var geom = geoReader.Read((byte[]) dr.GetValue(0));

                                var env = geom.EnvelopeInternal;
                                if (minx > env.MinX)
                                    minx = env.MinX;
                                if (miny > env.MinY)
                                    miny = env.MinY;
                                if (maxx < env.MaxX)
                                    maxx = env.MaxX;
                                if (maxy < env.MaxY)
                                    maxy = env.MaxY;

                                box = new Envelope(minx, maxx, miny, maxy);
                            }
                        }
                        else
                        {
                            box = new Envelope();
                        }
                        dr.Close();
                    }
                }
                conn.Close();
                _cachedExtents = box;
                return new Envelope(box);
            }
        }
Esempio n. 3
0
        public override void ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds)
        {
            GetNonSpatialColumns();
            using (var conn = GetConnection(ConnectionString))
            {
                var strSql = "SELECT " + _columns + ", \"" + GeometryColumn + "\" AS \"_smtmp_\" ";
                strSql += "FROM " + Table + " WHERE ";

                // Attribute constraint
                if (!String.IsNullOrEmpty(_definitionQuery))
                    strSql += DefinitionQuery + " AND ";
                
                // Spatial constraint
                strSql += GetBoxClause(box);

                using (var cmd = new SQLiteCommand(strSql, conn))
                {
                    using (var reader = cmd.ExecuteReader())
                    {
                        var geomIndex = reader.FieldCount - 1;
                        var fdt = CreateTableFromReader(reader, geomIndex);

                        var dataTransfer = new object[geomIndex];
                        var geoReader = new GaiaGeoReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel,
                                                          _ordinates);
                        fdt.BeginLoadData();
                        while (reader.Read())
                        {
                            IGeometry g = null;
                            if (!reader.IsDBNull(geomIndex))
                                g = geoReader.Read((byte[])reader.GetValue(geomIndex));

                            //No geometry, no feature!
                            if (g == null)
                                continue;

                            //If not using RTree index we need to filter in code
                            if (_spatiaLiteIndex != SpatiaLiteIndex.RTree && !box.Intersects(g.EnvelopeInternal))
                                continue;

                            //Get all the attribute data
                            var count = reader.GetValues(dataTransfer);
                            System.Diagnostics.Debug.Assert(count == dataTransfer.Length);

                            var fdr = (FeatureDataRow)fdt.LoadDataRow(dataTransfer, true);
                            fdr.Geometry = g;
                        }
                        reader.Close();
                        fdt.EndLoadData();
                        ds.Tables.Add(fdt);
                    }
                }
            }
        }
Esempio n. 4
0
 public override IGeometry GetGeometryByID(uint oid)
 {
     IGeometry geom = null;
     var reader = new GaiaGeoReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel, _ordinates);
     using (var conn = GetConnection(ConnectionString))
     {
         string strSql = "SELECT \"" + GeometryColumn + "\" AS Geom FROM \"" + Table + "\" WHERE \"" +
                         ObjectIdColumn + "\" = " + oid;
         using (var command = new SQLiteCommand(strSql, conn))
         {
             using (var dr = command.ExecuteReader())
             {
                 while (dr.Read())
                 {
                     if (!dr.IsDBNull(0))
                     {
                         geom = reader.Read((byte[]) dr[0]);
                     }
                 }
                 dr.Close();
             }
         }
         conn.Close();
     }
     return geom;
 }
Esempio n. 5
0
        public override Collection<uint> GetObjectIDsInView(Envelope bbox)
        {
            var objectlist = new Collection<uint>();
            using (var conn = GetConnection(ConnectionString))
            {
                string strSql = "SELECT \"" + ObjectIdColumn + "\" ";
                if (_spatiaLiteIndex != SpatiaLiteIndex.RTree)
                    strSql += ", \"" + _geometryColumn + "\" ";

                strSql += "FROM " + Table + " WHERE ";

                if (!String.IsNullOrEmpty(_definitionQuery))
                    strSql += DefinitionQuery + " AND ";

                strSql += GetBoxClause(bbox);


                using (var command = new SQLiteCommand(strSql, conn))
                {
                    using (var dr = command.ExecuteReader())
                    {
                        var geoReader = new GaiaGeoReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel,
                                                          _ordinates);
                        while (dr.Read())
                        {
                            if (dr.IsDBNull(0))
                                continue;

                            //If we didn't have a spatial index we need to compare geometries manually
                            if (_spatiaLiteIndex != SpatiaLiteIndex.RTree)
                            {
                                if (dr.IsDBNull(1))
                                    continue;

                                var geom = geoReader.Read((byte[])dr.GetValue(1));
                                if (geom == null)
                                    continue;

                                if (!bbox.Intersects(geom.EnvelopeInternal))
                                    continue;
                            }

                            var id = Convert.ToUInt32(dr[0]);
                            objectlist.Add(id);
                        }
                        dr.Close();
                    }
                    conn.Close();
                }
            }
            return objectlist;
        }
Esempio n. 6
0
        public override Collection<IGeometry> GetGeometriesInView(Envelope bbox)
        {
            var features = new Collection<IGeometry>();
            using (var conn = GetConnection(ConnectionString))
            {
                var strSql = "SELECT \"" + GeometryColumn + "\" AS \"_smtmp_\" ";
                strSql += "FROM " + Table + " WHERE ";
                if (!String.IsNullOrEmpty(_definitionQuery))
                    strSql += DefinitionQuery + " AND ";
                strSql += GetBoxClause(bbox);

                using (var command = new SQLiteCommand(strSql, conn))
                {
                    using (var dr = command.ExecuteReader())
                    {
                        var geoReader = new GaiaGeoReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel,
                                                          _ordinates);
                        while (dr.Read())
                        {
                            if (dr.IsDBNull(0))
                                continue;

                            var geom = geoReader.Read((byte[])dr.GetValue(0));

                            //No geometry to add
                            if (geom == null)
                                continue;

                            //If we didn't have a spatial index we need to compare geometries manually
                            if (_spatiaLiteIndex != SpatiaLiteIndex.RTree && !bbox.Intersects(geom.EnvelopeInternal))
                                continue;

                            features.Add(geom);
                        }
                        dr.Close();
                    }
                }
            }
            return features;
        }
Esempio n. 7
0
        public override IEnumerable<Feature> GetFeatures(FeatureType featureType, Layer layer, Envelope window)
        {
            var cmd = _useCounter > 0 ? CreateCommand(Connection) : GetOrCreateCommand(Connection);
            _useCounter++;

            try
            {

                SetParameters(cmd, window);
                using (var reader = cmd.ExecuteReader())
                {
                    var geomOrdinal = reader.GetOrdinal(GeometryColumn);
                    if (geomOrdinal < 0) throw new Exception("Geometry column not found: " + GeometryColumn);

                    var fidOrdinal = string.IsNullOrEmpty(FidColumn) ? -1 : reader.GetOrdinal(FidColumn);
                    var buffer = new byte[1024];

                    var fieldCount = reader.FieldCount;
                    var values = new object[fieldCount];
                    var geoReader = new GaiaGeoReader();

                    while (reader.Read())
                    {
                        long fid;
                        if (fidOrdinal >= 0)
                        {
                            fid = reader.GetInt64(fidOrdinal);
                            values[fidOrdinal] = fid;
                        }
                        else
                            fid = 0;

                        for (int i = 0; i < fieldCount; i++)
                            if (i != fidOrdinal && i != geomOrdinal)
                                values[i] = reader.GetValue(i);

                        var len = (int)reader.GetBytes(geomOrdinal, 0L, null, 0, 0);
                        if (len > buffer.Length)
                            buffer = new byte[len];

                        reader.GetBytes(geomOrdinal, 0L, buffer, 0, len);

                        IGeometry geom;
                        try
                        {
                            geom = geoReader.Read(buffer, 0, len);
                            if (geom == null) continue;
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine(string.Format("Invalid geometry while reading {0} Fid={1}: {2}", ToString(), fid, ex.Message));
                            continue;
                        }

                        //var geom = GeometryReader.ReadBlobGeometry(new MemoryStream(buffer, 0, buffer.Length));
                        values[geomOrdinal] = geom;
                        //featureType.GeometryType = geom.OgcGeometryType;
                        var feature = featureType.NewFeature(fid, values);
                        feature.Geometry = geom;

                        yield return feature;
                    }
                }
            }
            finally
            {
                _useCounter--;
            }
        }