-
Notifications
You must be signed in to change notification settings - Fork 104
/
Feature.java
367 lines (331 loc) · 11.8 KB
/
Feature.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* Copyright 2014-2017 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of fiware-cygnus (FIWARE project).
*
* fiware-cygnus is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
* General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
* fiware-cygnus is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License along with fiware-cygnus. If not, see
* http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License please contact with iot_support at tid dot es
*/
package com.telefonica.iot.cygnus.backends.arcgis.model;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.telefonica.iot.cygnus.backends.arcgis.exceptions.ArcgisException;
import com.telefonica.iot.cygnus.log.CygnusLogger;
/**
* @author dmartinez
*
*/
public class Feature {
private static final CygnusLogger LOGGER = new CygnusLogger(Feature.class);
private static final String GREGORIAN_CALENDAR_CLASSNAME = "GregorianCalendar";
private static final String DATE_CLASSNAME = "Date";
private static final String BOOLEAN_CLASSNAME = "Boolean";
private static final String STRING_CLASSNAME = "String";
private static final String DOUBLE_CLASSNAME = "Double";
private static final String FLOAT_CLASSNAME = "Float";
private static final String LONG_CLASSNAME = "Long";
private static final String INTEGER_CLASSNAME = "Integer";
private static final String ATTRIBUTES_TAG = "attributes";
private static final String GEOMETRY_TAG = "geometry";
private static final String DATE_PATTERN = "MM/dd/yyyy hh:mm:ss";
private static final String OBJECTID_FIELDNAME = "OBJECTID";
private Geometry geometry;
private Map<String, Object> attributes;
/**
*
*/
public Feature() {
attributes = new HashMap<String, Object>();
geometry = null;
}
/**
*
* @param geometry
*/
public Feature(Geometry geometry) {
this();
this.geometry = geometry;
}
/**
*
* @param geometry
* @param attributes
*/
public Feature(Geometry geometry, Map<String, Object> attributes) {
this.geometry = geometry;
this.attributes = attributes;
}
/**
*
* @param attName
* @param attValue
*/
public void addAttribute(String attName, Object attValue) {
attributes.put(attName, attValue);
}
/**
* @return the geometry
*/
public Geometry getGeometry() {
return geometry;
}
/**
* @param geometry
* the geometry to set
*/
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}
/**
* @return the attributes
*/
public Map<String, Object> getAttributes() {
return attributes;
}
/**
* @param attributes
* the attributes to set
*/
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
/**
*
* @param latitud
* @param longitud
* @return
*/
public static Feature createPointFeature(double latitud, double longitud) {
return new Feature(new Point(longitud, latitud, SpatialReference.WGS84));
}
/**
*
* @param latitud
* @param longitud
* @return
*/
public static Feature createPointFeature(String latitud, String longitud) {
double lat;
double lon;
try {
lat = Double.parseDouble(latitud);
lon = Double.parseDouble(longitud);
} catch (Exception e) {
lat = new Double(0);
lon = new Double(0);
}
return createPointFeature(lat, lon);
}
/**
* This method merges unexistent attributes from sourceFeature.
*
* @param sourceFeature
*/
public void completeAttributesFrom(Feature sourceFeature) {
Map<String, Object> sourceAttributes = sourceFeature.getAttributes();
Set<String> sourceKeyset = sourceAttributes.keySet();
for (String key : sourceKeyset) {
if (!this.attributes.containsKey(key)) {
this.attributes.put(key, sourceAttributes.get(key));
}
}
}
/**
*
*/
@Override
public String toString() {
return toJson().toString();
}
private void addProperty(JsonObject jsonObj, String name, Object property) {
SimpleDateFormat simpleDateFormat = null;
if (property != null) {
switch (property.getClass().getSimpleName()) {
case INTEGER_CLASSNAME:
jsonObj.addProperty(name, (Integer) property);
break;
case LONG_CLASSNAME:
jsonObj.addProperty(name, (Long) property);
break;
case FLOAT_CLASSNAME:
jsonObj.addProperty(name, (Float) property);
break;
case DOUBLE_CLASSNAME:
jsonObj.addProperty(name, (Double) property);
break;
case STRING_CLASSNAME:
jsonObj.addProperty(name, (String) property);
break;
case BOOLEAN_CLASSNAME:
jsonObj.addProperty(name, (Boolean) property);
break;
case DATE_CLASSNAME:
simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);
jsonObj.addProperty(name, simpleDateFormat.format((Date) property));
break;
case GREGORIAN_CALENDAR_CLASSNAME:
simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);
GregorianCalendar calendar = (GregorianCalendar) property;
jsonObj.addProperty(name, simpleDateFormat.format(calendar.getTime()));
break;
default:
jsonObj.addProperty(name, property.toString());
}
} else {
jsonObj.add(name, null);
}
}
/**
* Retorna el OBJECTID del GIS de la entidad.
*
* @return OBJECTID value
* @throws ArcgisException
*/
public Integer getObjectId() throws ArcgisException {
Integer objectId = -1;
for (Map.Entry<String, Object> attribute : attributes.entrySet()) {
if (OBJECTID_FIELDNAME.equals(attribute.getKey())) {
objectId = (Integer) attribute.getValue();
break;
}
}
if ("".equals(objectId)) {
throw new ArcgisException("Cant find " + GisAttributeType.OID + " in Feature Object.");
} else {
return objectId;
}
}
/**
* Establece el OBJECTID del GIS de la entidad.
*
* @param objectId
* @return
* @throws ArcgisException
*/
public void setObjectId(Integer objectId) throws ArcgisException {
try {
boolean found = false;
for (Map.Entry<String, Object> attribute : attributes.entrySet()) {
if (OBJECTID_FIELDNAME.equals(attribute.getKey())) {
found = true;
attribute.setValue(objectId);
break;
}
}
if (!found) {
attributes.put(OBJECTID_FIELDNAME, objectId);
}
} catch (Exception e) {
throw new ArcgisException(
"Error setting OBJECTID for feature " + this.toString() + " - Error: " + e);
}
}
/**
*
* @return
*/
public JsonObject toJson() {
JsonObject resultJSON = new JsonObject();
resultJSON.add(GEOMETRY_TAG, this.getGeometry().toJSON());
JsonObject attributes = new JsonObject();
for (Map.Entry<String, Object> attribute : this.attributes.entrySet()) {
addProperty(attributes, attribute.getKey(), attribute.getValue());
}
resultJSON.add(ATTRIBUTES_TAG, attributes);
return resultJSON;
}
/**
*
* @param json
* @return
* @throws ArcgisException
*/
public static Feature createInstanceFromJson(String json) throws ArcgisException {
JsonParser parser = new JsonParser();
JsonObject jsonObj = parser.parse(json).getAsJsonObject();
return createInstanceFromJson(jsonObj);
}
/**
*
* @param json
* @return
* @throws ArcgisException
*/
public static Feature createInstanceFromJson(JsonObject json) throws ArcgisException {
try {
Geometry geometry;
if (json.has(GEOMETRY_TAG)) {
JsonObject jsonGeometry = json.get(GEOMETRY_TAG).getAsJsonObject();
geometry = Point.createInstanceFromJson(jsonGeometry); // TODO another
//geometry types?
} else {
geometry = new Point(0, 0);
}
Map<String, Object> attributes = attToMap(json.get(ATTRIBUTES_TAG).getAsJsonObject());
return new Feature(geometry, attributes);
} catch (Exception e) {
throw new ArcgisException("Can't cast Feature from Json, "
+ e.getClass().getSimpleName() + " - " + e.getMessage());
}
}
/**
*
* @param attributes
* @return
* @throws ArcgisException
*/
protected static Map<String, Object> attToMap(JsonObject attributes) throws ArcgisException {
Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
if (attributes.isJsonObject()) {
for (Entry<String, JsonElement> attribute : attributes.entrySet()) {
JsonElement attValue = attribute.getValue();
if (attValue.isJsonPrimitive()) {
try {
int intValue = attValue.getAsInt();
if (attValue.getAsString().equals(Integer.toString(intValue))) {
resultMap.put(attribute.getKey(), attValue.getAsInt());
} else {
throw new Exception("It isn't an integer, maybe a long");
}
} catch (Exception e2) {
try {
resultMap.put(attribute.getKey(), attValue.getAsLong());
} catch (Exception e1) {
try {
resultMap.put(attribute.getKey(), attValue.getAsDouble());
} catch (Exception e3) {
resultMap.put(attribute.getKey(), attValue.getAsString());
}
}
}
} else if (attValue.isJsonNull()) {
resultMap.put(attribute.getKey(), null);
} else {
resultMap.put(attribute.getKey(), attValue.getAsString());
}
}
} else {
LOGGER.error("Cant parse attributes, JsonObject expected.");
throw new ArcgisException("Cant parse attributes, JsonObject expected.");
}
return resultMap;
}
}