svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geometrymeasurement.app / org.gvsig.geometrymeasurement.app.mainplugin / src / main / java / org / gvsig / geometrymeasurement / app / extension / utils / Operations.java @ 44137
History | View | Annotate | Download (7.43 KB)
1 | 40557 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40557 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40557 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | 40557 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.geometrymeasurement.app.extension.utils; |
||
25 | |||
26 | import java.util.Iterator; |
||
27 | 44137 | omartinez | import java.util.regex.Pattern; |
28 | 40435 | jjdelcerro | |
29 | import org.slf4j.Logger; |
||
30 | import org.slf4j.LoggerFactory; |
||
31 | |||
32 | import org.gvsig.app.project.documents.table.TableOperations; |
||
33 | import org.gvsig.fmap.dal.exception.DataException; |
||
34 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
35 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
36 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
37 | import org.gvsig.fmap.dal.feature.Feature; |
||
38 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
39 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
40 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
41 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
42 | import org.gvsig.fmap.dal.feature.exception.NeedEditingModeException; |
||
43 | 43463 | jjdelcerro | import org.gvsig.fmap.geom.Geometry; |
44 | 40435 | jjdelcerro | import org.gvsig.fmap.geom.operation.GeometryOperationException; |
45 | import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
||
46 | import org.gvsig.fmap.geom.primitive.Point; |
||
47 | import org.gvsig.tools.dataTypes.DataTypes; |
||
48 | import org.gvsig.tools.dispose.DisposableIterator; |
||
49 | |||
50 | /**
|
||
51 | * Andami extension to show PerimeterMeasurementExtension in the application.
|
||
52 | *
|
||
53 | * @author gvSIG Team
|
||
54 | * @version $Id$
|
||
55 | */
|
||
56 | public class Operations { |
||
57 | |||
58 | private static final Logger LOG = LoggerFactory.getLogger(Operations.class); |
||
59 | |||
60 | private static final int NUM_DECIMALS = 5; |
||
61 | |||
62 | public void addDoubleFieldFromOperation(FeatureStore featureStore, |
||
63 | String fieldName, String operationName) throws DataException, |
||
64 | GeometryOperationNotSupportedException, GeometryOperationException { |
||
65 | boolean isEditing = false; |
||
66 | if (featureStore.isEditing()) {
|
||
67 | isEditing = true;
|
||
68 | } else {
|
||
69 | featureStore.edit(); |
||
70 | } |
||
71 | |||
72 | featureStore.beginEditingGroup(operationName); |
||
73 | |||
74 | // Adding the new attribute type
|
||
75 | FeatureType featureType = featureStore.getDefaultFeatureType(); |
||
76 | EditableFeatureType editableFeatureType = featureType.getEditable(); |
||
77 | |||
78 | String realFieldName = getFieldName(featureStore, fieldName, 0); |
||
79 | EditableFeatureAttributeDescriptor editableFeatureAttributeDescriptor = |
||
80 | editableFeatureType.add(realFieldName, DataTypes.DOUBLE); |
||
81 | editableFeatureAttributeDescriptor.setPrecision(NUM_DECIMALS); |
||
82 | editableFeatureAttributeDescriptor |
||
83 | .setSize(TableOperations.MAX_FIELD_LENGTH); |
||
84 | |||
85 | featureStore.update(editableFeatureType); |
||
86 | |||
87 | DisposableIterator disposableIterator = null;
|
||
88 | FeatureSet featureSet = null;
|
||
89 | try {
|
||
90 | // Adding the attribute value
|
||
91 | featureSet = featureStore.getFeatureSet(); |
||
92 | disposableIterator = featureSet.fastIterator(); |
||
93 | while (disposableIterator.hasNext()) {
|
||
94 | Feature feature = (Feature) disposableIterator.next(); |
||
95 | EditableFeature editableFeature = feature.getEditable(); |
||
96 | 43463 | jjdelcerro | Geometry geom = feature.getDefaultGeometry(); |
97 | if( geom!=null ) { |
||
98 | editableFeature.setDouble(realFieldName, (Double) geom.invokeOperation(operationName, null)); |
||
99 | } |
||
100 | 40435 | jjdelcerro | featureSet.update(editableFeature); |
101 | } |
||
102 | } finally {
|
||
103 | if (disposableIterator != null) { |
||
104 | disposableIterator.dispose(); |
||
105 | } |
||
106 | if (featureSet != null) { |
||
107 | featureSet.dispose(); |
||
108 | } |
||
109 | try {
|
||
110 | featureStore.endEditingGroup(); |
||
111 | } catch (NeedEditingModeException e) {
|
||
112 | LOG.error("Exception endEditingGroup", e);
|
||
113 | } |
||
114 | } |
||
115 | |||
116 | if (!isEditing) {
|
||
117 | featureStore.finishEditing(); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | public String getFieldName(FeatureStore featureStore, String fieldName, |
||
122 | int index) throws DataException { |
||
123 | String fullName = fieldName;
|
||
124 | if (index != 0) { |
||
125 | fullName = fullName + index; |
||
126 | } |
||
127 | @SuppressWarnings("unchecked") |
||
128 | Iterator<FeatureAttributeDescriptor> it =
|
||
129 | featureStore.getDefaultFeatureType().iterator(); |
||
130 | while (it.hasNext()) {
|
||
131 | FeatureAttributeDescriptor featureAttributeDescriptor = it.next(); |
||
132 | 44137 | omartinez | if (featureAttributeDescriptor.getName().toUpperCase().equals(fullName.toUpperCase())) {
|
133 | 40435 | jjdelcerro | return getFieldName(featureStore, fieldName, index + 1); |
134 | } |
||
135 | } |
||
136 | return fullName;
|
||
137 | } |
||
138 | |||
139 | /**
|
||
140 | * @param featureStore
|
||
141 | * @param string
|
||
142 | * @param string2
|
||
143 | * @throws DataException
|
||
144 | */
|
||
145 | public void addXYPoints(FeatureStore featureStore, String xFieldName, |
||
146 | String yFieldName) throws DataException { |
||
147 | boolean isEditing = false; |
||
148 | if (featureStore.isEditing()) {
|
||
149 | isEditing = true;
|
||
150 | } else {
|
||
151 | featureStore.edit(); |
||
152 | } |
||
153 | |||
154 | // Adding the new attribute type
|
||
155 | FeatureType featureType = featureStore.getDefaultFeatureType(); |
||
156 | EditableFeatureType editableFeatureType = featureType.getEditable(); |
||
157 | |||
158 | String realXFieldName = getFieldName(featureStore, xFieldName, 0); |
||
159 | EditableFeatureAttributeDescriptor editableFeatureAttributeDescriptor = |
||
160 | editableFeatureType.add(realXFieldName, DataTypes.DOUBLE); |
||
161 | editableFeatureAttributeDescriptor.setPrecision(NUM_DECIMALS); |
||
162 | editableFeatureAttributeDescriptor |
||
163 | .setSize(TableOperations.MAX_FIELD_LENGTH); |
||
164 | |||
165 | String realYFieldName = getFieldName(featureStore, yFieldName, 0); |
||
166 | editableFeatureAttributeDescriptor = |
||
167 | editableFeatureType.add(realYFieldName, DataTypes.DOUBLE); |
||
168 | editableFeatureAttributeDescriptor.setPrecision(NUM_DECIMALS); |
||
169 | editableFeatureAttributeDescriptor |
||
170 | .setSize(TableOperations.MAX_FIELD_LENGTH); |
||
171 | |||
172 | featureStore.update(editableFeatureType); |
||
173 | |||
174 | // Adding the attribute value
|
||
175 | FeatureSet featureSet = featureStore.getFeatureSet(); |
||
176 | DisposableIterator disposableIterator = featureSet.fastIterator(); |
||
177 | while (disposableIterator.hasNext()) {
|
||
178 | Feature feature = (Feature) disposableIterator.next(); |
||
179 | EditableFeature editableFeature = feature.getEditable(); |
||
180 | 43463 | jjdelcerro | Geometry geom = feature.getDefaultGeometry(); |
181 | if( geom!=null ) { |
||
182 | editableFeature.setDouble(realXFieldName,((Point)geom).getX());
|
||
183 | editableFeature.setDouble(realYFieldName,((Point)geom).getY());
|
||
184 | } |
||
185 | 40435 | jjdelcerro | featureSet.update(editableFeature); |
186 | } |
||
187 | |||
188 | featureSet.dispose(); |
||
189 | |||
190 | if (!isEditing) {
|
||
191 | featureStore.finishEditing(); |
||
192 | } |
||
193 | |||
194 | } |
||
195 | |||
196 | } |