Ejemplo n.º 1
0
        public override DbCommand SelectSpatialReferenceIds(gView.Framework.OGC.DB.OgcSpatialFeatureclass fc)
        {
            string    cmdText = "select top(100) " + ToDbName(fc.ShapeFieldName) + ".STSrid as srid from " + ToDbName(fc.Name) + " where " + ToDbName(fc.ShapeFieldName) + " is not null";
            DbCommand command = this.ProviderFactory.CreateCommand();

            command.CommandText = cmdText;

            return(command);
        }
Ejemplo n.º 2
0
        public override DbCommand SelectSpatialReferenceIds(gView.Framework.OGC.DB.OgcSpatialFeatureclass fc)
        {
            //string cmdText = "select distinct " + fc.ShapeFieldName + ".STSrid as srid from " + fc.Name + " where " + fc.ShapeFieldName + " is not null";
            //DbCommand command = this.ProviderFactory.CreateCommand();
            //command.CommandText = cmdText;

            //return command;
            return(null);
        }
Ejemplo n.º 3
0
        virtual protected object ShapeParameterValue(OgcSpatialFeatureclass featureClass, IGeometry shape, int srid, out bool AsSqlParameter)
        {
            AsSqlParameter = true;

            byte[] geometry       = gView.Framework.OGC.OGC.GeometryToWKB(shape, 0, gView.Framework.OGC.OGC.WkbByteOrder.Ndr);
            string geometryString = gView.Framework.OGC.OGC.BytesToHexString(geometry);

            return(geometryString);
        }
Ejemplo n.º 4
0
        async virtual public Task <IDatasetElement> Element(string title)
        {
            try
            {
                DataTable tab = new DataTable();
                using (DbConnection conn = this.ProviderFactory.CreateConnection())
                {
                    conn.ConnectionString = _connectionString;
                    await conn.OpenAsync();

                    DbDataAdapter adapter = this.ProviderFactory.CreateDataAdapter();
                    adapter.SelectCommand = this.ProviderFactory.CreateCommand();

                    if (title.Contains("."))
                    {
                        string schema = title.Split('.')[0];
                        string table  = title.Substring(schema.Length + 1);
                        adapter.SelectCommand.CommandText = "SELECT * FROM " + DbSchemaPrefix + OgcDictionary("geometry_columns") + " WHERE " + OgcDictionary("geometry_columns.f_table_name") + "='" + table + "' AND " + OgcDictionary("geometry_columns.f_table_schema") + "='" + schema + "'";
                    }
                    else
                    {
                        adapter.SelectCommand.CommandText = "SELECT * FROM " + DbSchemaPrefix + OgcDictionary("geometry_columns") + " WHERE " + OgcDictionary("geometry_columns.f_table_name") + "='" + title + "'";
                    }
                    adapter.SelectCommand.Connection = conn;

                    adapter.Fill(tab);
                    conn.Close();
                }
                if (tab.Rows.Count != 1)
                {
                    _errMsg = "Layer not found: " + title;
                    return(null);
                }

                return(new DatasetElement(await OgcSpatialFeatureclass.Create(this, tab.Rows[0])));
            }
            catch (Exception ex)
            {
                _errMsg = ex.Message;
                return(null);
            }
        }
Ejemplo n.º 5
0
        async virtual public Task <List <IDatasetElement> > Elements()
        {
            if (_layers == null || _layers.Count == 0)
            {
                List <IDatasetElement> layers = new List <IDatasetElement>();
                DataTable tab = new DataTable();
                try
                {
                    using (DbConnection conn = this.ProviderFactory.CreateConnection())
                    {
                        conn.ConnectionString = _connectionString;
                        conn.Open();

                        DbDataAdapter adapter = this.ProviderFactory.CreateDataAdapter();
                        adapter.SelectCommand             = this.ProviderFactory.CreateCommand();
                        adapter.SelectCommand.CommandText = "SELECT * FROM " + DbSchemaPrefix + OgcDictionary("geometry_columns");
                        adapter.SelectCommand.Connection  = conn;
                        adapter.Fill(tab);
                        conn.Close();
                    }
                }
                catch (Exception ex)
                {
                    _errMsg = ex.Message;
                    return(layers);
                }

                foreach (DataRow row in tab.Rows)
                {
                    IFeatureClass fc = await OgcSpatialFeatureclass.Create(this, row);

                    layers.Add(new DatasetElement(fc));
                }

                _layers = layers;
            }
            return(_layers);
        }
