Mercurial > postarc > postarc
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1aca3d413885 |
---|---|
1 /* | |
2 * Postarc | |
3 * | |
4 * Author: | |
5 * Christian Lins <christian.lins@intevation.de> | |
6 * | |
7 * Copyright: | |
8 * Copyright (C) 2012 Intevation GmbH <http://www.intevation.de/> | |
9 * | |
10 * This program is free software: you can redistribute it and/or modify | |
11 * it under the terms of the GNU Lesser General Public License as published by | |
12 * the Free Software Foundation, either version 3 of the License, or | |
13 * (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU Lesser General Public License | |
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 */ | |
23 | |
24 using System; | |
25 using System.Collections.Generic; | |
26 using System.Linq; | |
27 using ESRI.ArcGIS.Geodatabase; | |
28 using System.Text; | |
29 using Npgsql; | |
30 using System.Diagnostics; | |
31 | |
32 namespace Postarc.Feature | |
33 { | |
34 static class StringExtension | |
35 { | |
36 public static string Replace(this string str, string oldValue, string newValue, StringComparison comparison) | |
37 { | |
38 StringBuilder sb = new StringBuilder(); | |
39 | |
40 int previousIndex = 0; | |
41 int index = str.IndexOf(oldValue, comparison); | |
42 while (index != -1) | |
43 { | |
44 sb.Append(str.Substring(previousIndex, index - previousIndex)); | |
45 sb.Append(newValue); | |
46 index += oldValue.Length; | |
47 | |
48 previousIndex = index; | |
49 index = str.IndexOf(oldValue, index, comparison); | |
50 } | |
51 sb.Append(str.Substring(previousIndex)); | |
52 | |
53 return sb.ToString(); | |
54 } | |
55 } | |
56 | |
57 class FeatureCursor : IFeatureCursor, ICursor | |
58 { | |
59 /// <summary> | |
60 /// Currently Postarc requires an Postarc_ID column in the PostGIS table | |
61 /// which is mapped to ArcGIS' OID. | |
62 /// </summary> | |
63 public const string POSTARC_OID_COLUMN = "oid"; | |
64 public const string POSTARC_SHAPE_COLUMN = "shape"; | |
65 public const string ARCGIS_OID_COLUMN = "oid"; | |
66 | |
67 protected PostGISConnection connection; | |
68 protected NpgsqlConnection npgsqlConnection; | |
69 protected FeatureClass featureClass; | |
70 protected IFields fields; | |
71 protected NpgsqlDataReader reader; | |
72 | |
73 private int oidIdx = -1; | |
74 | |
75 public FeatureCursor(FeatureClass featClass, PostGISConnection conn, IQueryFilter filter) | |
76 { | |
77 this.featureClass = featClass; | |
78 this.connection = conn; | |
79 | |
80 // If a QueryFilter is given, we must adapt our SQL query | |
81 string subFields = AllFields(); | |
82 string whereClause = ""; | |
83 if (filter != null) | |
84 { | |
85 /*if (!filter.SubFields.Equals("*")) | |
86 { | |
87 subFields = filter.SubFields; | |
88 if (!subFields.Contains(POSTARC_OID_COLUMN) && !subFields.Equals("*")) | |
89 { | |
90 subFields = subFields + "," + POSTARC_OID_COLUMN; | |
91 } | |
92 }*/ | |
93 | |
94 whereClause = filter.WhereClause; | |
95 if(whereClause != null && !whereClause.Equals("")) | |
96 { | |
97 whereClause = " WHERE " + whereClause; | |
98 } | |
99 } | |
100 | |
101 subFields = subFields.Replace( | |
102 POSTARC_SHAPE_COLUMN, | |
103 "ST_AsText(" + POSTARC_SHAPE_COLUMN + ") AS " + POSTARC_SHAPE_COLUMN); | |
104 | |
105 this.npgsqlConnection = conn.Open(); | |
106 NpgsqlCommand cmd = new NpgsqlCommand( | |
107 "SELECT " + subFields + " FROM " + featClass.TableName + whereClause + " ORDER BY " + POSTARC_OID_COLUMN, | |
108 npgsqlConnection); | |
109 this.reader = cmd.ExecuteReader(); | |
110 | |
111 // Build the fields | |
112 IFieldsEdit fieldsEdit = new FieldsClass(); | |
113 for (int n = 0; n < reader.FieldCount; n++) | |
114 { | |
115 string fieldName = reader.GetName(n); | |
116 if (fieldName.Equals(POSTARC_OID_COLUMN, StringComparison.OrdinalIgnoreCase)) | |
117 { | |
118 fieldName = ARCGIS_OID_COLUMN; | |
119 oidIdx = n; | |
120 } | |
121 int fidx = featClass.FindField(fieldName); | |
122 IField field = featClass.Fields.get_Field(fidx); | |
123 fieldsEdit.AddField(field); | |
124 } | |
125 this.fields = fieldsEdit as IFields; | |
126 } | |
127 | |
128 protected string AllFields() | |
129 { | |
130 StringBuilder buf = new StringBuilder(); | |
131 | |
132 for (int n = 0; n < featureClass.Fields.FieldCount; n++) | |
133 { | |
134 buf.Append(featureClass.Fields.get_Field(n).Name); | |
135 if (n < featureClass.Fields.FieldCount - 1) | |
136 { | |
137 buf.Append(","); | |
138 } | |
139 } | |
140 | |
141 return buf.ToString(); | |
142 } | |
143 | |
144 public void DeleteFeature() | |
145 { | |
146 // Not implemented | |
147 } | |
148 | |
149 public IFields Fields | |
150 { | |
151 get | |
152 { | |
153 return this.fields; | |
154 } | |
155 } | |
156 | |
157 public int FindField(string name) | |
158 { | |
159 return this.Fields.FindField(name); | |
160 } | |
161 | |
162 public void Flush() | |
163 { | |
164 // TODO: Not implemented | |
165 } | |
166 | |
167 public object InsertFeature(IFeatureBuffer buffer) | |
168 { | |
169 Debug.WriteLine("FeatureCursor::InsertFeature(): FIXME"); | |
170 return null; // TODO: Not implemented | |
171 } | |
172 | |
173 public IFeature NextFeature() | |
174 { | |
175 if (this.reader.Read()) | |
176 { | |
177 Feature feature = new Feature(featureClass, Fields, oidIdx, this.reader); | |
178 return feature; | |
179 } | |
180 this.reader.Close(); // We've reached the last feature | |
181 return null; | |
182 } | |
183 | |
184 public void UpdateFeature(IFeature Object) | |
185 { | |
186 // TODO: Not implemented | |
187 Debug.WriteLine("FeatureCursor::UpdateFeature(): FIXME"); | |
188 } | |
189 | |
190 public void DeleteRow() | |
191 { | |
192 // TODO: Not implemented | |
193 Debug.WriteLine("FeatureCursor::DeleteRow(): FIXME"); | |
194 } | |
195 | |
196 public object InsertRow(IRowBuffer buffer) | |
197 { | |
198 // TODO: Not implemented | |
199 Debug.WriteLine("FeatureCursor::InsertRow(): FIXME"); | |
200 return null; | |
201 } | |
202 | |
203 public IRow NextRow() | |
204 { | |
205 return NextFeature(); | |
206 } | |
207 | |
208 public void UpdateRow(IRow Row) | |
209 { | |
210 // TODO: Not implemented | |
211 Debug.WriteLine("FeatureCursor::UpdateRow(): FIXME"); | |
212 } | |
213 } | |
214 } |