Skip to content

Commit 351d54b

Browse files
dplewisrogerhu
authored andcommitted
Add Type Polygon to Schema and PolygonContains to query (#696)
* Add Type Polygon to Schema and PolygonContains to query * offlineQuery
1 parent b1fead8 commit 351d54b

16 files changed

+646
-14
lines changed

Parse/src/main/java/com/parse/OfflineQueryLogic.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ private static boolean matchesEqualConstraint(Object constraint, Object value) {
231231
&& lhs.getLongitude() == rhs.getLongitude();
232232
}
233233

234+
if (constraint instanceof ParsePolygon && value instanceof ParsePolygon) {
235+
ParsePolygon lhs = (ParsePolygon) constraint;
236+
ParsePolygon rhs = (ParsePolygon) value;
237+
return lhs.equals(rhs);
238+
}
239+
234240
return compare(constraint, value, new Decider() {
235241
@Override
236242
public boolean decide(Object constraint, Object value) {
@@ -457,6 +463,23 @@ private static boolean matchesWithinConstraint(Object constraint, Object value)
457463
&& target.getLongitude() <= northeast.getLongitude());
458464
}
459465

466+
/**
467+
* Matches $geoIntersects constraints.
468+
*/
469+
private static boolean matchesGeoIntersectsConstraint(Object constraint, Object value)
470+
throws ParseException {
471+
if (value == null || value == JSONObject.NULL) {
472+
return false;
473+
}
474+
475+
@SuppressWarnings("unchecked")
476+
HashMap<String, ParseGeoPoint> constraintMap =
477+
(HashMap<String, ParseGeoPoint>) constraint;
478+
ParseGeoPoint point = constraintMap.get("$point");
479+
ParsePolygon target = (ParsePolygon) value;
480+
return target.containsPoint(point);
481+
}
482+
460483
/**
461484
* Returns true iff the given value matches the given operator and constraint.
462485
*
@@ -512,6 +535,9 @@ private static boolean matchesStatelessConstraint(String operator, Object constr
512535
case "$within":
513536
return matchesWithinConstraint(constraint, value);
514537

538+
case "$geoIntersects":
539+
return matchesGeoIntersectsConstraint(constraint, value);
540+
515541
default:
516542
throw new UnsupportedOperationException(String.format(
517543
"The offline store does not yet support the %s operator.", operator));

Parse/src/main/java/com/parse/ParseDecoder.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,24 @@ protected ParseDecoder() {
5757
}
5858
return outputMap;
5959
}
60-
60+
6161
/**
6262
* Gets the <code>ParseObject</code> another object points to. By default a new
6363
* object will be created.
6464
*/
6565
protected ParseObject decodePointer(String className, String objectId) {
66-
return ParseObject.createWithoutData(className, objectId);
66+
return ParseObject.createWithoutData(className, objectId);
6767
}
6868

6969
public Object decode(Object object) {
7070
if (object instanceof JSONArray) {
7171
return convertJSONArrayToList((JSONArray) object);
7272
}
73-
73+
7474
if (!(object instanceof JSONObject)) {
7575
return object;
7676
}
77-
77+
7878
JSONObject jsonObject = (JSONObject) object;
7979

8080
String opString = jsonObject.optString("__op", null);
@@ -121,6 +121,20 @@ public Object decode(Object object) {
121121
return new ParseGeoPoint(latitude, longitude);
122122
}
123123

124+
if (typeString.equals("Polygon")) {
125+
List<ParseGeoPoint> coordinates = new ArrayList<ParseGeoPoint>();
126+
try {
127+
JSONArray array = jsonObject.getJSONArray("coordinates");
128+
for (int i = 0; i < array.length(); ++i) {
129+
JSONArray point = array.getJSONArray(i);
130+
coordinates.add(new ParseGeoPoint(point.getDouble(0), point.getDouble(1)));
131+
}
132+
} catch (JSONException e) {
133+
throw new RuntimeException(e);
134+
}
135+
return new ParsePolygon(coordinates);
136+
}
137+
124138
if (typeString.equals("Object")) {
125139
return ParseObject.fromJSON(jsonObject, null, this);
126140
}
@@ -132,7 +146,7 @@ public Object decode(Object object) {
132146
if (typeString.equals("OfflineObject")) {
133147
throw new RuntimeException("An unexpected offline pointer was encountered.");
134148
}
135-
149+
136150
return null;
137151
}
138152
}

Parse/src/main/java/com/parse/ParseEncoder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
|| value instanceof ParseACL
4141
|| value instanceof ParseFile
4242
|| value instanceof ParseGeoPoint
43+
|| value instanceof ParsePolygon
4344
|| value instanceof ParseRelation;
4445
}
4546

@@ -84,6 +85,14 @@ public Object encode(Object object) {
8485
return json;
8586
}
8687

88+
if (object instanceof ParsePolygon) {
89+
ParsePolygon polygon = (ParsePolygon) object;
90+
JSONObject json = new JSONObject();
91+
json.put("__type", "Polygon");
92+
json.put("coordinates", polygon.coordinatesToJSONArray());
93+
return json;
94+
}
95+
8796
if (object instanceof ParseACL) {
8897
ParseACL acl = (ParseACL) object;
8998
return acl.toJSONObject(this);

Parse/src/main/java/com/parse/ParseGeoPoint.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public ParseGeoPoint() {
4949

5050
/**
5151
* Creates a new point with the specified latitude and longitude.
52-
*
52+
*
5353
* @param latitude
5454
* The point's latitude.
5555
* @param longitude
@@ -96,7 +96,7 @@ protected ParseGeoPoint(Parcel source) {
9696

9797
/**
9898
* Set latitude. Valid range is (-90.0, 90.0). Extremes should not be used.
99-
*
99+
*
100100
* @param latitude
101101
* The point's latitude.
102102
*/
@@ -116,7 +116,7 @@ public double getLatitude() {
116116

117117
/**
118118
* Set longitude. Valid range is (-180.0, 180.0). Extremes should not be used.
119-
*
119+
*
120120
* @param longitude
121121
* The point's longitude.
122122
*/
@@ -137,7 +137,7 @@ public double getLongitude() {
137137
/**
138138
* Get distance in radians between this point and another {@code ParseGeoPoint}. This is the
139139
* smallest angular distance between the two points.
140-
*
140+
*
141141
* @param point
142142
* {@code ParseGeoPoint} describing the other point being measured against.
143143
*/
@@ -162,7 +162,7 @@ public double distanceInRadiansTo(ParseGeoPoint point) {
162162

163163
/**
164164
* Get distance between this point and another {@code ParseGeoPoint} in kilometers.
165-
*
165+
*
166166
* @param point
167167
* {@code ParseGeoPoint} describing the other point being measured against.
168168
*/
@@ -172,7 +172,7 @@ public double distanceInKilometersTo(ParseGeoPoint point) {
172172

173173
/**
174174
* Get distance between this point and another {@code ParseGeoPoint} in kilometers.
175-
*
175+
*
176176
* @param point
177177
* {@code ParseGeoPoint} describing the other point being measured against.
178178
*/
@@ -274,7 +274,7 @@ public ParseGeoPoint then(Task<Location> task) throws Exception {
274274
* times for a fix.
275275
* * For better battery efficiency and faster location fixes, you can set
276276
* {@link Criteria#setPowerRequirement(int)}, however, this will result in lower accuracy.
277-
*
277+
*
278278
* @param timeout
279279
* The number of milliseconds to allow before timing out.
280280
* @param criteria
@@ -290,6 +290,18 @@ public static void getCurrentLocationInBackground(long timeout, Criteria criteri
290290
ParseTaskUtils.callbackOnMainThreadAsync(getCurrentLocationInBackground(timeout, criteria), callback);
291291
}
292292

293+
@Override
294+
public boolean equals(Object obj) {
295+
if (obj == null || !(obj instanceof ParseGeoPoint)) {
296+
return false;
297+
}
298+
if (obj == this) {
299+
return true;
300+
}
301+
return ((ParseGeoPoint) obj).getLatitude() == latitude &&
302+
((ParseGeoPoint) obj).getLongitude() == longitude;
303+
}
304+
293305
@Override
294306
public String toString() {
295307
return String.format(Locale.US, "ParseGeoPoint[%.6f,%.6f]", latitude, longitude);

Parse/src/main/java/com/parse/ParseObject.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3457,6 +3457,24 @@ public ParseGeoPoint getParseGeoPoint(String key) {
34573457
}
34583458
}
34593459

3460+
/**
3461+
* Access a {@link ParsePolygon} value.
3462+
*
3463+
* @param key
3464+
* The key to access the value for
3465+
* @return {@code null} if there is no such key or if it is not a {@link ParsePolygon}.
3466+
*/
3467+
public ParsePolygon getParsePolygon(String key) {
3468+
synchronized (mutex) {
3469+
checkGetAccess(key);
3470+
Object value = estimatedData.get(key);
3471+
if (!(value instanceof ParsePolygon)) {
3472+
return null;
3473+
}
3474+
return (ParsePolygon) value;
3475+
}
3476+
}
3477+
34603478
/**
34613479
* Access the {@link ParseACL} governing this object.
34623480
*/

Parse/src/main/java/com/parse/ParseParcelDecoder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public Object decode(Parcel source) {
6565
case ParseParcelEncoder.TYPE_GEOPOINT:
6666
return new ParseGeoPoint(source, this);
6767

68+
case ParseParcelEncoder.TYPE_POLYGON:
69+
return new ParsePolygon(source, this);
70+
6871
case ParseParcelEncoder.TYPE_ACL:
6972
return new ParseACL(source, this);
7073

Parse/src/main/java/com/parse/ParseParcelEncoder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ private static boolean isValidType(Object value) {
5555
/* package */ final static String TYPE_OP = "Operation";
5656
/* package */ final static String TYPE_FILE = "File";
5757
/* package */ final static String TYPE_GEOPOINT = "GeoPoint";
58+
/* package */ final static String TYPE_POLYGON = "Polygon";
5859

5960
public void encode(Object object, Parcel dest) {
6061
try {
@@ -84,6 +85,10 @@ public void encode(Object object, Parcel dest) {
8485
dest.writeString(TYPE_GEOPOINT);
8586
((ParseGeoPoint) object).writeToParcel(dest, this);
8687

88+
} else if (object instanceof ParsePolygon) {
89+
dest.writeString(TYPE_POLYGON);
90+
((ParsePolygon) object).writeToParcel(dest, this);
91+
8792
} else if (object instanceof ParseACL) {
8893
dest.writeString(TYPE_ACL);
8994
((ParseACL) object).writeToParcel(dest, this);

0 commit comments

Comments
 (0)