Statistics
| Revision:

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.csv / src / main / java / org / gvsig / fmap / dal / store / simplereader / SimpleReaderPointAttributeEmulator.java @ 47638

History | View | Annotate | Download (5.29 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.store.simplereader;
7

    
8
import javax.json.JsonArray;
9
import javax.json.JsonObject;
10
import org.gvsig.fmap.dal.DataTypes;
11
import org.gvsig.fmap.dal.feature.EditableFeature;
12
import org.gvsig.fmap.dal.feature.Feature;
13
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
14
import org.gvsig.fmap.geom.Geometry;
15
import org.gvsig.fmap.geom.GeometryLocator;
16
import org.gvsig.fmap.geom.GeometryManager;
17
import org.gvsig.fmap.geom.aggregate.MultiPoint;
18
import org.gvsig.fmap.geom.primitive.Point;
19
import org.gvsig.json.Json;
20
import org.gvsig.json.JsonObjectBuilder;
21
import org.gvsig.tools.ToolsLocator;
22
import org.gvsig.tools.dataTypes.Coercion;
23
import org.gvsig.tools.dataTypes.DataType;
24
import org.gvsig.tools.dataTypes.DataTypesManager;
25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27

    
28
/**
29
 *
30
 * @author jjdelcerro
31
 */
32
public class SimpleReaderPointAttributeEmulator implements FeatureAttributeEmulator {
33
    
34
    private static final Logger logger = LoggerFactory.getLogger(SimpleReaderPointAttributeEmulator.class);
35
    private static final int XNAME = 0;
36
    private static final int YNAME = 1;
37
    private static final int ZNAME = 2;
38
    private GeometryManager geommgr;
39
    private String[] fieldNames;
40
    private Coercion toDouble;
41
    private DataType dataType;
42
    private int errorcount = 0;
43

    
44
    public SimpleReaderPointAttributeEmulator() {
45
    }
46

    
47
    public SimpleReaderPointAttributeEmulator(String[] pointDimensionNames) {
48
        if (pointDimensionNames.length > 2) {
49
            this.fieldNames = new String[3];
50
            this.fieldNames[ZNAME] = pointDimensionNames[2];
51
        } else {
52
            this.fieldNames = new String[2];
53
        }
54
        this.fieldNames[XNAME] = pointDimensionNames[0];
55
        this.fieldNames[YNAME] = pointDimensionNames[1];
56
        this.geommgr = GeometryLocator.getGeometryManager();
57
        DataTypesManager datatypeManager = ToolsLocator.getDataTypesManager();
58
        this.toDouble = datatypeManager.getCoercion(DataTypes.DOUBLE);
59
        this.dataType = datatypeManager.get(DataTypes.GEOMETRY);
60
    }
61

    
62
    public String[] getFieldNames() {
63
        return fieldNames;
64
    }
65
    
66
    @Override
67
    public Object get(Feature feature) {
68
        try {
69
            Object valueX = feature.get(this.fieldNames[XNAME]);
70
            valueX = toDouble.coerce(valueX);
71
            if (valueX == null) {
72
                return null;
73
            }
74
            Object valueY = feature.get(this.fieldNames[YNAME]);
75
            valueY = toDouble.coerce(valueY);
76
            if (valueY == null) {
77
                return null;
78
            }
79
            Object valueZ = null;
80
            if (this.fieldNames.length > 2) {
81
                valueZ = toDouble.coerce(feature.get(this.fieldNames[ZNAME]));
82
                if (valueZ == null) {
83
                    return null;
84
                }
85
            }
86
            double x = (Double) valueX;
87
            double y = (Double) valueY;
88
            Point point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM3D);
89
            if (this.fieldNames.length > 2) {
90
                double z = (Double) valueZ;
91
                point.setCoordinateAt(2, z);
92
            }
93
            return point;
94
        } catch (Exception ex) {
95
            if (++errorcount < 5) {
96
                logger.warn("[" + errorcount + "] Can't create point in CSV provider. XNAME='" + this.fieldNames[XNAME] + "', YNAME='" + this.fieldNames[XNAME] + "' feature=" + feature.toString(), ex);
97
            }
98
            return null;
99
        }
100
    }
101

    
102
    @Override
103
    public void set(EditableFeature feature, Object value) {
104
        if (value == null) {
105
            return;
106
        }
107
        Point point;
108
        if (value instanceof MultiPoint) {
109
            point = (Point) ((MultiPoint) value).getPrimitiveAt(0);
110
        } else {
111
            point = (Point) value;
112
        }
113
        feature.set(this.fieldNames[XNAME], point.getX());
114
        feature.set(this.fieldNames[YNAME], point.getY());
115
        if (this.fieldNames.length > 2) {
116
            feature.set(this.fieldNames[ZNAME], point.getCoordinateAt(2));
117
        }
118
    }
119

    
120
    @Override
121
    public boolean allowSetting() {
122
        return true;
123
    }
124

    
125
    @Override
126
    public String[] getRequiredFieldNames() {
127
        return this.fieldNames;
128
    }
129

    
130
    @Override
131
    public JsonObject toJson() {
132
        return this.toJsonBuilder().build();
133
    }
134

    
135
    @Override
136
    public JsonObjectBuilder toJsonBuilder() {
137
        JsonObjectBuilder builder = Json.createObjectBuilder();
138
        builder.add_class(this);
139
        builder.add("fieldNames", fieldNames);
140
        return builder;
141
    }
142

    
143
    @Override
144
    public void fromJson(JsonObject json) {
145
        JsonArray jsonFieldNames = json.getJsonArray("fieldNames");
146
        this.fieldNames = new String[jsonFieldNames.size()];
147
        for (int i = 0; i < jsonFieldNames.size(); i++) {
148
            this.fieldNames[XNAME] = jsonFieldNames.getString(i);
149
        }
150
        this.geommgr = GeometryLocator.getGeometryManager();
151
        DataTypesManager datatypeManager = ToolsLocator.getDataTypesManager();
152
        this.toDouble = datatypeManager.getCoercion(DataTypes.DOUBLE);
153
        this.dataType = datatypeManager.get(DataTypes.GEOMETRY);
154
    }
155
    
156
}