@@ -1341,9 +1341,15 @@ static int prepare_database(layerObj *layer, rectObj rect, char **query_string)
1341
1341
/*
1342
1342
"Geometry::STGeomFromText('POLYGON(())',)" + terminator = 40 chars
1343
1343
Plus 10 formatted doubles (15 digits of precision, a decimal point, a space/comma delimiter each = 17 chars each)
1344
+ Plus SRID + comma - if SRID is a long...we'll be safe with 10 chars
1345
+
1346
+ or for geography columns
1347
+
1348
+ "Geography::STGeomFromText('CURVEPOLYGON(())',)" + terminator = 46 chars
1349
+ Plus 18 formatted doubles (15 digits of precision, a decimal point, a space/comma delimiter each = 17 chars each)
1344
1350
Plus SRID + comma - if SRID is a long...we'll be safe with 10 chars
1345
1351
*/
1346
- char box3d [40 + 10 * 22 + 11 ];
1352
+ char box3d [46 + 18 * 22 + 11 ];
1347
1353
int t ;
1348
1354
1349
1355
char * pos_from , * pos_ftab , * pos_space , * pos_paren ;
@@ -1399,10 +1405,25 @@ static int prepare_database(layerObj *layer, rectObj rect, char **query_string)
1399
1405
/* create point shape for rectangles with zero area */
1400
1406
sprintf (box3d , "%s::STGeomFromText('POINT(%.15g %.15g)',%s)" , /* %s.STSrid)", */
1401
1407
layerinfo -> geom_column_type , rect .minx , rect .miny , layerinfo -> user_srid );
1402
- }
1403
- else {
1404
- sprintf (box3d , "%s::STGeomFromText('POLYGON((%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g))',%s)" , /* %s.STSrid)", */
1405
- layerinfo -> geom_column_type ,
1408
+ } else if (strcasecmp (layerinfo -> geom_column_type , "geography" ) == 0 ) {
1409
+ /* SQL Server has a problem when x is -180 or 180 */
1410
+ double minx = rect .minx <= -180 ? -179.999 : rect .minx ;
1411
+ double maxx = rect .maxx >= 180 ? 179.999 : rect .maxx ;
1412
+ double miny = rect .miny < -90 ? -90 : rect .miny ;
1413
+ double maxy = rect .maxy > 90 ? 90 : rect .maxy ;
1414
+ sprintf (box3d , "Geography::STGeomFromText('CURVEPOLYGON((%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g))',%s)" , /* %s.STSrid)", */
1415
+ minx , miny ,
1416
+ minx + (maxx - minx ) / 2 , miny ,
1417
+ maxx , miny ,
1418
+ maxx , miny + (maxy - miny ) / 2 ,
1419
+ maxx , maxy ,
1420
+ minx + (maxx - minx ) / 2 , maxy ,
1421
+ minx , maxy ,
1422
+ minx , miny + (maxy - miny ) / 2 ,
1423
+ minx , miny ,
1424
+ layerinfo -> user_srid );
1425
+ } else {
1426
+ sprintf (box3d , "Geometry::STGeomFromText('POLYGON((%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g,%.15g %.15g))',%s)" , /* %s.STSrid)", */
1406
1427
rect .minx , rect .miny ,
1407
1428
rect .maxx , rect .miny ,
1408
1429
rect .maxx , rect .maxy ,
0 commit comments