svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.shp / src / main / java / org / gvsig / fmap / dal / store / shp / SHPStoreProvider.java @ 46875
History | View | Annotate | Download (23.2 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 41610 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 40435 | jjdelcerro | *
|
11 | 41610 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 40435 | jjdelcerro | *
|
16 | 41610 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 40435 | jjdelcerro | *
|
20 | 41610 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | package org.gvsig.fmap.dal.store.shp; |
||
24 | |||
25 | import java.io.File; |
||
26 | import java.io.IOException; |
||
27 | 43978 | omartinez | import java.util.ArrayList; |
28 | 40435 | jjdelcerro | import java.util.Iterator; |
29 | 43978 | omartinez | import java.util.List; |
30 | 40435 | jjdelcerro | |
31 | import org.apache.commons.io.FileUtils; |
||
32 | 41860 | jjdelcerro | import org.apache.commons.io.FilenameUtils; |
33 | 40435 | jjdelcerro | import org.cresques.cts.IProjection; |
34 | import org.slf4j.Logger; |
||
35 | import org.slf4j.LoggerFactory; |
||
36 | |||
37 | import org.gvsig.fmap.dal.DataStore; |
||
38 | import org.gvsig.fmap.dal.DataTypes; |
||
39 | import org.gvsig.fmap.dal.FileHelper; |
||
40 | import org.gvsig.fmap.dal.exception.CloseException; |
||
41 | import org.gvsig.fmap.dal.exception.DataException; |
||
42 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
43 | import org.gvsig.fmap.dal.exception.ReadException; |
||
44 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
45 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
46 | import org.gvsig.fmap.dal.feature.Feature; |
||
47 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
48 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
49 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
50 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
51 | import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
||
52 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
53 | import org.gvsig.fmap.dal.resource.ResourceAction; |
||
54 | import org.gvsig.fmap.dal.resource.exception.ResourceException; |
||
55 | import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException; |
||
56 | import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException; |
||
57 | import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException; |
||
58 | import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException; |
||
59 | import org.gvsig.fmap.dal.resource.file.FileResource; |
||
60 | import org.gvsig.fmap.dal.resource.spi.MultiResource; |
||
61 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
62 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
63 | import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters; |
||
64 | import org.gvsig.fmap.dal.store.dbf.DBFStoreProvider; |
||
65 | 41556 | jjdelcerro | import org.gvsig.fmap.dal.store.shp.utils.ISHPFile; |
66 | 42567 | fdiaz | import org.gvsig.fmap.dal.store.shp.utils.SHP; |
67 | 41556 | jjdelcerro | import org.gvsig.fmap.dal.store.shp.utils.SHPFile2; |
68 | 40435 | jjdelcerro | import org.gvsig.fmap.geom.Geometry; |
69 | import org.gvsig.fmap.geom.GeometryLocator; |
||
70 | import org.gvsig.fmap.geom.GeometryManager; |
||
71 | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
||
72 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
73 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
74 | import org.gvsig.tools.dispose.DisposableIterator; |
||
75 | import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
||
76 | import org.gvsig.tools.exception.BaseException; |
||
77 | |||
78 | 42572 | fdiaz | /**
|
79 | *
|
||
80 | */
|
||
81 | 40435 | jjdelcerro | public class SHPStoreProvider extends DBFStoreProvider { |
82 | |||
83 | 44669 | jjdelcerro | private static final GeometryManager GEOM_MANAGER = GeometryLocator.getGeometryManager(); |
84 | private static final Logger LOGGER = LoggerFactory.getLogger(SHPStoreProvider.class); |
||
85 | |||
86 | public static String NAME = DataStore.SHAPE_PROVIDER_NAME; |
||
87 | 41610 | jjdelcerro | public static String DESCRIPTION = "Shape file"; |
88 | private ISHPFile shpFile;
|
||
89 | 40435 | jjdelcerro | |
90 | 41610 | jjdelcerro | private MultiResource resource;
|
91 | 40435 | jjdelcerro | |
92 | 44669 | jjdelcerro | public static final String GEOMETRY_ATTIBUTE_NAME = "GEOMETRY"; |
93 | 40435 | jjdelcerro | |
94 | 41610 | jjdelcerro | public static final String METADATA_DEFINITION_NAME = NAME; |
95 | 40435 | jjdelcerro | |
96 | 41610 | jjdelcerro | private SHPFeatureWriter writer = null; |
97 | 40435 | jjdelcerro | |
98 | 42517 | dmartinezizquierdo | private boolean loTengoEnUso; |
99 | |||
100 | 42574 | fdiaz | /**
|
101 | * @param params
|
||
102 | * @param storeServices
|
||
103 | * @throws InitializeException
|
||
104 | */
|
||
105 | public SHPStoreProvider(SHPStoreParameters params, DataStoreProviderServices storeServices)
|
||
106 | throws InitializeException {
|
||
107 | super(params, storeServices, FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME));
|
||
108 | 41610 | jjdelcerro | } |
109 | 40435 | jjdelcerro | |
110 | 42574 | fdiaz | protected void init(DBFStoreParameters params, DataStoreProviderServices storeServices) throws InitializeException { |
111 | 40435 | jjdelcerro | |
112 | 41610 | jjdelcerro | this.shpFile = new SHPFile2((SHPStoreParameters) params); |
113 | super.init(params, storeServices);
|
||
114 | this.shpFile.setUseNullGeometry(this.getShpParameters().getUseNullGeometry()); |
||
115 | } |
||
116 | 40435 | jjdelcerro | |
117 | 43246 | jjdelcerro | @Override
|
118 | public SHPStoreParameters getParameters() {
|
||
119 | return (SHPStoreParameters) super.getParameters(); |
||
120 | } |
||
121 | |||
122 | 44022 | jjdelcerro | public IProjection getProjection() {
|
123 | return this.getShpParameters().getCRS(); |
||
124 | } |
||
125 | |||
126 | 43246 | jjdelcerro | @Override
|
127 | 41610 | jjdelcerro | public Object getDynValue(String name) throws DynFieldNotFoundException { |
128 | if (DataStore.METADATA_CRS.equalsIgnoreCase(name)) {
|
||
129 | 40435 | jjdelcerro | |
130 | 41610 | jjdelcerro | return this.getShpParameters().getCRS(); |
131 | 40435 | jjdelcerro | |
132 | 41610 | jjdelcerro | } else if (DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name)) { |
133 | try {
|
||
134 | return this.shpFile.getFullExtent(); |
||
135 | } catch (ReadException e) {
|
||
136 | return null; |
||
137 | } |
||
138 | } |
||
139 | return super.getDynValue(name); |
||
140 | } |
||
141 | 40435 | jjdelcerro | |
142 | 46875 | fdiaz | @Override
|
143 | public boolean hasDynValue(String name) throws DynFieldNotFoundException { |
||
144 | if (DataStore.METADATA_CRS.equalsIgnoreCase(name)) {
|
||
145 | |||
146 | return this.getShpParameters().getCRS() != null; |
||
147 | |||
148 | } else if (DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name)) { |
||
149 | try {
|
||
150 | return this.shpFile.getFullExtent() != null; |
||
151 | } catch (ReadException e) {
|
||
152 | return false; |
||
153 | } |
||
154 | } |
||
155 | return super.hasDynValue(name); |
||
156 | } |
||
157 | |||
158 | 42574 | fdiaz | protected void initResource(DBFStoreParameters params, DataStoreProviderServices storeServices) |
159 | throws InitializeException {
|
||
160 | 41610 | jjdelcerro | |
161 | SHPStoreParameters shpParams = (SHPStoreParameters) params; |
||
162 | 42574 | fdiaz | resource = (MultiResource) createResource(MultiResource.TYPE_NAME, new Object[] { shpParams.getSHPFileName() }); |
163 | 41610 | jjdelcerro | |
164 | 42574 | fdiaz | resource.addResource(FileResource.NAME, new Object[] { shpParams.getSHPFileName() }, true); |
165 | resource.addResource(FileResource.NAME, new Object[] { shpParams.getSHXFileName() }, true); |
||
166 | resource.addResource(FileResource.NAME, new Object[] { shpParams.getDBFFileName() }, true); |
||
167 | 41610 | jjdelcerro | |
168 | 42517 | dmartinezizquierdo | resource.frozen(); |
169 | resource.addMultiResourceConsumer(this);
|
||
170 | 42814 | fdiaz | super.initResource(resource, storeServices);
|
171 | 41610 | jjdelcerro | } |
172 | |||
173 | ; |
||
174 | |||
175 | 42574 | fdiaz | public ResourceProvider getResource() {
|
176 | 41610 | jjdelcerro | return resource;
|
177 | } |
||
178 | 40435 | jjdelcerro | |
179 | 41610 | jjdelcerro | /**
|
180 | *
|
||
181 | * @throws ResourceNotifyChangesException
|
||
182 | */
|
||
183 | 42574 | fdiaz | protected void resourcesNotifyChanges() throws ResourceNotifyChangesException { |
184 | 41610 | jjdelcerro | getResource().notifyChanges(); |
185 | // TODO .prj
|
||
186 | 40435 | jjdelcerro | |
187 | 41610 | jjdelcerro | } |
188 | 40435 | jjdelcerro | |
189 | 41610 | jjdelcerro | /**
|
190 | * @throws ResourceNotifyCloseException
|
||
191 | *
|
||
192 | */
|
||
193 | protected void resourcesNotifyClose() throws ResourceNotifyCloseException { |
||
194 | getResource().notifyClose(); |
||
195 | // TODO .prj
|
||
196 | 40435 | jjdelcerro | |
197 | 41610 | jjdelcerro | } |
198 | 40435 | jjdelcerro | |
199 | 41610 | jjdelcerro | @Override
|
200 | protected void doDispose() throws BaseException { |
||
201 | super.doDispose();
|
||
202 | getResource().removeConsumer(this);
|
||
203 | this.writer = null; |
||
204 | this.shpFile = null; |
||
205 | } |
||
206 | 40435 | jjdelcerro | |
207 | 41610 | jjdelcerro | protected void disposeResource() { |
208 | getResource().removeConsumer(this);
|
||
209 | } |
||
210 | 40435 | jjdelcerro | |
211 | 41610 | jjdelcerro | /**
|
212 | * @throws ResourceNotifyOpenException
|
||
213 | *
|
||
214 | */
|
||
215 | protected void resourcesOpen() throws ResourceNotifyOpenException { |
||
216 | getResource().notifyOpen(); |
||
217 | } |
||
218 | 40435 | jjdelcerro | |
219 | 42574 | fdiaz | protected static EditableFeatureAttributeDescriptor addGeometryColumn(EditableFeatureType fType) { |
220 | 40435 | jjdelcerro | |
221 | 41610 | jjdelcerro | EditableFeatureAttributeDescriptor attrTmp = null;
|
222 | EditableFeatureAttributeDescriptor attr = null;
|
||
223 | 42574 | fdiaz | Iterator<?> iter = fType.iterator();
|
224 | 41610 | jjdelcerro | while (iter.hasNext()) {
|
225 | attrTmp = (EditableFeatureAttributeDescriptor) iter.next(); |
||
226 | if (attrTmp.getType() == DataTypes.GEOMETRY) {
|
||
227 | if (attr != null) { |
||
228 | // Two geom fields not allowed
|
||
229 | fType.remove(attrTmp.getName()); |
||
230 | } else {
|
||
231 | attr = attrTmp; |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | 40435 | jjdelcerro | |
236 | 41610 | jjdelcerro | if (attr == null) { |
237 | String geofield = createGeometryFieldName(fType);
|
||
238 | attr = fType.add(geofield, DataTypes.GEOMETRY); |
||
239 | attr.setDefaultValue(null);
|
||
240 | } |
||
241 | 40435 | jjdelcerro | |
242 | 41610 | jjdelcerro | attr.setObjectClass(Geometry.class); |
243 | 43412 | jjdelcerro | attr.setAllowNull(true);
|
244 | 44669 | jjdelcerro | // fType.setDefaultGeometryAttributeName(attr.getName());
|
245 | 41610 | jjdelcerro | return attr;
|
246 | 40435 | jjdelcerro | |
247 | 41610 | jjdelcerro | } |
248 | 40435 | jjdelcerro | |
249 | 41610 | jjdelcerro | private static String createGeometryFieldName(FeatureType ft) { |
250 | 40435 | jjdelcerro | |
251 | 41610 | jjdelcerro | if (ft.getAttributeDescriptor(GEOMETRY_ATTIBUTE_NAME) == null) { |
252 | return GEOMETRY_ATTIBUTE_NAME;
|
||
253 | } |
||
254 | 41228 | jldominguez | |
255 | 41610 | jjdelcerro | int i = 0; |
256 | String candidate = GEOMETRY_ATTIBUTE_NAME + i;
|
||
257 | while (ft.getAttributeDescriptor(candidate) != null) { |
||
258 | i++; |
||
259 | candidate = GEOMETRY_ATTIBUTE_NAME + i; |
||
260 | } |
||
261 | 41228 | jldominguez | return candidate;
|
262 | } |
||
263 | |||
264 | 44669 | jjdelcerro | protected EditableFeatureType getTheFeatureType() throws InitializeException{ |
265 | try {
|
||
266 | this.open();
|
||
267 | } catch (DataException e) {
|
||
268 | throw new InitializeException(this.getProviderName(), e); |
||
269 | 41610 | jjdelcerro | } |
270 | final EditableFeatureType fType = super.getTheFeatureType(); |
||
271 | try {
|
||
272 | getResource().execute(new ResourceAction() {
|
||
273 | 42574 | fdiaz | |
274 | 41610 | jjdelcerro | public Object run() throws Exception { |
275 | 42574 | fdiaz | EditableFeatureAttributeDescriptor attr = addGeometryColumn(fType); |
276 | 40435 | jjdelcerro | |
277 | 44669 | jjdelcerro | attr.setGeometryType(GEOM_MANAGER.getGeometryType( |
278 | 43412 | jjdelcerro | shpFile.getGeometryType(), |
279 | shpFile.getGeometrySubType() |
||
280 | )); |
||
281 | 42574 | fdiaz | |
282 | 41610 | jjdelcerro | IProjection srs = getShpParameters().getCRS(); |
283 | attr.setSRS(srs); |
||
284 | 40435 | jjdelcerro | |
285 | 41610 | jjdelcerro | return null; |
286 | } |
||
287 | }); |
||
288 | return fType;
|
||
289 | } catch (ResourceExecuteException e) {
|
||
290 | throw new InitializeException(e); |
||
291 | } |
||
292 | } |
||
293 | 40435 | jjdelcerro | |
294 | 41610 | jjdelcerro | protected SHPStoreParameters getShpParameters() {
|
295 | return (SHPStoreParameters) getParameters();
|
||
296 | } |
||
297 | 40435 | jjdelcerro | |
298 | 41610 | jjdelcerro | public String getProviderName() { |
299 | 42814 | fdiaz | return NAME;
|
300 | 41610 | jjdelcerro | } |
301 | 40435 | jjdelcerro | |
302 | 41610 | jjdelcerro | public boolean allowWrite() { |
303 | 42814 | fdiaz | return this.shpFile.isEditable() && |
304 | super.allowWrite() &&
|
||
305 | !this.getShpParameters().getLoadCorruptGeometriesAsNull() &&
|
||
306 | !this.getShpParameters().getAllowInconsistenciesInGeometryType() &&
|
||
307 | !this.getShpParameters().getFixLinearRings();
|
||
308 | 41610 | jjdelcerro | } |
309 | 40435 | jjdelcerro | |
310 | 41610 | jjdelcerro | /**
|
311 | *
|
||
312 | * @param index
|
||
313 | * @param featureType
|
||
314 | * @return
|
||
315 | * @throws ReadException
|
||
316 | */
|
||
317 | 42574 | fdiaz | protected FeatureProvider getFeatureProviderByIndex(long index, FeatureType featureType) throws DataException { |
318 | this.open();
|
||
319 | 41610 | jjdelcerro | try {
|
320 | 40435 | jjdelcerro | |
321 | 42574 | fdiaz | FeatureProvider featureProvider = super.getFeatureProviderByIndex(index, featureType);
|
322 | 41610 | jjdelcerro | featureProvider.setDefaultEnvelope(this.shpFile.getBoundingBox(index));
|
323 | return featureProvider;
|
||
324 | } catch (DataException e) {
|
||
325 | throw e;
|
||
326 | } catch (CreateEnvelopeException e) {
|
||
327 | throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(e); |
||
328 | } catch (CreateGeometryException e) {
|
||
329 | throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(e); |
||
330 | 40435 | jjdelcerro | } |
331 | |||
332 | 41610 | jjdelcerro | } |
333 | 40435 | jjdelcerro | |
334 | 42574 | fdiaz | protected void initFeatureProviderByIndex(FeatureProvider featureProvider, long index, FeatureType featureType) |
335 | throws DataException {
|
||
336 | 41610 | jjdelcerro | try {
|
337 | super.initFeatureProviderByIndex(featureProvider, index, featureType);
|
||
338 | featureProvider.setDefaultEnvelope(this.shpFile.getBoundingBox(index));
|
||
339 | } catch (CreateEnvelopeException e) {
|
||
340 | throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(e); |
||
341 | } catch (CreateGeometryException e) {
|
||
342 | throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(e); |
||
343 | 40435 | jjdelcerro | } |
344 | 41610 | jjdelcerro | } |
345 | 40435 | jjdelcerro | |
346 | 41610 | jjdelcerro | /**
|
347 | *
|
||
348 | * @param featureProvider
|
||
349 | * @throws DataException
|
||
350 | */
|
||
351 | 42787 | jjdelcerro | @Override
|
352 | 42574 | fdiaz | protected void loadFeatureProviderByIndex(FeatureProvider featureProvider) throws DataException { |
353 | 40435 | jjdelcerro | |
354 | 41610 | jjdelcerro | FeatureType featureType = featureProvider.getType(); |
355 | 42787 | jjdelcerro | long index = ((Long) featureProvider.getOID()); |
356 | 41610 | jjdelcerro | boolean hasGeometry = false; |
357 | int i = featureType.getDefaultGeometryAttributeIndex();
|
||
358 | if (i >= 0) { |
||
359 | if (!featureProvider.isReadOnly(i)) {
|
||
360 | 42787 | jjdelcerro | Geometry geom; |
361 | 41610 | jjdelcerro | try {
|
362 | 42787 | jjdelcerro | geom = this.shpFile.getGeometry(index);
|
363 | } catch (Exception e) { |
||
364 | if( this.getShpParameters().getLoadCorruptGeometriesAsNull() ) { |
||
365 | geom = null;
|
||
366 | } else {
|
||
367 | 42811 | jjdelcerro | throw new ReadGeometryException(getName(), featureProvider.getOID(), e); |
368 | 42787 | jjdelcerro | } |
369 | 41215 | jjdelcerro | } |
370 | 42787 | jjdelcerro | featureProvider.set(i, geom); |
371 | 41215 | jjdelcerro | } |
372 | 41610 | jjdelcerro | hasGeometry = true;
|
373 | 41215 | jjdelcerro | } |
374 | 41610 | jjdelcerro | if (hasDBFAttributes(featureType, hasGeometry)) {
|
375 | super.loadFeatureProviderByIndex(featureProvider);
|
||
376 | } |
||
377 | 40435 | jjdelcerro | |
378 | 41610 | jjdelcerro | } |
379 | 42814 | fdiaz | |
380 | 42811 | jjdelcerro | public class ReadGeometryException extends ReadException { |
381 | 40435 | jjdelcerro | |
382 | 42814 | fdiaz | private final static String MESSAGE_FORMAT = "There was errors loading a geometry from '%(store)'.\nCheck 'Load corrupt geometries as null' in the shape's properties of the add layer dialog to skip corrupt geometries. The layer will become read only."; |
383 | 42811 | jjdelcerro | private final static String MESSAGE_KEY = "_ReadGeometryException"; |
384 | private static final long serialVersionUID = 2626155328734197112L; |
||
385 | |||
386 | public ReadGeometryException(String store, Object oid, Throwable cause) { |
||
387 | super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
||
388 | setValue("store", store);
|
||
389 | setValue("storeParameters",getParameters());
|
||
390 | } |
||
391 | } |
||
392 | |||
393 | 42574 | fdiaz | private boolean hasDBFAttributes(FeatureType featureType, boolean hasGeometry) { |
394 | FeatureAttributeDescriptor[] attributes = featureType.getAttributeDescriptors();
|
||
395 | 41610 | jjdelcerro | // If there aren't any attributes, nor has any DBF attributes
|
396 | if (attributes == null || attributes.length == 0) { |
||
397 | return false; |
||
398 | } |
||
399 | // If there is only one attribute and it is the geometry one
|
||
400 | if (attributes.length == 1 && hasGeometry) { |
||
401 | return false; |
||
402 | } |
||
403 | // In any other case
|
||
404 | return true; |
||
405 | } |
||
406 | 40435 | jjdelcerro | |
407 | 44001 | omartinez | protected void loadValue(FeatureProvider featureProvider, long rowIndex, FeatureAttributeDescriptor descriptor) |
408 | 42574 | fdiaz | throws ReadException {
|
409 | 41610 | jjdelcerro | if (descriptor.getType() == DataTypes.GEOMETRY) {
|
410 | return;
|
||
411 | } else {
|
||
412 | super.loadValue(featureProvider, rowIndex, descriptor);
|
||
413 | } |
||
414 | } |
||
415 | 40435 | jjdelcerro | |
416 | 41610 | jjdelcerro | public FeatureProvider createFeatureProvider(FeatureType type) throws DataException { |
417 | FeatureProvider data = new SHPFeatureProvider(this, type); |
||
418 | return data;
|
||
419 | } |
||
420 | 40435 | jjdelcerro | |
421 | 41610 | jjdelcerro | protected void openFile() throws IOException, DataException { |
422 | super.openFile();
|
||
423 | this.shpFile.open();
|
||
424 | 40435 | jjdelcerro | |
425 | 41610 | jjdelcerro | } |
426 | 40435 | jjdelcerro | |
427 | 41610 | jjdelcerro | protected void closeFile() throws CloseException { |
428 | super.closeFile();
|
||
429 | if (!this.shpFile.isOpen()) { |
||
430 | return;
|
||
431 | } |
||
432 | this.shpFile.close();
|
||
433 | } |
||
434 | 40435 | jjdelcerro | |
435 | 42574 | fdiaz | public boolean canWriteGeometry(final int geometryType, int geometrySubType) throws DataException { |
436 | 41610 | jjdelcerro | this.open();
|
437 | return ((Boolean) getResource().execute(new ResourceAction() { |
||
438 | 42574 | fdiaz | |
439 | 41610 | jjdelcerro | public Object run() throws Exception { |
440 | boolean value = shpFile.canWriteGeometry(geometryType);
|
||
441 | return value ? Boolean.TRUE : Boolean.FALSE; |
||
442 | } |
||
443 | })).booleanValue(); |
||
444 | } |
||
445 | 40435 | jjdelcerro | |
446 | 42574 | fdiaz | @SuppressWarnings("rawtypes") |
447 | public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, |
||
448 | Iterator originalFeatureTypesUpdated) throws PerformEditingException { |
||
449 | 43978 | omartinez | final List<Object> originalFeatureTypesUpdatedList = new ArrayList<>(); |
450 | while (originalFeatureTypesUpdated.hasNext()) {
|
||
451 | originalFeatureTypesUpdatedList.add(originalFeatureTypesUpdated.next()); |
||
452 | } |
||
453 | |||
454 | 41610 | jjdelcerro | /*
|
455 | * This will throw an exception if there are new fields
|
||
456 | * with names too long
|
||
457 | */
|
||
458 | 43978 | omartinez | checkNewFieldsNameSize(originalFeatureTypesUpdatedList.iterator()); |
459 | 40435 | jjdelcerro | |
460 | 41610 | jjdelcerro | final FeatureType fType;
|
461 | try {
|
||
462 | fType = this.getStoreServices().getDefaultFeatureType();
|
||
463 | } catch (DataException e) {
|
||
464 | throw new PerformEditingException(this.getProviderName(), e); |
||
465 | } |
||
466 | try {
|
||
467 | 42517 | dmartinezizquierdo | // TODO repasar el concepto de enUso de un recurso.
|
468 | loTengoEnUso = true;
|
||
469 | resourceCloseRequest(); |
||
470 | 40435 | jjdelcerro | |
471 | 41610 | jjdelcerro | getResource().execute(new ResourceAction() {
|
472 | 42574 | fdiaz | |
473 | 41610 | jjdelcerro | public Object run() throws Exception { |
474 | FeatureSet set = null;
|
||
475 | DisposableIterator iter = null;
|
||
476 | try {
|
||
477 | set = getFeatureStore().getFeatureSet(); |
||
478 | writer = new SHPFeatureWriter(getProviderName());
|
||
479 | 40435 | jjdelcerro | |
480 | 41610 | jjdelcerro | SHPStoreParameters shpParams = getShpParameters(); |
481 | 42574 | fdiaz | SHPStoreParameters tmpParams = (SHPStoreParameters) shpParams.getCopy(); |
482 | 40435 | jjdelcerro | |
483 | 42574 | fdiaz | File tmp_base = File.createTempFile("tmp_" + System.currentTimeMillis(), null); |
484 | 41610 | jjdelcerro | String str_base = tmp_base.getCanonicalPath();
|
485 | 40435 | jjdelcerro | |
486 | 41610 | jjdelcerro | tmpParams.setDBFFile(str_base + ".dbf");
|
487 | tmpParams.setSHPFile(str_base + ".shp");
|
||
488 | tmpParams.setSHXFile(str_base + ".shx");
|
||
489 | 40435 | jjdelcerro | |
490 | 44669 | jjdelcerro | writer.begin(tmpParams, fType, set.getSize()); |
491 | 40435 | jjdelcerro | |
492 | 41610 | jjdelcerro | iter = set.fastIterator(); |
493 | while (iter.hasNext()) {
|
||
494 | Feature feature = (Feature) iter.next(); |
||
495 | writer.append(feature); |
||
496 | } |
||
497 | 40435 | jjdelcerro | |
498 | 41610 | jjdelcerro | writer.end(); |
499 | 42517 | dmartinezizquierdo | loTengoEnUso = false;
|
500 | 41610 | jjdelcerro | close(); |
501 | |||
502 | 42464 | fdiaz | |
503 | 41610 | jjdelcerro | if (!shpParams.getDBFFile().delete()) {
|
504 | 44669 | jjdelcerro | LOGGER.debug("Can't delete dbf file '" + shpParams.getDBFFile() + "'."); |
505 | 42574 | fdiaz | throw new IOException("Can't delete dbf '" |
506 | + FilenameUtils.getBaseName(shpParams.getDBFFileName()) |
||
507 | + "' file to replace with the new dbf.\nThe new dbf is in temporary file '" + str_base
|
||
508 | + "'");
|
||
509 | 41610 | jjdelcerro | } |
510 | if (!shpParams.getSHPFile().delete()) {
|
||
511 | 44669 | jjdelcerro | LOGGER.debug("Can't delete dbf file '" + shpParams.getSHPFile() + "'."); |
512 | 42574 | fdiaz | throw new IOException("Can't delete shp '" |
513 | + FilenameUtils.getBaseName(shpParams.getSHPFileName()) |
||
514 | + "' file to replace with the new shp.\nThe new shp is in temporary file '" + str_base
|
||
515 | + "'");
|
||
516 | 41610 | jjdelcerro | } |
517 | if (!shpParams.getSHXFile().delete()) {
|
||
518 | 44669 | jjdelcerro | LOGGER.debug("Can't delete dbf file '" + shpParams.getSHXFile() + "'."); |
519 | 42574 | fdiaz | throw new IOException("Can't delete shx '" |
520 | + FilenameUtils.getBaseName(shpParams.getSHXFileName()) |
||
521 | + "' file to replace with the new shx.\nThe new shx is in temporary file '" + str_base
|
||
522 | + "'");
|
||
523 | 41610 | jjdelcerro | } |
524 | 43245 | jjdelcerro | if (shpParams.getCPGFile().exists() && !shpParams.getCPGFile().delete()) {
|
525 | 44669 | jjdelcerro | LOGGER.debug("Can't delete cpg file '" + shpParams.getCPGFile() + "'."); |
526 | 43245 | jjdelcerro | throw new IOException("Can't delete cpg '" |
527 | + FilenameUtils.getBaseName(shpParams.getCPGFileName()) |
||
528 | + "' file to replace with the new cpg.\nThe new cpg is in temporary file '" + str_base
|
||
529 | + "'");
|
||
530 | } |
||
531 | 42567 | fdiaz | |
532 | File prjFile = SHP.getPrjFile(shpParams.getSHPFile());
|
||
533 | if (prjFile.exists()) {
|
||
534 | if (!prjFile.delete()) {
|
||
535 | 44669 | jjdelcerro | LOGGER.debug("Can't delete prj file '" + prjFile + "'."); |
536 | 42567 | fdiaz | throw new IOException("Can't delete shx '" |
537 | + FilenameUtils.getBaseName(prjFile.getPath()) |
||
538 | + "' file to replace with the new shx.\nThe new shx is in temporary file '"
|
||
539 | + str_base + "'");
|
||
540 | } |
||
541 | } |
||
542 | 42574 | fdiaz | FileUtils.moveFile(tmpParams.getDBFFile(), shpParams.getDBFFile()); |
543 | FileUtils.moveFile(tmpParams.getSHPFile(), shpParams.getSHPFile()); |
||
544 | FileUtils.moveFile(tmpParams.getSHXFile(), shpParams.getSHXFile()); |
||
545 | 43245 | jjdelcerro | FileUtils.moveFile(tmpParams.getCPGFile(), shpParams.getCPGFile()); |
546 | 41610 | jjdelcerro | |
547 | 42712 | fdiaz | savePrjFile(shpParams.getFile(), tmpParams.getCRS()); |
548 | 42567 | fdiaz | |
549 | 41610 | jjdelcerro | resourcesNotifyChanges(); |
550 | 43978 | omartinez | if (!originalFeatureTypesUpdatedList.isEmpty()) {
|
551 | initFeatureType(); |
||
552 | } |
||
553 | |||
554 | 41610 | jjdelcerro | return null; |
555 | } finally {
|
||
556 | 42517 | dmartinezizquierdo | loTengoEnUso = false;
|
557 | 41610 | jjdelcerro | dispose(set); |
558 | dispose(iter); |
||
559 | } |
||
560 | } |
||
561 | }); |
||
562 | 40435 | jjdelcerro | |
563 | 41610 | jjdelcerro | } catch (Exception e) { |
564 | throw new PerformEditingException(this.getProviderName(), e); |
||
565 | } |
||
566 | 40435 | jjdelcerro | |
567 | 41610 | jjdelcerro | } |
568 | 40435 | jjdelcerro | |
569 | 41610 | jjdelcerro | protected void resourceCloseRequest() throws ResourceException { |
570 | getResource().closeRequest(); |
||
571 | } |
||
572 | 40435 | jjdelcerro | |
573 | 41610 | jjdelcerro | public Envelope getEnvelope() throws DataException { |
574 | this.open();
|
||
575 | return (Envelope) this.getDynValue("Envelope"); |
||
576 | } |
||
577 | 40435 | jjdelcerro | |
578 | 41610 | jjdelcerro | public void append(final FeatureProvider featureProvider) throws DataException { |
579 | getResource().execute(new ResourceAction() {
|
||
580 | 42574 | fdiaz | |
581 | 41610 | jjdelcerro | public Object run() throws Exception { |
582 | writer.append(getStoreServices().createFeature(featureProvider)); |
||
583 | return null; |
||
584 | } |
||
585 | }); |
||
586 | } |
||
587 | 40435 | jjdelcerro | |
588 | 41610 | jjdelcerro | public void beginAppend() throws DataException { |
589 | getResource().execute(new ResourceAction() {
|
||
590 | 42574 | fdiaz | |
591 | 41610 | jjdelcerro | public Object run() throws Exception { |
592 | FeatureStore store = getFeatureStore(); |
||
593 | FeatureType fType = store.getDefaultFeatureType(); |
||
594 | 40435 | jjdelcerro | |
595 | 41610 | jjdelcerro | writer = new SHPFeatureWriter(getProviderName());
|
596 | 44669 | jjdelcerro | writer.begin(getShpParameters(), fType, store.getFeatureCount()); |
597 | 41610 | jjdelcerro | return null; |
598 | } |
||
599 | }); |
||
600 | } |
||
601 | 40435 | jjdelcerro | |
602 | 41610 | jjdelcerro | public void endAppend() throws DataException { |
603 | getResource().execute(new ResourceAction() {
|
||
604 | 42574 | fdiaz | |
605 | 41610 | jjdelcerro | public Object run() throws Exception { |
606 | writer.end(); |
||
607 | 41882 | jjdelcerro | close(); |
608 | 42572 | fdiaz | |
609 | SHPStoreParameters shpParameters = SHPStoreProvider.this.getShpParameters(); |
||
610 | |||
611 | 42712 | fdiaz | savePrjFile(shpParameters.getFile(), shpParameters.getCRS()); |
612 | |||
613 | 41610 | jjdelcerro | resourcesNotifyChanges(); |
614 | return null; |
||
615 | } |
||
616 | }); |
||
617 | } |
||
618 | 40435 | jjdelcerro | |
619 | 41610 | jjdelcerro | public Object getSourceId() { |
620 | return this.getShpParameters().getFile(); |
||
621 | } |
||
622 | 42712 | fdiaz | |
623 | |||
624 | 41610 | jjdelcerro | } |