@@ -97,21 +97,113 @@ suite.addBatch({
9797 "Point" : function ( centroid ) {
9898 assert . deepEqual ( centroid ( { type : "Point" , coordinates : [ 0 , 0 ] } ) , [ 480 , 250 ] ) ;
9999 } ,
100- "MultiPoint" : function ( centroid ) {
101- assert . deepEqual ( centroid ( { type : "MultiPoint" , coordinates : [ [ 0 , 0 ] ] } ) , [ 480 , 250 ] ) ;
100+ "MultiPoint" : {
101+ "empty" : function ( centroid ) {
102+ assert . isNull ( centroid ( { type : "MultiPoint" , coordinates : [ ] } ) ) ;
103+ } ,
104+ "single point" : function ( centroid ) {
105+ assert . deepEqual ( centroid ( { type : "MultiPoint" , coordinates : [ [ 0 , 0 ] ] } ) , [ 480 , 250 ] ) ;
106+ } ,
107+ "two points" : function ( centroid ) {
108+ assert . deepEqual ( centroid ( { type : "MultiPoint" , coordinates : [ [ - 122 , 37 ] , [ - 74 , 40 ] ] } ) , [ - 10 , 57.5 ] ) ;
109+ }
102110 } ,
103- "LineString" : function ( centroid ) {
104- assert . deepEqual ( centroid ( { type : "LineString" , coordinates : [ [ 100 , 0 ] , [ 0 , 0 ] ] } ) , [ 730 , 250 ] ) ;
105- assert . deepEqual ( centroid ( { type : "LineString" , coordinates : [ [ 0 , 0 ] , [ 100 , 0 ] , [ 101 , 0 ] ] } ) , [ 732.5 , 250 ] ) ;
111+ "LineString" : {
112+ "empty" : function ( centroid ) {
113+ assert . isNull ( centroid ( { type : "LineString" , coordinates : [ ] } ) ) ;
114+ } ,
115+ "two points" : function ( centroid ) {
116+ assert . deepEqual ( centroid ( { type : "LineString" , coordinates : [ [ 100 , 0 ] , [ 0 , 0 ] ] } ) , [ 730 , 250 ] ) ;
117+ assert . deepEqual ( centroid ( { type : "LineString" , coordinates : [ [ 0 , 0 ] , [ 100 , 0 ] , [ 101 , 0 ] ] } ) , [ 732.5 , 250 ] ) ;
118+ } ,
119+ "two points, one unique" : function ( centroid ) {
120+ assert . isNull ( centroid ( { type : "LineString" , coordinates : [ [ - 122 , 37 ] , [ - 122 , 37 ] ] } ) ) ;
121+ assert . isNull ( centroid ( { type : "LineString" , coordinates : [ [ - 74 , 40 ] , [ - 74 , 40 ] ] } ) ) ;
122+ } ,
123+ "three points; two unique" : function ( centroid ) {
124+ assert . deepEqual ( centroid ( { type : "LineString" , coordinates : [ [ - 122 , 37 ] , [ - 74 , 40 ] , [ - 74 , 40 ] ] } ) , [ - 10 , 57.5 ] ) ;
125+ } ,
126+ "three points" : function ( centroid ) {
127+ assert . inDelta ( centroid ( { type : "LineString" , coordinates : [ [ - 122 , 37 ] , [ - 74 , 40 ] , [ - 100 , 0 ] ] } ) , [ 17.389135 , 103.563545 ] , 1e-6 ) ;
128+ }
106129 } ,
107130 "MultiLineString" : function ( centroid ) {
108131 assert . deepEqual ( centroid ( { type : "MultiLineString" , coordinates : [ [ [ 100 , 0 ] , [ 0 , 0 ] ] , [ [ - 10 , 0 ] , [ 0 , 0 ] ] ] } ) , [ 705 , 250 ] ) ;
109132 } ,
110- "Polygon" : function ( centroid ) {
111- assert . deepEqual ( centroid ( { type : "Polygon" , coordinates : [ [ [ 100 , 0 ] , [ 101 , 0 ] , [ 101 , 1 ] , [ 100 , 1 ] , [ 100 , 0 ] ] ] } ) , [ 982.5 , 247.5 ] ) ;
133+ "Polygon" : {
134+ "single ring" : function ( centroid ) {
135+ assert . deepEqual ( centroid ( { type : "Polygon" , coordinates : [ [ [ 100 , 0 ] , [ 101 , 0 ] , [ 101 , 1 ] , [ 100 , 1 ] , [ 100 , 0 ] ] ] } ) , [ 982.5 , 247.5 ] ) ;
136+ } ,
137+ "zero area" : function ( centroid ) {
138+ assert . isNull ( centroid ( { type : "Polygon" , coordinates : [ [ [ 1 , 0 ] , [ 2 , 0 ] , [ 3 , 0 ] , [ 1 , 0 ] ] ] } ) ) ;
139+ } ,
140+ "two rings, one zero area" : function ( centroid ) {
141+ assert . deepEqual ( centroid ( { type : "Polygon" , coordinates : [ [ [ 100 , 0 ] , [ 101 , 0 ] , [ 101 , 1 ] , [ 100 , 1 ] , [ 100 , 0 ] ] , [ [ 100.1 , 0 ] , [ 100.2 , 0 ] , [ 100.3 , 0 ] , [ 100.1 , 0 ] ] ] } ) , [ 982.5 , 247.5 ] ) ;
142+ }
143+ } ,
144+ "MultiPolygon" : {
145+ "empty" : function ( centroid ) {
146+ assert . isNull ( centroid ( { type : "MultiPolygon" , coordinates : [ ] } ) ) ;
147+ } ,
148+ "single polygon" : function ( centroid ) {
149+ assert . deepEqual ( centroid ( { type : "MultiPolygon" , coordinates : [ [ [ [ 100 , 0 ] , [ 101 , 0 ] , [ 101 , 1 ] , [ 100 , 1 ] , [ 100 , 0 ] ] ] ] } ) , [ 982.5 , 247.5 ] ) ;
150+ } ,
151+ "two polygons" : function ( centroid ) {
152+ assert . deepEqual ( centroid ( { type : "MultiPolygon" , coordinates : [ [ [ [ 100 , 0 ] , [ 101 , 0 ] , [ 101 , 1 ] , [ 100 , 1 ] , [ 100 , 0 ] ] ] , [ [ [ 0 , 0 ] , [ 1 , 0 ] , [ 1 , - 1 ] , [ 0 , - 1 ] , [ 0 , 0 ] ] ] ] } ) , [ 732.5 , 250 ] ) ;
153+ } ,
154+ "two polygons, one zero area" : function ( centroid ) {
155+ assert . deepEqual ( centroid ( { type : "MultiPolygon" , coordinates : [ [ [ [ 100 , 0 ] , [ 101 , 0 ] , [ 101 , 1 ] , [ 100 , 1 ] , [ 100 , 0 ] ] ] , [ [ [ 0 , 0 ] , [ 1 , 0 ] , [ 2 , 0 ] , [ 0 , 0 ] ] ] ] } ) , [ 982.5 , 247.5 ] ) ;
156+ }
157+ } ,
158+ "GeometryCollection" : {
159+ "empty" : function ( centroid ) {
160+ assert . isNull ( centroid ( { type : "GeometryCollection" , geometries : [ ] } ) ) ;
161+ } ,
162+ "collection containing an empty geometry" : function ( centroid ) {
163+ assert . deepEqual ( centroid ( { type : "GeometryCollection" , geometries : [
164+ { type : "MultiPoint" , coordinates : [ ] } ,
165+ { type : "Point" , coordinates : [ - 122 , 37 ] }
166+ ] } ) , [ - 130 , 65 ] ) ;
167+ } ,
168+ "ignores dimensions lower than one" : function ( centroid ) {
169+ assert . deepEqual ( centroid ( { type : "GeometryCollection" , geometries : [
170+ { type : "Point" , coordinates : [ - 122 , 37 ] } ,
171+ { type : "MultiPoint" , coordinates : [ [ - 122 , 37 ] ] } ,
172+ { type : "LineString" , coordinates : [ [ - 122 , 37 ] , [ - 74 , 40 ] ] }
173+ ] } ) , [ - 10 , 57.5 ] ) ;
174+ } ,
175+ "ignores dimensions lower than two" : function ( centroid ) {
176+ assert . deepEqual ( centroid ( { type : "GeometryCollection" , geometries : [
177+ { type : "Point" , coordinates : [ - 122 , 37 ] } ,
178+ { type : "MultiPoint" , coordinates : [ [ - 122 , 37 ] ] } ,
179+ { type : "LineString" , coordinates : [ [ - 122 , 37 ] , [ - 74 , 40 ] ] } ,
180+ { type : "Polygon" , coordinates : [ [ [ 100 , 0 ] , [ 101 , 0 ] , [ 101 , 1 ] , [ 100 , 1 ] , [ 100 , 0 ] ] ] }
181+ ] } ) , [ 982.5 , 247.5 ] ) ;
182+ } ,
183+ "empty higher dimensions" : function ( centroid ) {
184+ assert . isNull ( centroid ( { type : "GeometryCollection" , geometries : [
185+ { type : "Point" , coordinates : [ - 122 , 37 ] } ,
186+ { type : "MultiPoint" , coordinates : [ [ - 122 , 37 ] ] } ,
187+ { type : "MultiLineString" , coordinates : [ ] }
188+ ] } ) ) ;
189+ }
112190 } ,
113- "MultiPolygon" : function ( centroid ) {
114- assert . deepEqual ( centroid ( { type : "MultiPolygon" , coordinates : [ [ [ [ 100 , 0 ] , [ 101 , 0 ] , [ 101 , 1 ] , [ 100 , 1 ] , [ 100 , 0 ] ] ] , [ [ [ 0 , 0 ] , [ 1 , 0 ] , [ 1 , - 1 ] , [ 0 , - 1 ] , [ 0 , 0 ] ] ] ] } ) , [ 732.5 , 250 ] ) ;
191+ "FeatureCollection" : {
192+ "empty" : function ( centroid ) {
193+ assert . isNull ( centroid ( { type : "FeatureCollection" , features : [ ] } ) ) ;
194+ } ,
195+ "collection containing a feature with an empty geometry" : function ( centroid ) {
196+ assert . deepEqual ( centroid ( { type : "FeatureCollection" , features : [
197+ feature ( { type : "MultiPoint" , coordinates : [ ] } ) ,
198+ feature ( { type : "Point" , coordinates : [ - 122 , 37 ] } )
199+ ] } ) , [ - 130 , 65 ] ) ;
200+ } ,
201+ "repeated geometries" : function ( centroid ) {
202+ var lineString = { type : "LineString" , coordinates : [ [ - 122 , 37 ] , [ - 74 , 40 ] ] } ;
203+ assert . deepEqual ( centroid ( { type : "FeatureCollection" , features : [ lineString ] . map ( feature ) } ) , [ - 10 , 57.5 ] ) ;
204+ assert . deepEqual ( centroid ( { type : "FeatureCollection" , features : [ lineString , lineString ] . map ( feature ) } ) , [ - 10 , 57.5 ] ) ;
205+ assert . deepEqual ( centroid ( { type : "FeatureCollection" , features : [ lineString , lineString , lineString ] . map ( feature ) } ) , [ - 10 , 57.5 ] ) ;
206+ }
115207 }
116208 }
117209 }
@@ -127,4 +219,8 @@ var testContext = {
127219 buffer : function ( ) { var result = testBuffer ; testBuffer = [ ] ; return result ; }
128220} ;
129221
222+ function feature ( o ) {
223+ return { type : "Feature" , geometry : o } ;
224+ }
225+
130226suite . export ( module ) ;
0 commit comments