forked from cocoahero/android-geojson
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Feature.java
178 lines (142 loc) · 4.48 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
package com.cocoahero.android.geojson;
import android.os.Parcel;
import android.os.Parcelable;
import com.cocoahero.android.geojson.util.JSONUtils;
import org.json.JSONException;
import org.json.JSONObject;
public class Feature extends GeoJSONObject {
// ------------------------------------------------------------------------
// Private Constants
// ------------------------------------------------------------------------
private static final String JSON_ID = "id";
private static final String JSON_GEOMETRY = "geometry";
private static final String JSON_PROPERTIES = "properties";
// ------------------------------------------------------------------------
// Instance Variables
// ------------------------------------------------------------------------
private String mIdentifier;
private Geometry mGeometry;
private JSONObject mProperties;
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
/**
* Creates an empty Feature without a geometry.
*/
public Feature() {
// Default Constructor
}
/**
* Parses the given {@link JSONObject} as a GeoJSON Feature.
*
* @param json
*/
public Feature(JSONObject json) {
super(json);
this.mIdentifier = JSONUtils.optString(json, JSON_ID);
JSONObject geometry = json.optJSONObject(JSON_GEOMETRY);
if (geometry != null) {
this.mGeometry = (Geometry) GeoJSON.parse(geometry);
}
this.mProperties = json.optJSONObject(JSON_PROPERTIES);
}
/**
* Creates a new GeoJSON Feature object with the given geometry.
*
* @param geometry
*/
public Feature(Geometry geometry) {
this.mGeometry = geometry;
}
// ------------------------------------------------------------------------
// Parcelable Interface
// ------------------------------------------------------------------------
public static final Parcelable.Creator<Feature> CREATOR = new Creator<Feature>() {
@Override
public Feature createFromParcel(Parcel in) {
return (Feature) readParcel(in);
}
@Override
public Feature[] newArray(int size) {
return new Feature[size];
}
};
// ------------------------------------------------------------------------
// Public Methods
// ------------------------------------------------------------------------
/**
* The optional, common identifier of this feature.
*
* @return The common identifier of this feature, if set.
*/
public String getIdentifier() {
return this.mIdentifier;
}
/**
* Sets the optional, common identifier of this feature.
*
* @param identifier
*/
public void setIdentifier(String identifier) {
this.mIdentifier = identifier;
}
/**
* Returns the geometry of this feature. It will be a concrete instance of
* {@link Geometry}.
*
* @return the geometry of this feature, or null if not set.
*/
public Geometry getGeometry() {
return this.mGeometry;
}
/**
* Sets the {@link Geometry} of this feature.
*
* @param geometry
*/
public void setGeometry(Geometry geometry) {
this.mGeometry = geometry;
}
/**
* Returns the optional properties of this feature as JSON.
*
* @return the properties of this feature
*/
public JSONObject getProperties() {
return this.mProperties;
}
/**
* Sets the properties of this feature.
*
* @param properties
*/
public void setProperties(JSONObject properties) {
this.mProperties = properties;
}
/**
* {@inheritDoc}
*/
@Override
public String getType() {
return GeoJSON.TYPE_FEATURE;
}
/**
* {@inheritDoc}
*/
@Override
public JSONObject toJSON() throws JSONException {
JSONObject json = super.toJSON();
json.put(JSON_ID, this.mIdentifier);
if (this.mGeometry != null) {
json.put(JSON_GEOMETRY, this.mGeometry.toJSON());
} else {
json.put(JSON_GEOMETRY, JSONObject.NULL);
}
if (this.mProperties != null) {
json.put(JSON_PROPERTIES, this.mProperties);
} else {
json.put(JSON_PROPERTIES, JSONObject.NULL);
}
return json;
}
}