gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.base / src / main / java / org / gvsig / sextante / app / algorithm / base / core / DALFeaturePersister.java @ 28
History | View | Annotate | Download (3.74 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 |
*
|
4 |
* Copyright (C) 2010 Generalitat Valenciana.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*/
|
20 |
|
21 |
package org.gvsig.sextante.app.algorithm.base.core; |
22 |
|
23 |
import java.util.Iterator; |
24 |
|
25 |
import org.gvsig.fmap.dal.exception.DataException; |
26 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
27 |
import org.gvsig.fmap.dal.feature.Feature; |
28 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
29 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
30 |
import org.gvsig.fmap.dal.feature.FeatureType; |
31 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
32 |
import org.gvsig.fmap.geom.util.Converter; |
33 |
import org.gvsig.sextante.app.extension.core.gvVectorLayer; |
34 |
|
35 |
import com.vividsolutions.jts.geom.Geometry; |
36 |
|
37 |
import es.unex.sextante.core.Sextante; |
38 |
import es.unex.sextante.dataObjects.IVectorLayer; |
39 |
import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
40 |
|
41 |
/**
|
42 |
* Writes features in a FeatureStore
|
43 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
44 |
*/
|
45 |
public class DALFeaturePersister { |
46 |
private FeatureStore store = null; |
47 |
private String[] fieldNames = null; |
48 |
|
49 |
/**
|
50 |
* Sets the output FeatureType
|
51 |
* @param out
|
52 |
* @throws DataException
|
53 |
*/
|
54 |
public DALFeaturePersister(FeatureStore out, String[] fieldNames) { |
55 |
this.store = out;
|
56 |
this.fieldNames = fieldNames;
|
57 |
try {
|
58 |
store.edit(); |
59 |
} catch (DataException e) {
|
60 |
} |
61 |
} |
62 |
|
63 |
/**
|
64 |
* Adds a JTS feature to the FeatureStore
|
65 |
* @param entry
|
66 |
* @param newGeom
|
67 |
* @throws CreateGeometryException
|
68 |
* @throws DataException
|
69 |
*/
|
70 |
public EditableFeature addFeature(Feature feature, Geometry newGeom) throws CreateGeometryException, DataException { |
71 |
org.gvsig.fmap.geom.Geometry newDalGeom = Converter.jtsToGeometry(newGeom); |
72 |
EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), feature); |
73 |
feat.setGeometry("GEOMETRY", newDalGeom);
|
74 |
store.insert(feat); |
75 |
return feat;
|
76 |
} |
77 |
|
78 |
/**
|
79 |
* Adds a DAL feature to the FeatureStore
|
80 |
* @param entry
|
81 |
* @param newGeom
|
82 |
* @throws CreateGeometryException
|
83 |
* @throws DataException
|
84 |
*/
|
85 |
public EditableFeature addFeature(Feature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
86 |
EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), feature); |
87 |
feat.setGeometry(fieldNames.length, newGeom); |
88 |
store.insert(feat); |
89 |
return feat;
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* Adds a DAL feature to the FeatureStore
|
94 |
* @param entry
|
95 |
* @param newGeom
|
96 |
* @throws CreateGeometryException
|
97 |
* @throws DataException
|
98 |
*/
|
99 |
public EditableFeature addGeometryToExistingFeature(EditableFeature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
100 |
for (int i = 0; i < fieldNames.length; i++) |
101 |
feature.set(fieldNames[i], feature.get(i)); |
102 |
|
103 |
feature.setGeometry(fieldNames.length, newGeom); |
104 |
store.update(feature); |
105 |
return feature;
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* Ends the edition and closes the FeatureStore
|
110 |
*/
|
111 |
public void end() { |
112 |
try {
|
113 |
store.finishEditing(); |
114 |
} catch (DataException e) {
|
115 |
Sextante.addErrorToLog(e); |
116 |
} |
117 |
store.dispose(); |
118 |
} |
119 |
} |
120 |
|