add support for geoWithin.centerSphere queries via withJSON (#4825)
* add support for geoWithin.centerSphere queries via withJSON * added test for passing array of lat, lng instead of Parse.GeoPoint * added postgres support * added more tests * improved tests and validation * added more tests
This commit is contained in:
@@ -905,44 +905,70 @@ function transformConstraint(constraint, field) {
|
||||
|
||||
case '$geoWithin': {
|
||||
const polygon = constraint[key]['$polygon'];
|
||||
let points;
|
||||
if (typeof polygon === 'object' && polygon.__type === 'Polygon') {
|
||||
if (!polygon.coordinates || polygon.coordinates.length < 3) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_JSON,
|
||||
'bad $geoWithin value; Polygon.coordinates should contain at least 3 lon/lat pairs'
|
||||
);
|
||||
}
|
||||
points = polygon.coordinates;
|
||||
} else if (polygon instanceof Array) {
|
||||
if (polygon.length < 3) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_JSON,
|
||||
'bad $geoWithin value; $polygon should contain at least 3 GeoPoints'
|
||||
);
|
||||
}
|
||||
points = polygon;
|
||||
} else {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_JSON,
|
||||
'bad $geoWithin value; $polygon should be Polygon object or Array of Parse.GeoPoint\'s'
|
||||
);
|
||||
}
|
||||
points = points.map((point) => {
|
||||
if (point instanceof Array && point.length === 2) {
|
||||
Parse.GeoPoint._validate(point[1], point[0]);
|
||||
return point;
|
||||
}
|
||||
if (!GeoPointCoder.isValidJSON(point)) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value');
|
||||
const centerSphere = constraint[key]['$centerSphere'];
|
||||
if (polygon !== undefined) {
|
||||
let points;
|
||||
if (typeof polygon === 'object' && polygon.__type === 'Polygon') {
|
||||
if (!polygon.coordinates || polygon.coordinates.length < 3) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_JSON,
|
||||
'bad $geoWithin value; Polygon.coordinates should contain at least 3 lon/lat pairs'
|
||||
);
|
||||
}
|
||||
points = polygon.coordinates;
|
||||
} else if (polygon instanceof Array) {
|
||||
if (polygon.length < 3) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_JSON,
|
||||
'bad $geoWithin value; $polygon should contain at least 3 GeoPoints'
|
||||
);
|
||||
}
|
||||
points = polygon;
|
||||
} else {
|
||||
Parse.GeoPoint._validate(point.latitude, point.longitude);
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_JSON,
|
||||
'bad $geoWithin value; $polygon should be Polygon object or Array of Parse.GeoPoint\'s'
|
||||
);
|
||||
}
|
||||
return [point.longitude, point.latitude];
|
||||
});
|
||||
answer[key] = {
|
||||
'$polygon': points
|
||||
};
|
||||
points = points.map((point) => {
|
||||
if (point instanceof Array && point.length === 2) {
|
||||
Parse.GeoPoint._validate(point[1], point[0]);
|
||||
return point;
|
||||
}
|
||||
if (!GeoPointCoder.isValidJSON(point)) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value');
|
||||
} else {
|
||||
Parse.GeoPoint._validate(point.latitude, point.longitude);
|
||||
}
|
||||
return [point.longitude, point.latitude];
|
||||
});
|
||||
answer[key] = {
|
||||
'$polygon': points
|
||||
};
|
||||
} else if (centerSphere !== undefined) {
|
||||
if (!(centerSphere instanceof Array) || centerSphere.length < 2) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value; $centerSphere should be an array of Parse.GeoPoint and distance');
|
||||
}
|
||||
// Get point, convert to geo point if necessary and validate
|
||||
let point = centerSphere[0];
|
||||
if (point instanceof Array && point.length === 2) {
|
||||
point = new Parse.GeoPoint(point[1], point[0]);
|
||||
} else if (!GeoPointCoder.isValidJSON(point)) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value; $centerSphere geo point invalid');
|
||||
}
|
||||
Parse.GeoPoint._validate(point.latitude, point.longitude);
|
||||
// Get distance and validate
|
||||
const distance = centerSphere[1];
|
||||
if(isNaN(distance) || distance < 0) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value; $centerSphere distance invalid');
|
||||
}
|
||||
answer[key] = {
|
||||
'$centerSphere': [
|
||||
[point.longitude, point.latitude],
|
||||
distance
|
||||
]
|
||||
};
|
||||
}
|
||||
break;
|
||||
}
|
||||
case '$geoIntersects': {
|
||||
|
||||
@@ -535,6 +535,30 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => {
|
||||
index += 2;
|
||||
}
|
||||
|
||||
if (fieldValue.$geoWithin && fieldValue.$geoWithin.$centerSphere) {
|
||||
const centerSphere = fieldValue.$geoWithin.$centerSphere;
|
||||
if (!(centerSphere instanceof Array) || centerSphere.length < 2) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value; $centerSphere should be an array of Parse.GeoPoint and distance');
|
||||
}
|
||||
// Get point, convert to geo point if necessary and validate
|
||||
let point = centerSphere[0];
|
||||
if (point instanceof Array && point.length === 2) {
|
||||
point = new Parse.GeoPoint(point[1], point[0]);
|
||||
} else if (!GeoPointCoder.isValidJSON(point)) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value; $centerSphere geo point invalid');
|
||||
}
|
||||
Parse.GeoPoint._validate(point.latitude, point.longitude);
|
||||
// Get distance and validate
|
||||
const distance = centerSphere[1];
|
||||
if(isNaN(distance) || distance < 0) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value; $centerSphere distance invalid');
|
||||
}
|
||||
const distanceInKM = distance * 6371 * 1000;
|
||||
patterns.push(`ST_distance_sphere($${index}:name::geometry, POINT($${index + 1}, $${index + 2})::geometry) <= $${index + 3}`);
|
||||
values.push(fieldName, point.longitude, point.latitude, distanceInKM);
|
||||
index += 4;
|
||||
}
|
||||
|
||||
if (fieldValue.$geoWithin && fieldValue.$geoWithin.$polygon) {
|
||||
const polygon = fieldValue.$geoWithin.$polygon;
|
||||
let points;
|
||||
@@ -1986,4 +2010,13 @@ function literalizeRegexPart(s: string) {
|
||||
);
|
||||
}
|
||||
|
||||
var GeoPointCoder = {
|
||||
isValidJSON(value) {
|
||||
return (typeof value === 'object' &&
|
||||
value !== null &&
|
||||
value.__type === 'GeoPoint'
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default PostgresStorageAdapter;
|
||||
|
||||
Reference in New Issue
Block a user