Mercurial > postarc > postarc
comparison Postarc/Postarc/Feature/Feature.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 System.Text; | |
28 using ESRI.ArcGIS.Geodatabase; | |
29 using Npgsql; | |
30 using ESRI.ArcGIS.Geometry; | |
31 using System.Diagnostics; | |
32 | |
33 namespace Postarc.Feature | |
34 { | |
35 public class Feature : IFeature, IFeatureBuffer, IRow, IRowBuffer | |
36 { | |
37 protected FeatureClass featureClass; | |
38 protected IFields fields; | |
39 protected object[] row; | |
40 protected IWKTGeometry shape = new Point(); | |
41 | |
42 private int oidIdx = -1; | |
43 | |
44 /// <summary> | |
45 /// Constructs a Feature from an existing row in a database table. | |
46 /// </summary> | |
47 /// <param name="featureClass"></param> | |
48 /// <param name="reader"></param> | |
49 /// <param name="oidIdx"></param> | |
50 /// <param name="fields"></param> | |
51 public Feature(FeatureClass featureClass, IFields fields, int oidIdx, | |
52 NpgsqlDataReader reader) : this(featureClass, fields, oidIdx) | |
53 { | |
54 this.row = new object[fields.FieldCount]; | |
55 reader.GetValues(this.row); | |
56 LoadShapeFromRow(); | |
57 } | |
58 | |
59 protected Feature(FeatureClass featureClass, IFields fields, int oidIdx) | |
60 { | |
61 this.featureClass = featureClass; | |
62 this.fields = fields; | |
63 this.oidIdx = oidIdx; | |
64 } | |
65 | |
66 protected int ShapeFieldIndex | |
67 { | |
68 get | |
69 { | |
70 return this.fields.FindField(featureClass.ShapeFieldName); | |
71 } | |
72 } | |
73 | |
74 /// <summary> | |
75 /// Creates a new feature which is not yet written to a backend. | |
76 /// </summary> | |
77 /// <param name="featureClass"></param> | |
78 /// <param name="fields"></param> | |
79 /// <param name="oidIdx"></param> | |
80 /// <param name="row"></param> | |
81 public Feature(FeatureClass featureClass, IFields fields, int oidIdx, | |
82 object[] row) : this(featureClass, fields, oidIdx) | |
83 { | |
84 this.row = row; | |
85 LoadShapeFromRow(); | |
86 } | |
87 | |
88 protected void LoadShapeFromRow() | |
89 { | |
90 this.shape = new Point(); | |
91 int shpIdx = ShapeFieldIndex; | |
92 if (shpIdx >= 0 && row[ShapeFieldIndex] != null) | |
93 { | |
94 this.shape.SetWKT(row[ShapeFieldIndex] as string); | |
95 } | |
96 } | |
97 | |
98 public IObjectClass Class | |
99 { | |
100 get { throw new NotImplementedException(); } | |
101 } | |
102 | |
103 public void Delete() | |
104 { | |
105 // TODO: Not implemented | |
106 } | |
107 | |
108 public IEnvelope Extent | |
109 { | |
110 get | |
111 { | |
112 return this.Shape.Envelope; | |
113 } | |
114 } | |
115 | |
116 public esriFeatureType FeatureType | |
117 { | |
118 get | |
119 { | |
120 return this.featureClass.FeatureType; | |
121 } | |
122 } | |
123 | |
124 public IFields Fields | |
125 { | |
126 get | |
127 { | |
128 return this.fields; | |
129 } | |
130 } | |
131 | |
132 public bool HasOID | |
133 { | |
134 get | |
135 { | |
136 return oidIdx >= 0; | |
137 } | |
138 } | |
139 | |
140 public int OID | |
141 { | |
142 get | |
143 { | |
144 if (oidIdx >= 0) | |
145 { | |
146 int oid = Convert.ToInt32(this.row[oidIdx]); | |
147 return oid; | |
148 } | |
149 else | |
150 { | |
151 return -1; | |
152 } | |
153 } | |
154 } | |
155 | |
156 public IGeometry Shape | |
157 { | |
158 get | |
159 { | |
160 return this.shape as IGeometry; | |
161 } | |
162 set | |
163 { | |
164 this.shape = value as IWKTGeometry; | |
165 } | |
166 } | |
167 | |
168 public IGeometry ShapeCopy | |
169 { | |
170 get { throw new NotImplementedException(); } | |
171 } | |
172 | |
173 /// <summary> | |
174 /// Writes the row to the backend. From IRow interface. | |
175 /// </summary> | |
176 public void Store() | |
177 { | |
178 StringBuilder sql = new StringBuilder(); | |
179 NpgsqlConnection conn = featureClass.PostGISConnection.Open(); | |
180 NpgsqlCommand cmd; | |
181 | |
182 if (OID > 0) | |
183 { // Update existing row | |
184 sql.Append("UPDATE "); | |
185 sql.Append(featureClass.TableName); | |
186 sql.Append(" SET "); | |
187 for (int n = 0; n < this.row.Length; n++) | |
188 { | |
189 if (n != this.oidIdx) | |
190 { | |
191 sql.Append(Fields.get_Field(n).Name); | |
192 sql.Append(" = '"); | |
193 if (n == featureClass.ShapeFieldIndex) | |
194 sql.Append(shape.GetWKT()); | |
195 else | |
196 sql.Append(this.row[n]); | |
197 sql.Append("',"); | |
198 } | |
199 } | |
200 sql.Replace(',',' ', sql.Length - 1, 1); | |
201 sql.Append(" WHERE "); | |
202 sql.Append(FeatureCursor.POSTARC_OID_COLUMN); | |
203 sql.Append(" = "); | |
204 sql.Append(OID); | |
205 } | |
206 else | |
207 { // Create new row | |
208 sql.Append("INSERT INTO "); | |
209 sql.Append(featureClass.TableName); | |
210 sql.Append(" ("); | |
211 for (int n = 0; n < this.row.Length; n++) | |
212 { | |
213 if (n == this.oidIdx) | |
214 sql.Append(FeatureCursor.POSTARC_OID_COLUMN); | |
215 else | |
216 sql.Append(Fields.get_Field(n).Name); | |
217 | |
218 if (n != Fields.FieldCount - 1) | |
219 sql.Append(","); | |
220 } | |
221 sql.Append(" ) SELECT "); | |
222 for (int n = 0; n < this.row.Length; n++) | |
223 { | |
224 if (n == this.oidIdx) | |
225 { | |
226 sql.Append("Max("); | |
227 sql.Append(FeatureCursor.POSTARC_OID_COLUMN); | |
228 sql.Append(") + 1"); | |
229 } | |
230 else | |
231 { | |
232 sql.Append("'"); | |
233 if (n == featureClass.ShapeFieldIndex) | |
234 { | |
235 sql.Append(shape.GetWKT()); | |
236 } | |
237 else | |
238 { | |
239 sql.Append(this.row[n]); | |
240 } | |
241 sql.Append("'"); | |
242 } | |
243 if (n != Fields.FieldCount - 1) | |
244 sql.Append(","); | |
245 } | |
246 sql.Append(" FROM "); | |
247 sql.Append(featureClass.TableName); | |
248 } | |
249 | |
250 cmd = new NpgsqlCommand(sql.ToString(), conn); | |
251 int updatedRows = cmd.ExecuteNonQuery(); | |
252 conn.Close(); | |
253 Debug.WriteLine("Feature::Store: " + updatedRows + " rows affected"); | |
254 } | |
255 | |
256 public ITable Table | |
257 { | |
258 get | |
259 { | |
260 return this.featureClass; | |
261 } | |
262 } | |
263 | |
264 public object get_Value(int index) | |
265 { | |
266 Debug.WriteLine("Feature::get_Value(" + index + "): " | |
267 + (row[index] != null ? row[index].GetType() + " " + row[index].ToString() : "")); | |
268 | |
269 if (index == featureClass.ShapeFieldIndex) | |
270 { | |
271 return this.Shape; | |
272 } | |
273 else | |
274 { | |
275 return this.row[index]; | |
276 } | |
277 } | |
278 | |
279 public void set_Value(int index, object value) | |
280 { | |
281 Debug.WriteLine("Feature::set_Value(" + index + ", " + value + ")"); | |
282 this.row[index] = value; | |
283 } | |
284 } | |
285 } |