Ejemplo n.º 6
0
        public override DbCommand SelectCommand(gView.Framework.OGC.DB.OgcSpatialFeatureclass fc, IQueryFilter filter, out string shapeFieldName)
        {
            shapeFieldName = String.Empty;

            DbCommand command = this.ProviderFactory.CreateCommand();

            filter.fieldPrefix  = "[";
            filter.fieldPostfix = "]";

            if (filter.SubFields == "*")
            {
                filter.SubFields = "";

                foreach (IField field in fc.Fields.ToEnumerable())
                {
                    filter.AddField(field.name);
                }
                filter.AddField(fc.IDFieldName);
                filter.AddField(fc.ShapeFieldName);
            }
            else
            {
                filter.AddField(fc.IDFieldName);
            }

            string where = String.Empty;
            if (filter is ISpatialFilter && ((ISpatialFilter)filter).Geometry != null)
            {
                ISpatialFilter sFilter = filter as ISpatialFilter;


                if (sFilter.SpatialRelation == spatialRelation.SpatialRelationMapEnvelopeIntersects ||
                    sFilter.Geometry is IEnvelope)
                {
                    IEnvelope env = sFilter.Geometry.Envelope;

                    where  = fc.ShapeFieldName + ".Filter(";
                    where += "geography::STGeomFromText('POLYGON((";
                    where += Math.Max(-179.99, env.minx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + ",";

                    where += Math.Min(179.99, env.maxx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + ",";

                    where += Math.Min(179.99, env.maxx).ToString(_nhi) + " ";
                    where += Math.Min(89.99, env.maxy).ToString(_nhi) + ",";

                    where += Math.Max(-179.99, env.minx).ToString(_nhi) + " ";
                    where += Math.Min(89.99, env.maxy).ToString(_nhi) + ",";

                    where += Math.Max(-179.99, env.minx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + "))',4326))=1";
                }
                else if (sFilter.Geometry != null)
                {
                    IEnvelope env = sFilter.Geometry.Envelope;

                    where  = fc.ShapeFieldName + ".STIntersects(";
                    where += "geography::STGeomFromText('POLYGON((";
                    where += Math.Max(-180.0, env.minx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + ",";

                    where += Math.Max(-180.0, env.minx).ToString(_nhi) + " ";
                    where += Math.Min(89.99, env.maxy).ToString(_nhi) + ",";

                    where += Math.Min(180.0, env.maxx).ToString(_nhi) + " ";
                    where += Math.Min(89.99, env.maxy).ToString(_nhi) + ",";

                    where += Math.Min(180.0, env.maxx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + ",";

                    where += Math.Max(-180.0, env.minx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + "))',4326))=1";
                }
                filter.AddField(fc.ShapeFieldName);
            }

            string filterWhereClause = (filter is IRowIDFilter) ? ((IRowIDFilter)filter).RowIDWhereClause : filter.WhereClause;

            StringBuilder fieldNames = new StringBuilder();

            foreach (string fieldName in filter.SubFields.Split(' '))
            {
                if (fieldNames.Length > 0)
                {
                    fieldNames.Append(",");
                }
                if (fieldName == "[" + fc.ShapeFieldName + "]")
                {
                    fieldNames.Append(fc.ShapeFieldName + ".STAsBinary() as temp_geometry");
                    shapeFieldName = "temp_geometry";
                }
                else
                {
                    fieldNames.Append(fieldName);
                }
            }

            command.CommandText = "SELECT " + fieldNames + " FROM " + fc.Name;
            if (!String.IsNullOrEmpty(where))
            {
                command.CommandText += " WHERE " + where + ((filterWhereClause != "") ? " AND " + filterWhereClause : "");
            }
            else if (!String.IsNullOrEmpty(filterWhereClause))
            {
                command.CommandText += " WHERE " + filterWhereClause;
            }

            //command.CommandText = "SELECT " + fieldNames + " FROM " + table + ((filterWhereClause != "") ? " WHERE " + filterWhereClause : "");

            return(command);
        }
Ejemplo n.º 7
0
        public override DbCommand SelectCommand(gView.Framework.OGC.DB.OgcSpatialFeatureclass fc, IQueryFilter filter, out string shapeFieldName, string functionName = "", string functionField = "", string functionAlias = "")
        {
            shapeFieldName = String.Empty;

            DbCommand command = this.ProviderFactory.CreateCommand();

            filter.fieldPrefix  = "[";
            filter.fieldPostfix = "]";

            if (filter.QuerySubFields.Contains("*"))
            {
                // var subFieldFunctions = filter.QuerySubFields.Where(f => f.IsSubFieldFunction());
                filter.SubFields = "";

                foreach (IField field in fc.Fields.ToEnumerable())
                {
                    filter.AddField(field.name);
                }
                filter.AddField(fc.IDFieldName);
                filter.AddField(fc.ShapeFieldName);

                //foreach(var subFieldFunction in subFieldFunctions)
                //{
                //    filter.AddField(subFieldFunction);
                //}
            }
            else
            {
                filter.AddField(fc.IDFieldName);
            }

            string where = String.Empty;
            if (filter is ISpatialFilter && ((ISpatialFilter)filter).Geometry != null)
            {
                ISpatialFilter sFilter = filter as ISpatialFilter;

                int srid = 0;
                try
                {
                    if (fc.SpatialReference != null && fc.SpatialReference.Name.ToLower().StartsWith("epsg:"))
                    {
                        srid = Convert.ToInt32(fc.SpatialReference.Name.Split(':')[1]);
                    }
                }
                catch { }

                if (sFilter.SpatialRelation == spatialRelation.SpatialRelationMapEnvelopeIntersects /*|| sFilter.Geometry is IEnvelope*/)
                {
                    IEnvelope env = sFilter.Geometry.Envelope;

                    where  = fc.ShapeFieldName + ".Filter(";
                    where += "geometry::STGeomFromText('POLYGON((";
                    where += env.minx.ToString(_nhi) + " ";
                    where += env.miny.ToString(_nhi) + ",";

                    where += env.minx.ToString(_nhi) + " ";
                    where += env.maxy.ToString(_nhi) + ",";

                    where += env.maxx.ToString(_nhi) + " ";
                    where += env.maxy.ToString(_nhi) + ",";

                    where += env.maxx.ToString(_nhi) + " ";
                    where += env.miny.ToString(_nhi) + ",";

                    where += env.minx.ToString(_nhi) + " ";
                    where += env.miny.ToString(_nhi) + "))'," + srid + "))=1";
                }
                else if (sFilter.Geometry != null)
                {
                    IEnvelope env = sFilter.Geometry.Envelope;

                    where  = fc.ShapeFieldName + ".STIntersects(";
                    where += "geometry::STGeomFromText('POLYGON((";
                    where += env.minx.ToString(_nhi) + " ";
                    where += env.miny.ToString(_nhi) + ",";

                    where += env.minx.ToString(_nhi) + " ";
                    where += env.maxy.ToString(_nhi) + ",";

                    where += env.maxx.ToString(_nhi) + " ";
                    where += env.maxy.ToString(_nhi) + ",";

                    where += env.maxx.ToString(_nhi) + " ";
                    where += env.miny.ToString(_nhi) + ",";

                    where += env.minx.ToString(_nhi) + " ";
                    where += env.miny.ToString(_nhi) + "))'," + srid + "))=1";
                }
                filter.AddField(fc.ShapeFieldName);
            }

            if (!String.IsNullOrWhiteSpace(functionName) && !String.IsNullOrWhiteSpace(functionField))
            {
                filter.SubFields = "";
                filter.AddField(functionName + "(" + filter.fieldPrefix + functionField + filter.fieldPostfix + ")");
            }

            string filterWhereClause = (filter is IRowIDFilter) ? ((IRowIDFilter)filter).RowIDWhereClause : filter.WhereClause;

            StringBuilder fieldNames = new StringBuilder();

            foreach (string fieldName in filter.QuerySubFields)
            {
                if (fieldNames.Length > 0)
                {
                    fieldNames.Append(",");
                }

                if (fieldName == "[" + fc.ShapeFieldName + "]")
                {
                    fieldNames.Append(fc.ShapeFieldName + ".STAsBinary() as temp_geometry");
                    shapeFieldName = "temp_geometry";
                }
                else
                {
                    fieldNames.Append(fieldName);
                }
            }

            string limit = String.Empty, top = String.Empty, orderBy = String.Empty;

            if (!String.IsNullOrWhiteSpace(filter.OrderBy))
            {
                orderBy = " order by " + filter.OrderBy;
            }

            if (filter.Limit > 0)
            {
                if (String.IsNullOrEmpty(fc.IDFieldName) && String.IsNullOrWhiteSpace(orderBy))
                {
                    top = "top(" + filter.Limit + ") ";
                }
                else
                {
                    if (String.IsNullOrWhiteSpace(orderBy))
                    {
                        orderBy = " order by " + filter.fieldPrefix + fc.IDFieldName + filter.fieldPostfix;
                    }

                    limit = " offset " + Math.Max(0, filter.BeginRecord - 1) + " rows fetch next " + filter.Limit + " rows only";
                }
            }

            string fcName = fc.Name;

            if (fc is SdeFeatureClass && !String.IsNullOrWhiteSpace(((SdeFeatureClass)fc).MultiVersionedViewName))
            {
                fcName = ((SdeFeatureClass)fc).MultiVersionedViewName;

                // ToDo: filterWhereClause? SDE_STATE_ID=0??
            }

            command.CommandText = "SELECT " + top + fieldNames + " FROM " + fcName;

            if (!String.IsNullOrEmpty(where))
            {
                command.CommandText += " WHERE " + where + ((filterWhereClause != "") ? $" AND ({ filterWhereClause })" : "");
            }
            else if (!String.IsNullOrEmpty(filterWhereClause))
            {
                command.CommandText += " WHERE " + filterWhereClause;
            }
            command.CommandText += orderBy + limit;

            return(command);
        }
Ejemplo n.º 8
0
        public override DbCommand SelectCommand(gView.Framework.OGC.DB.OgcSpatialFeatureclass fc, IQueryFilter filter, out string shapeFieldName, string functionName = "", string functionField = "", string functionAlias = "")
        {
            shapeFieldName = String.Empty;

            DbCommand command = this.ProviderFactory.CreateCommand();

            filter.fieldPrefix  = "[";
            filter.fieldPostfix = "]";

            if (filter.SubFields == "*")
            {
                filter.SubFields = "";

                foreach (IField field in fc.Fields.ToEnumerable())
                {
                    filter.AddField(field.name);
                }
                filter.AddField(fc.IDFieldName);
                filter.AddField(fc.ShapeFieldName);
            }
            else
            {
                filter.AddField(fc.IDFieldName);
            }

            string where = String.Empty;
            if (filter is ISpatialFilter && ((ISpatialFilter)filter).Geometry != null)
            {
                ISpatialFilter sFilter = filter as ISpatialFilter;


                if (sFilter.SpatialRelation == spatialRelation.SpatialRelationMapEnvelopeIntersects /*|| sFilter.Geometry is IEnvelope*/)
                {
                    IEnvelope env = sFilter.Geometry.Envelope;

                    where  = fc.ShapeFieldName + ".Filter(";
                    where += "geography::STGeomFromText('POLYGON((";
                    where += Math.Max(-179.99, env.minx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + ",";

                    where += Math.Min(179.99, env.maxx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + ",";

                    where += Math.Min(179.99, env.maxx).ToString(_nhi) + " ";
                    where += Math.Min(89.99, env.maxy).ToString(_nhi) + ",";

                    where += Math.Max(-179.99, env.minx).ToString(_nhi) + " ";
                    where += Math.Min(89.99, env.maxy).ToString(_nhi) + ",";

                    where += Math.Max(-179.99, env.minx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + "))',4326))=1";
                }
                else if (sFilter.Geometry != null)
                {
                    IEnvelope env = sFilter.Geometry.Envelope;

                    where  = fc.ShapeFieldName + ".STIntersects(";
                    where += "geography::STGeomFromText('POLYGON((";
                    where += Math.Max(-180.0, env.minx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + ",";

                    where += Math.Max(-180.0, env.minx).ToString(_nhi) + " ";
                    where += Math.Min(89.99, env.maxy).ToString(_nhi) + ",";

                    where += Math.Min(180.0, env.maxx).ToString(_nhi) + " ";
                    where += Math.Min(89.99, env.maxy).ToString(_nhi) + ",";

                    where += Math.Min(180.0, env.maxx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + ",";

                    where += Math.Max(-180.0, env.minx).ToString(_nhi) + " ";
                    where += Math.Max(-89.99, env.miny).ToString(_nhi) + "))',4326))=1";
                }
                filter.AddField(fc.ShapeFieldName);
            }

            if (!String.IsNullOrWhiteSpace(functionName) && !String.IsNullOrWhiteSpace(functionField))
            {
                filter.SubFields = "";
                filter.AddField(functionName + "(" + filter.fieldPrefix + functionField + filter.fieldPostfix + ")");
            }

            string filterWhereClause = (filter is IRowIDFilter) ? ((IRowIDFilter)filter).RowIDWhereClause : filter.WhereClause;

            StringBuilder fieldNames = new StringBuilder();

            foreach (string fieldName in filter.SubFields.Split(' '))
            {
                if (fieldNames.Length > 0)
                {
                    fieldNames.Append(",");
                }
                if (fieldName == "[" + fc.ShapeFieldName + "]")
                {
                    fieldNames.Append(fc.ShapeFieldName + ".STAsBinary() as temp_geometry");
                    shapeFieldName = "temp_geometry";
                }
                else
                {
                    fieldNames.Append(fieldName);
                }
            }

            string limit = String.Empty, top = String.Empty, orderBy = String.Empty;

            if (!String.IsNullOrWhiteSpace(filter.OrderBy))
            {
                orderBy = " order by " + filter.OrderBy;
            }

            if (filter.Limit > 0)
            {
                if (String.IsNullOrEmpty(fc.IDFieldName) && String.IsNullOrWhiteSpace(orderBy))
                {
                    top = "top(" + filter.Limit + ") ";
                }
                else
                {
                    if (String.IsNullOrWhiteSpace(orderBy))
                    {
                        orderBy = " order by " + filter.fieldPrefix + fc.IDFieldName + filter.fieldPostfix;
                    }

                    limit = " offset " + Math.Max(0, filter.BeginRecord - 1) + " rows fetch next " + filter.Limit + " rows only";
                }
            }

            command.CommandText = "SELECT " + limit + fieldNames + " FROM " + fc.Name;

            if (!String.IsNullOrEmpty(where))
            {
                command.CommandText += " WHERE " + where + ((filterWhereClause != "") ? $" AND ({ filterWhereClause })" : "");
            }
            else if (!String.IsNullOrEmpty(filterWhereClause))
            {
                command.CommandText += " WHERE " + filterWhereClause;
            }

            command.CommandText += orderBy + limit;

            return(command);
        }
Ejemplo n.º 9
0
        async static public Task <IFeatureCursor> Create(OgcSpatialFeatureclass fc, IQueryFilter filter)
        {
            var featureCursor = new OgcSpatialFeatureCursor(fc, filter);

            if (fc == null || fc.Dataset == null)
            {
                return(featureCursor);
            }

            featureCursor._idField = fc.IDFieldName;
            if (filter is ISpatialFilter)
            {
                featureCursor._spatialfilter = (ISpatialFilter)filter;
            }

            try
            {
                if (fc.SpatialReference != null &&
                    filter is ISpatialFilter &&
                    ((ISpatialFilter)filter).FilterSpatialReference != null &&
                    !((ISpatialFilter)filter).FilterSpatialReference.Equals(fc.SpatialReference))
                {
                    filter = (ISpatialFilter)filter.Clone();

                    ((ISpatialFilter)filter).Geometry =
                        GeometricTransformerFactory.Transform2D(((ISpatialFilter)filter).Geometry,
                                                                ((ISpatialFilter)filter).FilterSpatialReference,
                                                                fc.SpatialReference);
                    ((ISpatialFilter)filter).FilterSpatialReference = null;
                    if (((ISpatialFilter)filter).SpatialRelation == spatialRelation.SpatialRelationMapEnvelopeIntersects &&
                        ((ISpatialFilter)filter).Geometry != null)
                    {
                        ((ISpatialFilter)filter).Geometry = ((ISpatialFilter)filter).Geometry.Envelope;
                    }

                    featureCursor._spatialfilter = (ISpatialFilter)filter;
                }
                DbCommand command = ((OgcSpatialDataset)fc.Dataset).SelectCommand(
                    fc, filter, out featureCursor._shapeField);
                if (command == null)
                {
                    return(featureCursor);
                }

                featureCursor._subFields = filter.QuerySubFields.ToArray();

                featureCursor._conn = ((OgcSpatialDataset)fc.Dataset).ProviderFactory.CreateConnection();
                featureCursor._conn.ConnectionString = fc.Dataset.ConnectionString;

                command.Connection = featureCursor._conn;

                if (featureCursor._conn.State != ConnectionState.Closed)
                {
                    try
                    {
                        featureCursor._conn.Close();
                    }
                    catch { }
                }
                await featureCursor._conn.OpenAsync();

                command.SetCustomCursorTimeout();
                featureCursor._reader = await command.ExecuteReaderAsync();

                return(featureCursor);
            }
            catch (Exception ex)
            {
                if (featureCursor._fc != null)
                {
                    featureCursor._fc.LastException = ex;
                }

                if (featureCursor._conn != null && featureCursor._conn.State != ConnectionState.Closed)
                {
                    featureCursor._conn.Close();
                    featureCursor._conn = null;
                }

                fc.LastException = ex;

                return(null);
            }
        }
Ejemplo n.º 10
0
 private OgcSpatialFeatureCursor(OgcSpatialFeatureclass fc, IQueryFilter filter)
     : base((fc != null) ? fc.SpatialReference : null,
            (filter != null) ? filter.FeatureSpatialReference : null)
 {
     base.CancelTracker = filter?.CancelTracker;
 }
Ejemplo n.º 11
0
 virtual protected string InsertShapeParameterExpression(OgcSpatialFeatureclass featureClass, IGeometry shape)
 {
     return("{0}");
 }
Ejemplo n.º 12
0
 virtual public DbCommand SelectSpatialReferenceIds(OgcSpatialFeatureclass fc)
 {
     return(null);
 }
Ejemplo n.º 13
0
        virtual public DbCommand SelectCommand(OgcSpatialFeatureclass fc, IQueryFilter filter, out string shapeFieldName)
        {
            shapeFieldName = String.Empty;

            filter.fieldPostfix = filter.fieldPrefix = "\"";
            if (filter.SubFields == "*")
            {
                filter.SubFields = "";

                foreach (IField field in fc.Fields.ToEnumerable())
                {
                    filter.AddField(field.name);
                }
                filter.AddField(fc.IDFieldName);
                filter.AddField(fc.ShapeFieldName);
            }
            else
            {
                filter.AddField(fc.IDFieldName);
            }

            string where = "";
            if (filter is ISpatialFilter)
            {
                ISpatialFilter sFilter = filter as ISpatialFilter;
                if (sFilter.Geometry is IEnvelope)
                {
                    where = fc.ShapeFieldName + " && " + gView.Framework.OGC.OGC.Envelope2box2(sFilter.Geometry as IEnvelope, fc.SpatialReference);
                }
                else if (sFilter.Geometry != null)
                {
                    where = fc.ShapeFieldName + " && " + gView.Framework.OGC.OGC.Envelope2box2(sFilter.Geometry.Envelope, fc.SpatialReference);
                }
                filter.AddField(fc.ShapeFieldName);
            }
            string filterWhereClause = (filter is IRowIDFilter) ? ((IRowIDFilter)filter).RowIDWhereClause : filter.WhereClause;

            if (where != "" && filterWhereClause != "")
            {
                where += " AND ";
            }
            where += filterWhereClause;

            StringBuilder fieldNames = new StringBuilder();

            foreach (string fieldName in filter.SubFields.Split(' '))
            {
                if (fieldNames.Length > 0)
                {
                    fieldNames.Append(",");
                }
                if (fieldName == "\"" + fc.ShapeFieldName + "\"")
                {
                    fieldNames.Append("ST_AsBinary(\"" + fc.ShapeFieldName + "\") as temp_geometry");
                    shapeFieldName = "temp_geometry";
                }
                else
                {
                    fieldNames.Append(fieldName);
                }
            }

            DbCommand command = ((OgcSpatialDataset)fc.Dataset).ProviderFactory.CreateCommand();

            command.CommandText = "SELECT " + fieldNames + " FROM " + DbTableName(fc.Name) + ((where != "") ? " WHERE " + where : "");

            return(command);
        }
Ejemplo n.º 14
0
        async public static Task <IFeatureClass> Create(OgcSpatialDataset dataset, DataRow geometry_columns_row)
        {
            var featureClass = new OgcSpatialFeatureclass(dataset, geometry_columns_row);

            featureClass._dataset = dataset;

            if (featureClass._dataset == null || geometry_columns_row == null)
            {
                return(featureClass);
            }

            try
            {
                featureClass._lastException = null;

                string schema = String.Empty;
                try
                {
                    if (!String.IsNullOrEmpty(featureClass._dataset.OgcDictionary("geometry_columns.f_table_schema")))
                    {
                        schema = geometry_columns_row[featureClass._dataset.OgcDictionary("geometry_columns.f_table_schema")].ToString();
                    }
                    if (!String.IsNullOrEmpty(schema))
                    {
                        schema += ".";
                    }
                }
                catch { schema = ""; }
                featureClass._name       = schema + geometry_columns_row[featureClass._dataset.OgcDictionary("geometry_columns.f_table_name")].ToString();
                featureClass._shapefield = geometry_columns_row[featureClass._dataset.OgcDictionary("geometry_columns.f_geometry_column")].ToString();
                featureClass._idfield    = featureClass._dataset.OgcDictionary("gid");

                // Read Primary Key -> PostGIS id is not always "gid";
                string pKey = featureClass.GetPKey();
                if (!String.IsNullOrWhiteSpace(pKey) && !pKey.Equals(featureClass._idfield))
                {
                    featureClass._idfield = pKey;
                }

                featureClass._geometry_columns_type = geometry_columns_row[featureClass._dataset.OgcDictionary("geometry_columns.type")].ToString().ToUpper();
                switch (featureClass._geometry_columns_type)
                {
                case "MULTIPOLYGON":
                case "POLYGON":
                case "MULTIPOLYGONM":
                case "POLYGONM":
                    featureClass._geomType = geometryType.Polygon;
                    break;

                case "MULTILINESTRING":
                case "LINESTRING":
                case "MULTILINESTRINGM":
                case "LINESTRINGM":
                    featureClass._geomType = geometryType.Polyline;
                    break;

                case "POINT":
                case "POINTM":
                case "MULTIPOINT":
                case "MULTIPOINTM":
                    featureClass._geomType = geometryType.Point;
                    break;

                default:
                    featureClass._geomType = geometryType.Unknown;
                    break;
                }

                featureClass._hasZ = (int)geometry_columns_row[featureClass._dataset.OgcDictionary("geometry_columns.coord_dimension")] == 3;

                try
                {
                    int srid = int.Parse(geometry_columns_row[featureClass._dataset.OgcDictionary("geometry_columns.srid")].ToString());
                    if (srid > 0)
                    {
                        featureClass._sRef = gView.Framework.Geometry.SpatialReference.FromID("epsg:" + srid.ToString());
                    }
                    else
                    {
                        featureClass._sRef = await TrySelectSpatialReference(dataset, featureClass);
                    }
                }
                catch { }
                await featureClass.ReadSchema();
            }
            catch (Exception ex)
            {
                featureClass._lastException = ex;
                string msg = ex.Message;
            }

            return(featureClass);
        }
Ejemplo n.º 15
0
        public OgcSpatialFeatureCursor(OgcSpatialFeatureclass fc, IQueryFilter filter)
            : base((fc != null) ? fc.SpatialReference : null,
                   (filter != null) ? filter.FeatureSpatialReference : null)
        {
            if (fc == null || fc.Dataset == null)
            {
                return;
            }

            _idField = fc.IDFieldName;
            if (filter is ISpatialFilter)
            {
                _spatialfilter = (ISpatialFilter)filter;
            }

            try
            {
                if (fc.SpatialReference != null &&
                    filter is ISpatialFilter &&
                    ((ISpatialFilter)filter).FilterSpatialReference != null &&
                    !((ISpatialFilter)filter).FilterSpatialReference.Equals(fc.SpatialReference))
                {
                    filter = (ISpatialFilter)filter.Clone();

                    ((ISpatialFilter)filter).Geometry =
                        GeometricTransformer.Transform2D(((ISpatialFilter)filter).Geometry,
                                                         ((ISpatialFilter)filter).FilterSpatialReference,
                                                         fc.SpatialReference);
                    ((ISpatialFilter)filter).FilterSpatialReference = null;
                    if (((ISpatialFilter)filter).SpatialRelation == spatialRelation.SpatialRelationMapEnvelopeIntersects &&
                        ((ISpatialFilter)filter).Geometry != null)
                    {
                        ((ISpatialFilter)filter).Geometry = ((ISpatialFilter)filter).Geometry.Envelope;
                    }

                    _spatialfilter = (ISpatialFilter)filter;
                }
                DbCommand command = ((OgcSpatialDataset)fc.Dataset).SelectCommand(
                    fc, filter, out _shapeField);
                if (command == null)
                {
                    return;
                }

                _conn = ((OgcSpatialDataset)fc.Dataset).ProviderFactory.CreateConnection();
                _conn.ConnectionString = fc.Dataset.ConnectionString;

                command.Connection = _conn;

                if (_conn.State != ConnectionState.Closed)
                {
                    try
                    {
                        _conn.Close();
                    }
                    catch { }
                }
                _conn.Open();
                //command.Prepare();

                _reader = command.ExecuteReader();
            }
            catch (Exception ex)
            {
                if (_fc != null)
                {
                    _fc.LastException = ex;
                }

                if (_conn != null && _conn.State != ConnectionState.Closed)
                {
                    _conn.Close();
                    _conn = null;
                }
            }
        }
Ejemplo n.º 16
0
        async public static Task <ISpatialReference> TrySelectSpatialReference(OgcSpatialDataset dataset, OgcSpatialFeatureclass fc)
        {
            try
            {
                DbCommand sridCommand = dataset.SelectSpatialReferenceIds(fc);
                if (sridCommand != null)
                {
                    using (DbConnection connection = dataset.ProviderFactory.CreateConnection())
                    {
                        connection.ConnectionString = dataset.ConnectionString;
                        sridCommand.Connection      = connection;
                        await connection.OpenAsync();

                        using (DbDataReader reader = await sridCommand.ExecuteReaderAsync())
                        {
                            while (await reader.ReadAsync())
                            {
                                object sridObj = reader["srid"];
                                if (sridObj != null && sridObj != DBNull.Value)
                                {
                                    int srid = Convert.ToInt32(sridObj);
                                    if (srid > 0)
                                    {
                                        var sRef = gView.Framework.Geometry.SpatialReference.FromID("epsg:" + srid.ToString());
                                        if (sRef != null)
                                        {
                                            return(sRef);
                                        }
                                    }
                                }
                            }
                        }

                        connection.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
            return(null);
        }
Ejemplo n.º 17
0
        virtual public DbCommand SelectCommand(OgcSpatialFeatureclass fc, IQueryFilter filter, out string shapeFieldName, string functionName = "", string functionField = "", string functionAlias = "")
        {
            shapeFieldName = String.Empty;

            filter.fieldPostfix = filter.fieldPrefix = "\"";
            if (filter.SubFields == "*")
            {
                filter.SubFields = "";

                foreach (IField field in fc.Fields.ToEnumerable())
                {
                    filter.AddField(field.name);
                }
                filter.AddField(fc.IDFieldName);
                filter.AddField(fc.ShapeFieldName);
            }
            else
            {
                filter.AddField(fc.IDFieldName);
            }

            string where = "";
            if (filter is ISpatialFilter)
            {
                ISpatialFilter sFilter = filter as ISpatialFilter;
                if (sFilter.Geometry is IEnvelope)
                {
                    where = fc.ShapeFieldName + " && " + gView.Framework.OGC.OGC.Envelope2box2(sFilter.Geometry as IEnvelope, fc.SpatialReference);
                }
                else if (sFilter.Geometry != null)
                {
                    where = fc.ShapeFieldName + " && " + gView.Framework.OGC.OGC.Envelope2box2(sFilter.Geometry.Envelope, fc.SpatialReference);
                }
                filter.AddField(fc.ShapeFieldName);
            }

            if (!String.IsNullOrWhiteSpace(functionName) && !String.IsNullOrWhiteSpace(functionField))
            {
                filter.SubFields = "";
                filter.AddField(functionName + "(" + filter.fieldPrefix + functionField + filter.fieldPostfix + ")");
            }

            string filterWhereClause = (filter is IRowIDFilter) ? ((IRowIDFilter)filter).RowIDWhereClause : filter.WhereClause;

            //if (where != "" && filterWhereClause != "")
            //{
            //    where += " AND ";
            //}

            where = String.IsNullOrWhiteSpace(where) ?
                    filterWhereClause :
                    where + (String.IsNullOrWhiteSpace(filterWhereClause) ? "" : $" AND ({ filterWhereClause })");

            StringBuilder fieldNames = new StringBuilder();

            foreach (string fieldName in filter.SubFields.Split(' '))
            {
                if (fieldNames.Length > 0)
                {
                    fieldNames.Append(",");
                }

                if (fieldName == "\"" + fc.ShapeFieldName + "\"")
                {
                    fieldNames.Append("ST_AsBinary(\"" + fc.ShapeFieldName + "\") as temp_geometry");
                    shapeFieldName = "temp_geometry";
                }
                else
                {
                    fieldNames.Append(fieldName);
                }
            }

            string limit = String.Empty, orderBy = String.Empty;

            if (!String.IsNullOrWhiteSpace(filter.OrderBy))
            {
                orderBy = " order by " + filter.OrderBy;
            }

            if (filter.Limit > 0)
            {
                limit = " limit " + filter.Limit;
            }

            if (filter.BeginRecord > 1)  // Default in QueryFilter is one!!!
            {
                limit += " offset " + Math.Max(0, filter.BeginRecord - 1);
            }

            DbCommand command = ((OgcSpatialDataset)fc.Dataset).ProviderFactory.CreateCommand();

            command.CommandText = ("SELECT " + fieldNames + " FROM " + DbTableName(fc.Name) + ((where != "") ? " WHERE " + where : "") + orderBy + limit).Trim();

            return(command);
        }