gvsig-gdal / trunk / org.gvsig.gdal / org.gvsig.gdal.prov / org.gvsig.gdal.prov.ogr / src / main / java / org / gvsig / gdal / prov / ogr / OGRConverter.java @ 209
History | View | Annotate | Download (25 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2016 gvSIG Association
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.gdal.prov.ogr; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.Calendar; |
28 |
import java.util.Date; |
29 |
import java.util.List; |
30 |
|
31 |
import org.apache.commons.lang3.StringUtils; |
32 |
import org.cresques.cts.ICRSFactory; |
33 |
import org.cresques.cts.IProjection; |
34 |
import org.gdal.ogr.DataSource; |
35 |
import org.gdal.ogr.Feature; |
36 |
import org.gdal.ogr.FeatureDefn; |
37 |
import org.gdal.ogr.FieldDefn; |
38 |
import org.gdal.ogr.GeomFieldDefn; |
39 |
import org.gdal.ogr.Layer; |
40 |
import org.gdal.ogr.ogr; |
41 |
import org.gdal.ogr.ogrConstants; |
42 |
import org.gdal.osr.SpatialReference; |
43 |
import org.gvsig.fmap.crs.CRSFactory; |
44 |
import org.gvsig.fmap.dal.DALLocator; |
45 |
import org.gvsig.fmap.dal.DataTypes; |
46 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
47 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
48 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
49 |
import org.gvsig.fmap.dal.feature.FeatureType; |
50 |
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider; |
51 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
52 |
import org.gvsig.fmap.geom.Geometry; |
53 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
54 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
55 |
import org.gvsig.fmap.geom.GeometryLocator; |
56 |
import org.gvsig.fmap.geom.GeometryManager; |
57 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
58 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
59 |
import org.gvsig.fmap.geom.type.GeometryType; |
60 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
61 |
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException; |
62 |
import org.gvsig.tools.exception.BaseException; |
63 |
import org.slf4j.Logger; |
64 |
import org.slf4j.LoggerFactory; |
65 |
|
66 |
/**
|
67 |
* Converter class used to parse OGR layer defn, feature, types... to gvSIG
|
68 |
* objects and constant.
|
69 |
*
|
70 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
71 |
*
|
72 |
*/
|
73 |
public class OGRConverter { |
74 |
|
75 |
/**
|
76 |
* Default geometry column name used when the geometry column of layer does
|
77 |
* not have name. This is necessary to create FeatureType and it must take
|
78 |
* into account when feature geometry is requested.
|
79 |
*/
|
80 |
private final String DEFAULT_GEOMETRY_COLUMN_NAME = "ogr_geometry"; |
81 |
|
82 |
private static final Logger LOG = LoggerFactory.getLogger(OGRConverter.class); |
83 |
|
84 |
/**
|
85 |
*
|
86 |
*/
|
87 |
public OGRConverter() {
|
88 |
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* Converts {@link FeatureDefn} to {@link FeatureType} and sets
|
93 |
* default geometry field to created {@link FeatureType}.
|
94 |
*
|
95 |
* @param featureDefn
|
96 |
* OGR definition to be converted
|
97 |
* @param defaultGeometryField
|
98 |
* Default geometry field. If it is not founded, this method will
|
99 |
* take the first geometry field.
|
100 |
* @return FeatureType Feature type equivalent
|
101 |
* @throws GeometryTypeNotSupportedException
|
102 |
* If there are problems parsing OGR geometry
|
103 |
* @throws GeometryTypeNotValidException
|
104 |
* If type of OGR Geometry is not supported
|
105 |
*/
|
106 |
public FeatureType convert(FeatureDefn featureDefn, String defaultGeometryField) |
107 |
throws GeometryTypeNotSupportedException, GeometryTypeNotValidException {
|
108 |
|
109 |
EditableFeatureType editableFeatureType = DALLocator.getDataManager().createFeatureType(); |
110 |
editableFeatureType.setHasOID(true);
|
111 |
|
112 |
for (int i = 0; i < featureDefn.GetFieldCount(); i++) { |
113 |
FieldDefn fieldDefn = featureDefn.GetFieldDefn(i); |
114 |
|
115 |
EditableFeatureAttributeDescriptor attributeDescriptor = |
116 |
editableFeatureType.add(fieldDefn.GetName(), |
117 |
this.convertToDataType(fieldDefn.GetFieldType()));
|
118 |
attributeDescriptor.setPrecision(fieldDefn.GetPrecision()); |
119 |
} |
120 |
|
121 |
for (int i = 0; i < featureDefn.GetGeomFieldCount(); i++) { |
122 |
GeomFieldDefn geomFieldDefn = featureDefn.GetGeomFieldDefn(i); |
123 |
|
124 |
String name = null; |
125 |
if (StringUtils.isBlank(geomFieldDefn.GetName())) {
|
126 |
// Set default geometry column name
|
127 |
name = DEFAULT_GEOMETRY_COLUMN_NAME; |
128 |
} else {
|
129 |
name = geomFieldDefn.GetName(); |
130 |
} |
131 |
|
132 |
EditableFeatureAttributeDescriptor attributeDescriptor = |
133 |
editableFeatureType.add(name, DataTypes.GEOMETRY); |
134 |
|
135 |
// GDAL 1.11.2 Only supports 2D geometries
|
136 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
137 |
GeometryType geometryType = |
138 |
geometryManager.getGeometryType( |
139 |
this.convertToGeomType(geomFieldDefn.GetFieldType()), SUBTYPES.GEOM2D);
|
140 |
attributeDescriptor.setGeometryType(geometryType); |
141 |
|
142 |
if (geomFieldDefn.GetSpatialRef() != null) { |
143 |
IProjection projection = |
144 |
CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT, |
145 |
geomFieldDefn.GetSpatialRef().ExportToWkt()); |
146 |
attributeDescriptor.setSRS(projection); |
147 |
} |
148 |
} |
149 |
|
150 |
if (editableFeatureType.getAttributeDescriptor(defaultGeometryField) != null) { |
151 |
editableFeatureType.setDefaultGeometryAttributeName(defaultGeometryField); |
152 |
} else {
|
153 |
FeatureAttributeDescriptor[] attributeDescriptors =
|
154 |
editableFeatureType.getAttributeDescriptors(); |
155 |
for (int i = 0; i < attributeDescriptors.length; i++) { |
156 |
|
157 |
if (attributeDescriptors[i].getDataType().getType() != DataTypes.GEOMETRY) {
|
158 |
continue;
|
159 |
} |
160 |
|
161 |
if (attributeDescriptors[i].getGeomType().getType() == convertToGeomType(featureDefn
|
162 |
.GetGeomType())) { |
163 |
editableFeatureType.setDefaultGeometryAttributeName(attributeDescriptors[i] |
164 |
.getName()); |
165 |
LOG.warn( |
166 |
"Default geometry field {} has not been found. Setting \"{}\" as default geometry field",
|
167 |
new Object[] { defaultGeometryField, attributeDescriptors[i].getName() }); |
168 |
break;
|
169 |
} |
170 |
} |
171 |
|
172 |
if (editableFeatureType.getDefaultGeometryAttributeName() == null) { |
173 |
LOG.warn("The default geometry field can not be set");
|
174 |
} |
175 |
} |
176 |
|
177 |
return editableFeatureType.getNotEditableCopy();
|
178 |
} |
179 |
|
180 |
/**
|
181 |
* Converts {@link FeatureAttributeDescriptor} of {@link FeatureType} to
|
182 |
* {@link FieldDefn}
|
183 |
*
|
184 |
* @param featureType
|
185 |
* Fields to be converted
|
186 |
* @return FieldDefn list OGR fields
|
187 |
*/
|
188 |
public List<FieldDefn> convertFields(FeatureType featureType) { |
189 |
|
190 |
List<FieldDefn> fields = new ArrayList<FieldDefn>(); |
191 |
|
192 |
for (int i = 0; i < featureType.getAttributeDescriptors().length; i++) { |
193 |
FeatureAttributeDescriptor attributeDescriptor = featureType.getAttributeDescriptor(i); |
194 |
if (attributeDescriptor.getType() != DataTypes.GEOMETRY) {
|
195 |
fields.add(convertField(attributeDescriptor)); |
196 |
} |
197 |
} |
198 |
return fields;
|
199 |
} |
200 |
|
201 |
/**
|
202 |
* Converts {@link FeatureAttributeDescriptor} to {@link FieldDefn}
|
203 |
*
|
204 |
* @param attDescriptor
|
205 |
* @return FieldDefn
|
206 |
*/
|
207 |
public FieldDefn convertField(FeatureAttributeDescriptor attDescriptor) {
|
208 |
if (attDescriptor != null) { |
209 |
FieldDefn fieldDefn = |
210 |
new FieldDefn(attDescriptor.getName(), this.convertToOGRType(attDescriptor |
211 |
.getType())); |
212 |
fieldDefn.SetPrecision(attDescriptor.getPrecision()); |
213 |
return fieldDefn;
|
214 |
} |
215 |
return null; |
216 |
} |
217 |
|
218 |
/**
|
219 |
* Converts geometry {@link FeatureAttributeDescriptor} of
|
220 |
* {@link FeatureType} to {@link GeomFieldDefn}
|
221 |
*
|
222 |
* @param featureType
|
223 |
* Geometry fields to be converted
|
224 |
* @param convertDefaultGeometryColumn
|
225 |
* True if default geometry will be converted, otherwise default
|
226 |
* geometry field won't be converted.
|
227 |
* @return GeomFieldDefn list OGR Geometry fields
|
228 |
*/
|
229 |
public List<GeomFieldDefn> convertGeometryFields(FeatureType featureType, |
230 |
boolean convertDefaultGeometryColumn) {
|
231 |
|
232 |
List<GeomFieldDefn> geometryFields = new ArrayList<GeomFieldDefn>(); |
233 |
|
234 |
for (int i = 0; i < featureType.getAttributeDescriptors().length; i++) { |
235 |
FeatureAttributeDescriptor attributeDescriptor = featureType.getAttributeDescriptor(i); |
236 |
|
237 |
if (attributeDescriptor.getType() == DataTypes.GEOMETRY) {
|
238 |
// Check if default geometry column must be converted too
|
239 |
if (i != featureType.getDefaultGeometryAttributeIndex()
|
240 |
|| (i == featureType.getDefaultGeometryAttributeIndex() && convertDefaultGeometryColumn)) { |
241 |
geometryFields.add(convertGeomField(attributeDescriptor)); |
242 |
} |
243 |
} |
244 |
} |
245 |
return geometryFields;
|
246 |
} |
247 |
|
248 |
/**
|
249 |
* Converts {@link FeatureAttributeDescriptor} to {@link GeomFieldDefn}
|
250 |
*
|
251 |
* @param attDescriptor
|
252 |
* Attribute to be converted
|
253 |
* @return GeomFieldDefn OGR Geometry field
|
254 |
*/
|
255 |
public GeomFieldDefn convertGeomField(FeatureAttributeDescriptor attDescriptor) {
|
256 |
|
257 |
String name = attDescriptor.getName();
|
258 |
if (attDescriptor.getName().equals(DEFAULT_GEOMETRY_COLUMN_NAME)) {
|
259 |
name = "";
|
260 |
} |
261 |
|
262 |
GeomFieldDefn geomFieldDefn = |
263 |
new GeomFieldDefn(name,
|
264 |
this.convertToOGRGeomType(attDescriptor.getGeomType().getType()));
|
265 |
|
266 |
if (attDescriptor.getSRS() != null) { |
267 |
SpatialReference srs = |
268 |
new SpatialReference(attDescriptor.getSRS().export(ICRSFactory.FORMAT_WKT));
|
269 |
geomFieldDefn.SetSpatialRef(srs); |
270 |
} |
271 |
|
272 |
return geomFieldDefn;
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* Converts {@link Feature} to {@link FeatureProvider}. If
|
277 |
* {@link FeatureProvider} is null, it will be instantiated. This method
|
278 |
* only takes into account the fields of received {@link FeatureType}.
|
279 |
*
|
280 |
* @param featureProvider
|
281 |
*
|
282 |
* @param featureType
|
283 |
* Type of feature provider
|
284 |
* @param ogrFeature
|
285 |
* OGR Feature to be converted
|
286 |
* @return FeatureProvider
|
287 |
* FeatureProvder with OGR Feature values
|
288 |
*/
|
289 |
public FeatureProvider convert(FeatureProvider featureProvider, FeatureType featureType,
|
290 |
Feature ogrFeature) { |
291 |
|
292 |
int fid = ogrFeature.GetFID();
|
293 |
if (featureProvider == null) { |
294 |
featureProvider = new DefaultFeatureProvider(featureType, fid);
|
295 |
} else {
|
296 |
featureProvider.setOID(fid); |
297 |
} |
298 |
|
299 |
FeatureDefn defnRef = ogrFeature.GetDefnRef(); |
300 |
FeatureAttributeDescriptor[] attributeDescriptors = featureType.getAttributeDescriptors();
|
301 |
for (int i = 0; i < attributeDescriptors.length; i++) { |
302 |
|
303 |
if (attributeDescriptors[i].getDataType().getType() == DataTypes.GEOMETRY) {
|
304 |
|
305 |
String name = attributeDescriptors[i].getName();
|
306 |
|
307 |
org.gdal.ogr.Geometry ogrGeometry = null;
|
308 |
int fieldIndex = -1; |
309 |
|
310 |
if (name.equals(DEFAULT_GEOMETRY_COLUMN_NAME)) {
|
311 |
// If geometry column name equals default column name,
|
312 |
// change it to empty string.
|
313 |
fieldIndex = defnRef.GetGeomFieldIndex("");
|
314 |
} else {
|
315 |
fieldIndex = defnRef.GetGeomFieldIndex(name); |
316 |
} |
317 |
|
318 |
ogrGeometry = ogrFeature.GetGeomFieldRef(fieldIndex); |
319 |
if (ogrGeometry != null) { |
320 |
Geometry geometry = null;
|
321 |
try {
|
322 |
geometry = |
323 |
GeometryLocator.getGeometryManager().createFrom( |
324 |
ogrGeometry.ExportToWkb()); |
325 |
} catch (BaseException e) {
|
326 |
LOG.warn( |
327 |
String
|
328 |
.format( |
329 |
"Can not create geometry from %1s. Setting null value to geometry field",
|
330 |
ogrGeometry.ExportToWkt()), e); |
331 |
geometry = null;
|
332 |
} |
333 |
|
334 |
if (attributeDescriptors[i].getName().equals(
|
335 |
featureType.getDefaultGeometryAttributeName())) { |
336 |
featureProvider.setDefaultGeometry(geometry); |
337 |
} |
338 |
|
339 |
featureProvider.set(name, geometry); |
340 |
} |
341 |
|
342 |
} else {
|
343 |
|
344 |
String name = attributeDescriptors[i].getName();
|
345 |
int fieldIndex = defnRef.GetFieldIndex(name);
|
346 |
FieldDefn fieldDefn = defnRef.GetFieldDefn(fieldIndex); |
347 |
int dataType = convertToDataType(fieldDefn.GetFieldType());
|
348 |
|
349 |
Object value = null; |
350 |
|
351 |
if (dataType == DataTypes.INT) {
|
352 |
value = ogrFeature.GetFieldAsInteger(i); |
353 |
} else if (dataType == DataTypes.DOUBLE) { |
354 |
value = ogrFeature.GetFieldAsDouble(i); |
355 |
} else {
|
356 |
value = ogrFeature.GetFieldAsString(i); |
357 |
} |
358 |
|
359 |
featureProvider.set(fieldDefn.GetName(), value); |
360 |
} |
361 |
} |
362 |
return featureProvider;
|
363 |
} |
364 |
|
365 |
/**
|
366 |
* Converts {@link FeatureProvider} to {@link Feature}.
|
367 |
*
|
368 |
* @param featureProvider
|
369 |
* Feature provider to be converted
|
370 |
* @return Feature OGR Feature with feature provider values.
|
371 |
* @throws GeometryOperationNotSupportedException
|
372 |
* If operation used to convert geometries is not supported
|
373 |
* @throws GeometryOperationException
|
374 |
* If there are some problems converting geometries
|
375 |
*/
|
376 |
public Feature convert(FeatureProvider featureProvider)
|
377 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
378 |
|
379 |
if (featureProvider == null) { |
380 |
return null; |
381 |
} |
382 |
|
383 |
FeatureType featureType = featureProvider.getType(); |
384 |
FeatureDefn featureDefn = new FeatureDefn();
|
385 |
List<FieldDefn> fields = this.convertFields(featureType); |
386 |
for (FieldDefn fieldDefn : fields) {
|
387 |
featureDefn.AddFieldDefn(fieldDefn); |
388 |
} |
389 |
|
390 |
List<GeomFieldDefn> geometryFields = this.convertGeometryFields(featureType, true); |
391 |
for (GeomFieldDefn geomFieldDefn : geometryFields) {
|
392 |
featureDefn.AddGeomFieldDefn(geomFieldDefn); |
393 |
} |
394 |
|
395 |
Feature feature = new Feature(featureDefn);
|
396 |
|
397 |
Integer oid = null; |
398 |
if (featureProvider.getOID() instanceof Integer) { |
399 |
oid = (Integer) featureProvider.getOID();
|
400 |
} else if (featureProvider.getOID() instanceof Long) { |
401 |
oid = ((Long) featureProvider.getOID()).intValue();
|
402 |
} |
403 |
|
404 |
feature.SetFID(oid); |
405 |
FeatureAttributeDescriptor[] attributeDescriptors = featureType.getAttributeDescriptors();
|
406 |
for (int i = 0; i < attributeDescriptors.length; i++) { |
407 |
|
408 |
Integer fieldIndex = null; |
409 |
if (attributeDescriptors[i].getType() == DataTypes.GEOMETRY
|
410 |
&& attributeDescriptors[i].getName().equals(DEFAULT_GEOMETRY_COLUMN_NAME)) { |
411 |
fieldIndex = featureDefn.GetGeomFieldIndex("");
|
412 |
} else if (attributeDescriptors[i].getType() == DataTypes.GEOMETRY) { |
413 |
fieldIndex = featureDefn.GetGeomFieldIndex(attributeDescriptors[i].getName()); |
414 |
} else {
|
415 |
fieldIndex = featureDefn.GetFieldIndex(attributeDescriptors[i].getName()); |
416 |
} |
417 |
|
418 |
Object value = featureProvider.get(i);
|
419 |
|
420 |
if (value == null) { |
421 |
continue;
|
422 |
} |
423 |
|
424 |
if (attributeDescriptors[i].getType() == DataTypes.STRING) {
|
425 |
feature.SetField(fieldIndex, (String) value);
|
426 |
} else if (attributeDescriptors[i].getType() == DataTypes.DOUBLE) { |
427 |
feature.SetField(fieldIndex, (Double) value);
|
428 |
} else if (attributeDescriptors[i].getType() == DataTypes.BOOLEAN) { |
429 |
feature.SetField(fieldIndex, ((Boolean) value).toString());
|
430 |
} else if (attributeDescriptors[i].getType() == DataTypes.DATE) { |
431 |
Date date = (Date) value; |
432 |
Calendar calendar = Calendar.getInstance(); |
433 |
calendar.setTime(date); |
434 |
feature.SetField(i, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), |
435 |
calendar.get(Calendar.DAY_OF_WEEK), calendar.get(Calendar.HOUR), |
436 |
calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), 0); |
437 |
} else if (attributeDescriptors[i].getType() == DataTypes.GEOMETRY) { |
438 |
org.gvsig.fmap.geom.Geometry geometry = (Geometry) value; |
439 |
if (geometry != null) { |
440 |
org.gdal.ogr.Geometry ogrGeometry = null;
|
441 |
if (attributeDescriptors[i].getSRS() != null) { |
442 |
IProjection projection = attributeDescriptors[i].getSRS(); |
443 |
SpatialReference srs = |
444 |
new SpatialReference(projection.export(ICRSFactory.FORMAT_WKT));
|
445 |
ogrGeometry = ogr.CreateGeometryFromWkt(geometry.convertToWKT(), srs); |
446 |
feature.SetGeomField(i, ogrGeometry); |
447 |
} else {
|
448 |
ogrGeometry = ogr.CreateGeometryFromWkt(geometry.convertToWKT()); |
449 |
feature.SetGeomField(fieldIndex, ogrGeometry); |
450 |
} |
451 |
} |
452 |
} |
453 |
} |
454 |
|
455 |
return feature;
|
456 |
} |
457 |
|
458 |
/**
|
459 |
* Convert gvSIG geometry type to OGR geometry type. See
|
460 |
* {@link ogrConstants}.
|
461 |
*
|
462 |
* @param type
|
463 |
* A type of {@link TYPES}
|
464 |
* @return int Equivalent type of {@link ogrConstants}
|
465 |
*/
|
466 |
public int convertToOGRGeomType(int type) { |
467 |
|
468 |
switch (type) {
|
469 |
case TYPES.POINT:
|
470 |
return ogrConstants.wkbPoint;
|
471 |
|
472 |
case TYPES.CURVE:
|
473 |
case TYPES.LINE:
|
474 |
case TYPES.CIRCLE:
|
475 |
case TYPES.ARC:
|
476 |
case TYPES.ELLIPSE:
|
477 |
case TYPES.SPLINE:
|
478 |
case TYPES.ELLIPTICARC:
|
479 |
case TYPES.CIRCUMFERENCE:
|
480 |
case TYPES.PERIELLIPSE:
|
481 |
return ogrConstants.wkbLineString;
|
482 |
|
483 |
case TYPES.SURFACE:
|
484 |
case TYPES.POLYGON:
|
485 |
case TYPES.FILLEDSPLINE:
|
486 |
return ogrConstants.wkbPolygon;
|
487 |
|
488 |
case TYPES.AGGREGATE:
|
489 |
return ogrConstants.wkbGeometryCollection;
|
490 |
|
491 |
case TYPES.MULTIPOINT:
|
492 |
return ogrConstants.wkbMultiPoint;
|
493 |
|
494 |
case TYPES.MULTICURVE:
|
495 |
case TYPES.MULTILINE:
|
496 |
return ogrConstants.wkbMultiLineString;
|
497 |
|
498 |
case TYPES.MULTISURFACE:
|
499 |
case TYPES.MULTIPOLYGON:
|
500 |
return ogrConstants.wkbMultiPolygon;
|
501 |
|
502 |
case TYPES.NULL:
|
503 |
return ogrConstants.wkbNone;
|
504 |
|
505 |
case TYPES.GEOMETRY:
|
506 |
case TYPES.MULTISOLID:
|
507 |
case TYPES.SOLID:
|
508 |
case TYPES.COMPLEX:
|
509 |
default:
|
510 |
return ogrConstants.wkbUnknown;
|
511 |
} |
512 |
} |
513 |
|
514 |
/**
|
515 |
* Convert OGR geometry type to gvSIG geometry type. See {@link TYPES}.
|
516 |
*
|
517 |
* @param type
|
518 |
* A type of {@link ogrConstants}
|
519 |
* @return int Equivalent type of {@link TYPES}
|
520 |
*/
|
521 |
public int convertToGeomType(int type) { |
522 |
|
523 |
switch (type) {
|
524 |
case ogrConstants.wkbPoint:
|
525 |
return TYPES.POINT;
|
526 |
|
527 |
case ogrConstants.wkbLineString:
|
528 |
return TYPES.CURVE;
|
529 |
|
530 |
case ogrConstants.wkbPolygon:
|
531 |
return TYPES.SURFACE;
|
532 |
|
533 |
case ogrConstants.wkbGeometryCollection:
|
534 |
return TYPES.AGGREGATE;
|
535 |
|
536 |
case ogrConstants.wkbMultiPoint:
|
537 |
return TYPES.MULTIPOINT;
|
538 |
|
539 |
case ogrConstants.wkbMultiLineString:
|
540 |
return TYPES.MULTILINE;
|
541 |
|
542 |
case ogrConstants.wkbMultiPolygon:
|
543 |
return TYPES.MULTISURFACE;
|
544 |
|
545 |
case ogrConstants.wkbNone:
|
546 |
return TYPES.NULL;
|
547 |
|
548 |
case ogrConstants.wkbUnknown:
|
549 |
default:
|
550 |
return TYPES.GEOMETRY;
|
551 |
} |
552 |
} |
553 |
|
554 |
/**
|
555 |
* Convert gvSIG type to OGR type. See {@link ogrConstants}.
|
556 |
*
|
557 |
* @param type
|
558 |
* A type of {@link DataTypes}
|
559 |
* @return int Equivalent type of {@link ogrConstants}
|
560 |
*/
|
561 |
public int convertToOGRType(int type) { |
562 |
|
563 |
switch (type) {
|
564 |
case DataTypes.DATE:
|
565 |
return ogrConstants.OFTDate;
|
566 |
case DataTypes.DOUBLE:
|
567 |
case DataTypes.FLOAT:
|
568 |
return ogrConstants.OFTReal;
|
569 |
case DataTypes.STRING:
|
570 |
return ogrConstants.OFTString;
|
571 |
case DataTypes.CHAR:
|
572 |
return ogrConstants.OFTString;
|
573 |
case DataTypes.INT:
|
574 |
return ogrConstants.OFTInteger;
|
575 |
case DataTypes.LONG: // There is not LONG type support |
576 |
default:
|
577 |
return ogrConstants.OFTString;
|
578 |
} |
579 |
} |
580 |
|
581 |
/**
|
582 |
* Convert OGR type to gvSIG type. See {@link DataTypes}.
|
583 |
*
|
584 |
* @param type
|
585 |
* A type of {@link ogrConstants}
|
586 |
* @return int Equivalent type of {@link DataTypes}
|
587 |
*/
|
588 |
public int convertToDataType(int type) { |
589 |
|
590 |
switch (type) {
|
591 |
case ogrConstants.OFTDate:
|
592 |
return DataTypes.DATE;
|
593 |
case ogrConstants.OFTReal:
|
594 |
return DataTypes.DOUBLE;
|
595 |
case ogrConstants.OFTInteger:
|
596 |
return DataTypes.INT;
|
597 |
case ogrConstants.OFTTime:
|
598 |
return DataTypes.TIME;
|
599 |
case ogrConstants.OFTString:
|
600 |
default:
|
601 |
return DataTypes.STRING;
|
602 |
} |
603 |
|
604 |
} |
605 |
|
606 |
/**
|
607 |
* Convert OGR Feature to another OGR Feature. This method take into account
|
608 |
* only {@link FeatureDefn} fields to create new feature. This method is
|
609 |
* useful when OGR {@link Layer} is copied to new {@link DataSource}.
|
610 |
*
|
611 |
* @param layerDefn
|
612 |
* Definition of new feature
|
613 |
* @param feature
|
614 |
* OGR feature to be converted
|
615 |
* @return Fixed feature
|
616 |
*/
|
617 |
public org.gdal.ogr.Feature convert(FeatureDefn layerDefn, org.gdal.ogr.Feature feature) {
|
618 |
|
619 |
Feature newFeature = new Feature(layerDefn);
|
620 |
for (int i = 0; i < feature.GetDefnRef().GetFieldCount(); i++) { |
621 |
|
622 |
FieldDefn fieldDefn = feature.GetDefnRef().GetFieldDefn(i); |
623 |
int fieldIndex = newFeature.GetDefnRef().GetFieldIndex(fieldDefn.GetName());
|
624 |
|
625 |
if (fieldIndex != -1) { |
626 |
|
627 |
int fieldType = feature.GetDefnRef().GetFieldDefn(i).GetFieldType();
|
628 |
|
629 |
switch (fieldType) {
|
630 |
case ogrConstants.OFTDate:
|
631 |
|
632 |
int[] year = new int[1]; |
633 |
int[] month = new int[1]; |
634 |
int[] day = new int[1]; |
635 |
int[] hour = new int[1]; |
636 |
int[] minute = new int[1]; |
637 |
int[] second = new int[1]; |
638 |
int[] flags = new int[1]; |
639 |
feature.GetFieldAsDateTime(i, year, month, day, hour, minute, second, flags); |
640 |
newFeature.SetField(i, year[0], month[0], day[0], hour[0], minute[0], |
641 |
second[0], flags[0]); |
642 |
break;
|
643 |
case ogrConstants.OFTReal:
|
644 |
newFeature.SetField(i, feature.GetFieldAsDouble(i)); |
645 |
break;
|
646 |
case ogrConstants.OFTInteger:
|
647 |
newFeature.SetField(i, feature.GetFieldAsInteger(i)); |
648 |
break;
|
649 |
case ogrConstants.OFTString:
|
650 |
default:
|
651 |
newFeature.SetField(i, feature.GetFieldAsString(i)); |
652 |
break;
|
653 |
} |
654 |
} |
655 |
|
656 |
} |
657 |
|
658 |
for (int i = 0; i < feature.GetDefnRef().GetGeomFieldCount(); i++) { |
659 |
GeomFieldDefn geomFieldDefn = feature.GetDefnRef().GetGeomFieldDefn(i); |
660 |
int fieldIndex = newFeature.GetDefnRef().GetGeomFieldIndex(geomFieldDefn.GetName());
|
661 |
|
662 |
if (fieldIndex != -1) { |
663 |
|
664 |
if (geomFieldDefn.GetFieldType() == newFeature.GetGeomFieldDefnRef(fieldIndex)
|
665 |
.GetFieldType()) { |
666 |
org.gdal.ogr.Geometry geometry = |
667 |
feature.GetGeomFieldRef(geomFieldDefn.GetName()); |
668 |
newFeature.SetGeomField(fieldIndex, geometry); |
669 |
} else {
|
670 |
LOG.warn( |
671 |
"Ignoring geometry field. Can not fix {}. Geometry type is not equals",
|
672 |
geomFieldDefn.GetName()); |
673 |
} |
674 |
} |
675 |
} |
676 |
|
677 |
return newFeature;
|
678 |
} |
679 |
} |