diff options
author | Hari Khalsa <hkhalsa@10gen.com> | 2013-03-29 15:25:55 -0400 |
---|---|---|
committer | Hari Khalsa <hkhalsa@10gen.com> | 2013-06-25 13:46:36 -0400 |
commit | ba239918c950c254056bf589a943a5e88fd4144c (patch) | |
tree | c65dcc83ab6362e7099caa13093d059cdf2c073e /src/mongo/db/geo/geoparser_test.cpp | |
parent | b290b65c9e679a42999fe1b0ef7d466b669bd6c9 (diff) | |
download | mongo-ba239918c950c254056bf589a943a5e88fd4144c.tar.gz |
add multipoint/line/poly. SERVER-8907 SERVER-8349
Diffstat (limited to 'src/mongo/db/geo/geoparser_test.cpp')
-rw-r--r-- | src/mongo/db/geo/geoparser_test.cpp | 284 |
1 files changed, 218 insertions, 66 deletions
diff --git a/src/mongo/db/geo/geoparser_test.cpp b/src/mongo/db/geo/geoparser_test.cpp index cb3af1a119e..bac9bb57cb9 100644 --- a/src/mongo/db/geo/geoparser_test.cpp +++ b/src/mongo/db/geo/geoparser_test.cpp @@ -22,20 +22,13 @@ #include <sstream> #include "mongo/db/geo/geoparser.h" +#include "mongo/db/geo/shapes.h" #include "mongo/db/json.h" #include "mongo/db/jsobj.h" #include "mongo/unittest/unittest.h" #include "mongo/util/assert_util.h" -#include "mongo/db/geo/shapes.h" -#include "third_party/s2/s2.h" -#include "third_party/s2/s2polygon.h" -#include "third_party/s2/s2polyline.h" - -using mongo::BSONObj; -using mongo::fromjson; -using mongo::GeoParser; -using mongo::Polygon; +using namespace mongo; namespace { @@ -63,27 +56,27 @@ namespace { } TEST(GeoParser, isValidLineString) { - ASSERT_TRUE(GeoParser::isLineString( + ASSERT_TRUE(GeoParser::isLine( fromjson("{'type':'LineString', 'coordinates':[[1,2], [3,4]]}"))); - ASSERT_TRUE(GeoParser::isLineString( + ASSERT_TRUE(GeoParser::isLine( fromjson("{'type':'LineString', 'coordinates':[[0,-90], [0,90]]}"))); - ASSERT_TRUE(GeoParser::isLineString( + ASSERT_TRUE(GeoParser::isLine( fromjson("{'type':'LineString', 'coordinates':[[180,-90], [-180,90]]}"))); - ASSERT_FALSE(GeoParser::isLineString( + ASSERT_FALSE(GeoParser::isLine( fromjson("{'type':'LineString', 'coordinates':[[180.1,-90], [-180.1,90]]}"))); - ASSERT_FALSE(GeoParser::isLineString( + ASSERT_FALSE(GeoParser::isLine( fromjson("{'type':'LineString', 'coordinates':[[0,-91], [0,90]]}"))); - ASSERT_FALSE(GeoParser::isLineString( + ASSERT_FALSE(GeoParser::isLine( fromjson("{'type':'LineString', 'coordinates':[[0,-90], [0,91]]}"))); - ASSERT_TRUE(GeoParser::isLineString( + ASSERT_TRUE(GeoParser::isLine( fromjson("{'type':'LineString', 'coordinates':[[1,2], [3,4], [5,6]]}"))); - ASSERT_FALSE(GeoParser::isLineString( + ASSERT_FALSE(GeoParser::isLine( fromjson("{'type':'LineString', 'coordinates':[[1,2]]}"))); - ASSERT_FALSE(GeoParser::isLineString( + ASSERT_FALSE(GeoParser::isLine( fromjson("{'type':'LineString', 'coordinates':[['chicken','little']]}"))); - ASSERT_FALSE(GeoParser::isLineString( + ASSERT_FALSE(GeoParser::isLine( fromjson("{'type':'LineString', 'coordinates':[1,2, 3, 4]}"))); - ASSERT_FALSE(GeoParser::isLineString( + ASSERT_FALSE(GeoParser::isLine( fromjson("{'type':'LineString', 'coordinates':[[1,2, 3], [3,4, 5], [5,6]]}"))); } @@ -111,71 +104,69 @@ namespace { } TEST(GeoParser, parsePoint) { - S2Point point; - ASSERT_TRUE(GeoParser::parsePoint(fromjson("{'type':'Point', 'coordinates': [40, 5]}"), - &point)); - ASSERT_TRUE(GeoParser::parsePoint(fromjson("{'type':'Point', 'coordinates': [-4.3, -5.0]}"), - &point)); + PointWithCRS point; + GeoParser::parsePoint(fromjson("{'type':'Point', 'coordinates': [40, 5]}"), &point); + GeoParser::parsePoint(fromjson("{'type':'Point', 'coordinates': [-4.3, -5.0]}"), &point); } - TEST(GeoParser, parseLineString) { - S2Polyline polyline; - GeoParser::parseLineString( + TEST(GeoParser, parseLine) { + LineWithCRS polyline; + GeoParser::parseLine( fromjson("{'type':'LineString', 'coordinates':[[1,2],[3,4]]}"), &polyline); - GeoParser::parseLineString( + GeoParser::parseLine( fromjson("{'type':'LineString', 'coordinates':[[1,2], [3,4], [5,6]]}"), &polyline); - GeoParser::parseLineString( + GeoParser::parseLine( fromjson("{'type':'LineString', 'coordinates':[[1,2], [3,4], [5,6]]}"), &polyline); } TEST(GeoParser, parsePolygon) { - S2Point point; + PointWithCRS point; GeoParser::parsePoint(fromjson("{'type':'Point', 'coordinates': [2, 2]}"), &point); - S2Polygon polygonA; + PolygonWithCRS polygonA; GeoParser::parsePolygon( fromjson("{'type':'Polygon', 'coordinates':[ [[0,0],[5,0],[5,5],[0,5],[0,0]] ]}"), &polygonA); - ASSERT_TRUE(polygonA.Contains(point)); + ASSERT_TRUE(polygonA.polygon.Contains(point.point)); - S2Polygon polygonB; + PolygonWithCRS polygonB; GeoParser::parsePolygon( fromjson("{'type':'Polygon', 'coordinates':[ [[0,0],[5,0],[5,5],[0,5],[0,0]]," " [[1,1],[1,4],[4,4],[4,1],[1,1]] ]}"), &polygonB); // We removed this in the hole. - ASSERT_FALSE(polygonB.Contains(point)); + ASSERT_FALSE(polygonB.polygon.Contains(point.point)); // Now we reverse the orientations and verify that the code fixes it up // (outer loop must be CCW, inner CW). - S2Polygon polygonC; + PolygonWithCRS polygonC; GeoParser::parsePolygon( fromjson("{'type':'Polygon', 'coordinates':[ [[0,0],[0,5],[5,5],[5,0],[0,0]] ]}"), &polygonC); - ASSERT_TRUE(polygonC.Contains(point)); + ASSERT_TRUE(polygonC.polygon.Contains(point.point)); - S2Polygon polygonD; + PolygonWithCRS polygonD; GeoParser::parsePolygon( fromjson("{'type':'Polygon', 'coordinates':[ [[0,0],[0,5],[5,5],[5,0],[0,0]]," " [[1,1],[1,4],[4,4],[4,1],[1,1]] ]}"), &polygonD); // Also removed in the loop. - ASSERT_FALSE(polygonD.Contains(point)); + ASSERT_FALSE(polygonD.polygon.Contains(point.point)); } - TEST(GeoParser, parseLegacyPoint) { - S2Point point; - ASSERT(GeoParser::parsePoint(BSON_ARRAY(0 << 1), &point)); - ASSERT_FALSE(GeoParser::parsePoint(BSON_ARRAY(0), &point)); - ASSERT_FALSE(GeoParser::parsePoint(BSON_ARRAY(0 << 1 << 2), &point)); - ASSERT(GeoParser::parsePoint(fromjson("{x: 50, y:40}"), &point)); - ASSERT_FALSE(GeoParser::parsePoint(fromjson("{x: '50', y:40}"), &point)); - ASSERT_FALSE(GeoParser::parsePoint(fromjson("{x: 5, y:40, z:50}"), &point)); - ASSERT_FALSE(GeoParser::parsePoint(fromjson("{x: 5}"), &point)); + TEST(GeoParser, legacyPoint) { + PointWithCRS point; + ASSERT(GeoParser::isPoint(BSON_ARRAY(0 << 1))); + ASSERT_FALSE(GeoParser::isPoint(BSON_ARRAY(0))); + ASSERT_FALSE(GeoParser::isPoint(BSON_ARRAY(0 << 1 << 2))); + ASSERT(GeoParser::isPoint(fromjson("{x: 50, y:40}"))); + ASSERT_FALSE(GeoParser::isPoint(fromjson("{x: '50', y:40}"))); + ASSERT_FALSE(GeoParser::isPoint(fromjson("{x: 5, y:40, z:50}"))); + ASSERT_FALSE(GeoParser::isPoint(fromjson("{x: 5}"))); } TEST(GeoParser, verifyCRS) { @@ -186,43 +177,204 @@ namespace { BSONObj point1 = fromjson("{'type':'Point', 'coordinates': [40, 5], " + goodCRS1 + "}"); BSONObj point2 = fromjson("{'type':'Point', 'coordinates': [40, 5], " + goodCRS2 + "}"); - ASSERT(GeoParser::isGeoJSONPoint(point1)); + ASSERT(GeoParser::isPoint(point1)); ASSERT(GeoParser::crsIsOK(point1)); - ASSERT(GeoParser::isGeoJSONPoint(point2)); + ASSERT(GeoParser::isPoint(point2)); ASSERT(GeoParser::crsIsOK(point2)); BSONObj point3 = fromjson("{'type':'Point', 'coordinates': [40, 5], " + badCRS1 + "}"); BSONObj point4 = fromjson("{'type':'Point', 'coordinates': [40, 5], " + badCRS2 + "}"); - ASSERT_FALSE(GeoParser::isGeoJSONPoint(point3)); + ASSERT_FALSE(GeoParser::isPoint(point3)); ASSERT_FALSE(GeoParser::crsIsOK(point3)); - ASSERT_FALSE(GeoParser::isGeoJSONPoint(point4)); + ASSERT_FALSE(GeoParser::isPoint(point4)); ASSERT_FALSE(GeoParser::crsIsOK(point4)); BSONObj polygon1 = fromjson("{'type':'Polygon', 'coordinates':[ [[0,0],[5,0],[5,5],[0,5],[0,0]]," " [[1,1],[1,4],[4,4],[4,1],[1,1]] ]," + goodCRS1 + "}"); - ASSERT(GeoParser::isGeoJSONPolygon(polygon1)); + ASSERT(GeoParser::isPolygon(polygon1)); ASSERT(GeoParser::crsIsOK(polygon1)); BSONObj polygon2 = fromjson("{'type':'Polygon', 'coordinates':[ [[0,0],[5,0],[5,5],[0,5],[0,0]]," " [[1,1],[1,4],[4,4],[4,1],[1,1]] ]," + badCRS2 + "}"); - ASSERT_FALSE(GeoParser::isGeoJSONPolygon(polygon2)); + ASSERT_FALSE(GeoParser::isPolygon(polygon2)); ASSERT_FALSE(GeoParser::crsIsOK(polygon2)); BSONObj line1 = fromjson("{'type':'LineString', 'coordinates':[[1,2], [3,4], [5,6]]," + goodCRS2 + "}"); - ASSERT(GeoParser::isGeoJSONLineString(line1)); + ASSERT(GeoParser::isLine(line1)); ASSERT(GeoParser::crsIsOK(line1)); BSONObj line2 = fromjson("{'type':'LineString', 'coordinates':[[1,2], [3,4], [5,6]]," + badCRS1 + "}"); - ASSERT_FALSE(GeoParser::isGeoJSONLineString(line2)); + ASSERT_FALSE(GeoParser::isLine(line2)); ASSERT_FALSE(GeoParser::crsIsOK(line2)); } - TEST(GeoParser, parseLegacyPolygon) { - mongo::Polygon polygon; - ASSERT(GeoParser::parsePolygon(fromjson("{$polygon: [[10,20],[10,40],[30,40],[30,20]]}"), - &polygon)); - ASSERT(GeoParser::parsePolygon(fromjson("{$polygon: [[10,20], [10,40], [30,40]]}"), &polygon)); - ASSERT_FALSE(GeoParser::parsePolygon(fromjson("{$polygon: [[10,20],[10,40]]}"), &polygon)); - ASSERT_FALSE(GeoParser::parsePolygon(fromjson("{$polygon: [['10', 20],[10,40],[30,40],[30,20]]}"), &polygon)); - ASSERT_FALSE(GeoParser::parsePolygon(fromjson("{$polygon: [[10,20,30],[10,40],[30,40],[30,20]]}"), &polygon)); - ASSERT(GeoParser::parsePolygon( - fromjson("{$polygon: {a:{x:40,y:5},b:{x:40,y:6},c:{x:41,y:6},d:{x:41,y:5}}}"), &polygon)); + TEST(GeoParser, legacyPolygon) { + PolygonWithCRS polygon; + GeoParser::parsePolygon(fromjson("{$polygon: [[10,20],[10,40],[30,40],[30,20]]}"), + &polygon); + ASSERT(polygon.crs == FLAT); + + GeoParser::parsePolygon(fromjson("{$polygon: [[10,20], [10,40], [30,40]]}"), &polygon); + ASSERT(polygon.crs == FLAT); + + ASSERT_FALSE(GeoParser::isPolygon(fromjson("{$polygon: [[10,20],[10,40]]}"))); + ASSERT_FALSE(GeoParser::isPolygon(fromjson("{$polygon: [['10',20],[10,40],[30,40],[30,20]]}"))); + ASSERT_FALSE(GeoParser::isPolygon(fromjson("{$polygon: [[10,20,30],[10,40],[30,40],[30,20]]}"))); + ASSERT(GeoParser::isPolygon(fromjson("{$polygon: {a:{x:40,y:5},b:{x:40,y:6},c:{x:41,y:6},d:{x:41,y:5}}}"))); + } + + TEST(GeoParser, multiPoint) { + ASSERT(GeoParser::isMultiPoint( + fromjson("{'type':'MultiPoint','coordinates':[[1,2],[3,4]]}"))); + ASSERT(GeoParser::isMultiPoint( + fromjson("{'type':'MultiPoint','coordinates':[[3,4]]}"))); + ASSERT(GeoParser::isMultiPoint( + fromjson("{'type':'MultiPoint','coordinates':[[1,2],[3,4],[5,6],[7,8]]}"))); + + ASSERT_FALSE(GeoParser::isMultiPoint( + fromjson("{'type':'MultiPoint','coordinates':[]}"))); + ASSERT_FALSE(GeoParser::isMultiPoint( + fromjson("{'type':'MultiPoint','coordinates':[[181,2],[3,4]]}"))); + ASSERT_FALSE(GeoParser::isMultiPoint( + fromjson("{'type':'MultiPoint','coordinates':[[1,-91],[3,4]]}"))); + ASSERT_FALSE(GeoParser::isMultiPoint( + fromjson("{'type':'MultiPoint','coordinates':[[181,2],[3,'chicken']]}"))); + } + + TEST(GeoParser, parseMultiPoint) { + mongo::MultiPointWithCRS mp; + GeoParser::parseMultiPoint(fromjson("{'type':'MultiPoint','coordinates':[[1,2],[3,4]]}"), + &mp); + GeoParser::parseMultiPoint(fromjson("{'type':'MultiPoint','coordinates':[[3,4]]}"), + &mp); + GeoParser::parseMultiPoint( + fromjson("{'type':'MultiPoint','coordinates':[[1,2],[3,4],[5,6],[7,8]]}"), &mp); + } + + TEST(GeoParser, multiLineString) { + ASSERT(GeoParser::isMultiLine( + fromjson("{'type':'MultiLineString','coordinates':[ [[1,1],[2,2],[3,3]]," + "[[4,5],[6,7]]]}"))); + ASSERT(GeoParser::isMultiLine( + fromjson("{'type':'MultiLineString','coordinates':[ [[1,1],[2,2]]," + "[[4,5],[6,7]]]}"))); + ASSERT(GeoParser::isMultiLine( + fromjson("{'type':'MultiLineString','coordinates':[ [[1,1],[2,2]]]}"))); + + ASSERT(GeoParser::isMultiLine( + fromjson("{'type':'MultiLineString','coordinates':[ [[1,1],[2,2]]," + "[[2,2],[1,1]]]}"))); + ASSERT_FALSE(GeoParser::isMultiLine( + fromjson("{'type':'MultiLineString','coordinates':[ [[1,1]]]}"))); + ASSERT_FALSE(GeoParser::isMultiLine( + fromjson("{'type':'MultiLineString','coordinates':[ [[1,1]],[[1,2],[3,4]]]}"))); + ASSERT_FALSE(GeoParser::isMultiLine( + fromjson("{'type':'MultiLineString','coordinates':[ [[181,1],[2,2]]]}"))); + ASSERT_FALSE(GeoParser::isMultiLine( + fromjson("{'type':'MultiLineString','coordinates':[ [[181,1],[2,-91]]]}"))); + } + + TEST(GeoParser, parseMultiLine) { + mongo::MultiLineWithCRS mls; + + GeoParser::parseMultiLine( + fromjson("{'type':'MultiLine','coordinates':[ [[1,1],[2,2],[3,3]]," + "[[4,5],[6,7]]]}"), + &mls); + + GeoParser::parseMultiLine( + fromjson("{'type':'MultiLine','coordinates':[ [[1,1],[2,2]]," + "[[4,5],[6,7]]]}"), + &mls); + + GeoParser::parseMultiLine( + fromjson("{'type':'MultiLine','coordinates':[ [[1,1],[2,2]]]}"), + &mls); + + GeoParser::parseMultiLine( + fromjson("{'type':'MultiLine','coordinates':[ [[1,1],[2,2]]," + "[[2,2],[1,1]]]}"), + &mls); + } + + TEST(GeoParser, multiPolygon) { + ASSERT(GeoParser::isMultiPolygon( + fromjson("{'type':'MultiPolygon','coordinates':[" + "[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]]," + "[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]," + "[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]" + "]}"))); + ASSERT(GeoParser::isMultiPolygon( + fromjson("{'type':'MultiPolygon','coordinates':[" + "[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]," + "[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]" + "]}"))); + } + + TEST(GeoParser, parseMultiPolygon) { + mongo::MultiPolygonWithCRS mp; + GeoParser::parseMultiPolygon( + fromjson("{'type':'MultiPolygon','coordinates':[" + "[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]]," + "[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]," + "[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]" + "]}"), &mp); + } + + TEST(GeoParser, parseGeometryCollection) { + { + mongo::GeometryCollection gc; + BSONObj obj = fromjson( + "{ 'type': 'GeometryCollection', 'geometries': [" + "{ 'type': 'Point','coordinates': [100.0,0.0]}," + "{ 'type': 'LineString', 'coordinates': [ [101.0, 0.0], [102.0, 1.0] ]}" + "]}"); + ASSERT(GeoParser::isGeometryCollection(obj)); + GeoParser::parseGeometryCollection(obj, &gc); + ASSERT_FALSE(gc.supportsContains()); + } + + { + BSONObj obj = fromjson( + "{ 'type': 'GeometryCollection', 'geometries': [" + "{'type':'MultiPolygon','coordinates':[" + "[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]]," + "[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]," + "[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]" + "]}" + "]}"); + + ASSERT(GeoParser::isGeometryCollection(obj)); + mongo::GeometryCollection gc; + GeoParser::parseGeometryCollection(obj, &gc); + ASSERT_TRUE(gc.supportsContains()); + } + { + BSONObj obj = fromjson( + "{ 'type': 'GeometryCollection', 'geometries': [" + "{'type':'Polygon', 'coordinates':[ [[0,0],[0,91],[5,5],[5,0],[0,0]] ]}," + "{'type':'MultiPolygon','coordinates':[" + "[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]]," + "[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]," + "[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]" + "]}" + "]}"); + + ASSERT_FALSE(GeoParser::isGeometryCollection(obj)); + } + + { + BSONObj obj = fromjson( + "{ 'type': 'GeometryCollection', 'geometries': [" + "{'type':'Polygon', 'coordinates':[ [[0,0],[0,5],[5,5],[5,0],[0,0]] ]}," + "{'type':'MultiPolygon','coordinates':[" + "[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]]," + "[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]," + "[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]" + "]}" + "]}"); + + ASSERT(GeoParser::isGeometryCollection(obj)); + mongo::GeometryCollection gc; + GeoParser::parseGeometryCollection(obj, &gc); + ASSERT_TRUE(gc.supportsContains()); + } } } |