forked from cocoahero/android-geojson
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Polygon.java
112 lines (86 loc) · 2.83 KB
/
Polygon.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
package com.cocoahero.android.geojson;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Parcel;
import android.os.Parcelable;
public class Polygon extends Geometry {
// ------------------------------------------------------------------------
// Instance Variables
// ------------------------------------------------------------------------
private final List<Ring> mRings = new ArrayList<Ring>();
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
public Polygon() {
// Default Constructor
}
public Polygon(Ring ring) {
this.addRing(ring);
}
public Polygon(JSONObject json) {
super(json);
this.setRings(json.optJSONArray(JSON_COORDINATES));
}
public Polygon(JSONArray rings) {
this.setRings(rings);
}
// ------------------------------------------------------------------------
// Parcelable Interface
// ------------------------------------------------------------------------
public static final Parcelable.Creator<Polygon> CREATOR = new Creator<Polygon>() {
@Override
public Polygon createFromParcel(Parcel in) {
return (Polygon) readParcel(in);
}
@Override
public Polygon[] newArray(int size) {
return new Polygon[size];
}
};
// ------------------------------------------------------------------------
// Public Methods
// ------------------------------------------------------------------------
public void addRing(Ring ring) {
this.mRings.add(ring);
}
public void removeRing(Ring ring) {
this.mRings.remove(ring);
}
public List<Ring> getRings() {
return this.mRings;
}
public void setRings(JSONArray rings) {
this.mRings.clear();
if (rings != null) {
for (int i = 0; i < rings.length(); i++) {
JSONArray ringJSON = rings.optJSONArray(i);
if (ringJSON != null) {
this.mRings.add(new Ring(ringJSON));
}
}
}
}
public void setRings(List<Ring> rings) {
this.mRings.clear();
if (rings != null) {
this.mRings.addAll(rings);
}
}
@Override
public String getType() {
return GeoJSON.TYPE_POLYGON;
}
@Override
public JSONObject toJSON() throws JSONException {
JSONObject json = super.toJSON();
JSONArray rings = new JSONArray();
for (Ring ring : this.mRings) {
rings.put(ring.toJSON());
}
json.put(JSON_COORDINATES, rings);
return json;
}
}