view Postarc/Postarc/Feature/FeatureCursor.cs @ 0:1aca3d413885 tip

Initial import of Postarc
author Christian Lins <christian.lins@intevation.de>
date Fri, 05 Oct 2012 23:55:06 +0200
parents
children
line wrap: on
line source
/*
 * Postarc
 *
 * Author:
 * Christian Lins <christian.lins@intevation.de>
 *
 * Copyright:
 * Copyright (C) 2012 Intevation GmbH <http://www.intevation.de/>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using ESRI.ArcGIS.Geodatabase;
using System.Text;
using Npgsql;
using System.Diagnostics;

namespace Postarc.Feature
{
    static class StringExtension
    {
        public static string Replace(this string str, string oldValue, string newValue, StringComparison comparison)
        {
            StringBuilder sb = new StringBuilder();

            int previousIndex = 0;
            int index = str.IndexOf(oldValue, comparison);
            while (index != -1)
            {
                sb.Append(str.Substring(previousIndex, index - previousIndex));
                sb.Append(newValue);
                index += oldValue.Length;

                previousIndex = index;
                index = str.IndexOf(oldValue, index, comparison);
            }
            sb.Append(str.Substring(previousIndex));

            return sb.ToString();
        }
    }

    class FeatureCursor : IFeatureCursor, ICursor
    {
        /// <summary>
        /// Currently Postarc requires an Postarc_ID column in the PostGIS table
        /// which is mapped to ArcGIS' OID.
        /// </summary>
        public const string POSTARC_OID_COLUMN = "oid";
        public const string POSTARC_SHAPE_COLUMN = "shape";
        public const string ARCGIS_OID_COLUMN = "oid";

        protected PostGISConnection connection;
        protected NpgsqlConnection npgsqlConnection;
        protected FeatureClass featureClass;
        protected IFields fields;
        protected NpgsqlDataReader reader;

        private int oidIdx = -1;

        public FeatureCursor(FeatureClass featClass, PostGISConnection conn, IQueryFilter filter)
        {
            this.featureClass = featClass;
            this.connection = conn;

            // If a QueryFilter is given, we must adapt our SQL query
            string subFields = AllFields();
            string whereClause = "";
            if (filter != null)
            {
                /*if (!filter.SubFields.Equals("*"))
                {
                    subFields = filter.SubFields;
                    if (!subFields.Contains(POSTARC_OID_COLUMN) && !subFields.Equals("*"))
                    {
                        subFields = subFields + "," + POSTARC_OID_COLUMN;
                    }
                }*/

                whereClause = filter.WhereClause;
                if(whereClause != null && !whereClause.Equals(""))
                {
                    whereClause = " WHERE " + whereClause;
                }
            }

            subFields = subFields.Replace(
                    POSTARC_SHAPE_COLUMN,
                    "ST_AsText(" + POSTARC_SHAPE_COLUMN + ") AS " + POSTARC_SHAPE_COLUMN);

            this.npgsqlConnection = conn.Open();
            NpgsqlCommand cmd = new NpgsqlCommand(
                "SELECT " + subFields + " FROM " + featClass.TableName + whereClause + " ORDER BY " + POSTARC_OID_COLUMN, 
                npgsqlConnection);
            this.reader = cmd.ExecuteReader();
            
            // Build the fields
            IFieldsEdit fieldsEdit = new FieldsClass();
            for (int n = 0; n < reader.FieldCount; n++)
            {
                string fieldName = reader.GetName(n);
                if (fieldName.Equals(POSTARC_OID_COLUMN, StringComparison.OrdinalIgnoreCase))
                {
                    fieldName = ARCGIS_OID_COLUMN;
                    oidIdx = n;
                }
                int fidx = featClass.FindField(fieldName);
                IField field = featClass.Fields.get_Field(fidx);
                fieldsEdit.AddField(field);
            }
            this.fields = fieldsEdit as IFields;
        }

        protected string AllFields()
        {
            StringBuilder buf = new StringBuilder();

            for (int n = 0; n < featureClass.Fields.FieldCount; n++)
            {
                buf.Append(featureClass.Fields.get_Field(n).Name);
                if (n < featureClass.Fields.FieldCount - 1)
                {
                    buf.Append(",");
                }
            }

            return buf.ToString();
        }

        public void DeleteFeature()
        {
            // Not implemented
        }

        public IFields Fields
        {
            get
            {
                return this.fields;
            }
        }

        public int FindField(string name)
        {
            return this.Fields.FindField(name);
        }

        public void Flush()
        {
            // TODO: Not implemented
        }

        public object InsertFeature(IFeatureBuffer buffer)
        {
            Debug.WriteLine("FeatureCursor::InsertFeature(): FIXME");
            return null; // TODO: Not implemented
        }

        public IFeature NextFeature()
        {
            if (this.reader.Read())
            {
                Feature feature = new Feature(featureClass, Fields, oidIdx, this.reader);
                return feature;
            }
            this.reader.Close(); // We've reached the last feature
            return null;
        }

        public void UpdateFeature(IFeature Object)
        {
            // TODO: Not implemented
            Debug.WriteLine("FeatureCursor::UpdateFeature(): FIXME");
        }

        public void DeleteRow()
        {
            // TODO: Not implemented
            Debug.WriteLine("FeatureCursor::DeleteRow(): FIXME");
        }

        public object InsertRow(IRowBuffer buffer)
        {
            // TODO: Not implemented
            Debug.WriteLine("FeatureCursor::InsertRow(): FIXME");
            return null;
        }

        public IRow NextRow()
        {
            return NextFeature();
        }

        public void UpdateRow(IRow Row)
        {
            // TODO: Not implemented
            Debug.WriteLine("FeatureCursor::UpdateRow(): FIXME");
        }
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)