[postgis-tickets] r15326 - Fix for incorrect scale/translate in ST_AsMVTGeom

bjorn at wololo.org bjorn at wololo.org
Fri Mar 10 13:54:09 PST 2017


Author: bjornharrtell
Date: 2017-03-10 13:54:09 -0800 (Fri, 10 Mar 2017)
New Revision: 15326

Modified:
   trunk/postgis/mvt.c
   trunk/regress/mvt.sql
   trunk/regress/mvt_expected
Log:
Fix for incorrect scale/translate in ST_AsMVTGeom

Modified: trunk/postgis/mvt.c
===================================================================
--- trunk/postgis/mvt.c	2017-03-10 20:17:32 UTC (rev 15325)
+++ trunk/postgis/mvt.c	2017-03-10 21:54:09 UTC (rev 15326)
@@ -444,55 +444,6 @@
 	ctx->feature->tags = tags;
 }
 
-static void ptarray_mirror_y(POINTARRAY *pa, uint32_t extent)
-{
-	int i;
-	POINT2D *p;
-
-	for (i = 0; i < pa->npoints; i++) {
-		p = (POINT2D *) getPoint_internal(pa, i);
-		p->y = extent - p->y;
-	}
-}
-
-static void lwgeom_mirror_y(LWGEOM *in, uint32_t extent)
-{
-	LWCOLLECTION *col;
-	LWPOLY *poly;
-	int i;
-
-	if ( (!in) || lwgeom_is_empty(in) ) return;
-
-	switch (in->type) {
-	case POINTTYPE:
-		ptarray_mirror_y(lwgeom_as_lwpoint(in)->point, extent);
-		break;
-	case LINETYPE:
-		ptarray_mirror_y(lwgeom_as_lwline(in)->points, extent);
-		break;
-	case POLYGONTYPE:
-		poly = (LWPOLY *) in;
-		for (i=0; i<poly->nrings; i++)
-			ptarray_mirror_y(poly->rings[i], extent);
-		break;
-	case MULTIPOINTTYPE:
-	case MULTILINETYPE:
-	case MULTIPOLYGONTYPE:
-	case COLLECTIONTYPE:
-		col = (LWCOLLECTION *) in;
-		for (i=0; i<col->ngeoms; i++)
-			lwgeom_mirror_y(col->geoms[i], extent);
-		break;
-	default:
-		lwerror("lwgeom_mirror_y: unsupported geometry type: %s",
-		        lwtype_name(in->type));
-		return;
-	}
-
-	lwgeom_drop_bbox(in);
-	lwgeom_add_bbox(in);
-}
-
 /**
  * Transform a geometry into vector tile coordinate space.
  *
@@ -506,6 +457,8 @@
 	double height = gbox->ymax - gbox->ymin;
 	double resx = width / extent;
 	double resy = height / extent;
+	double fx = extent / width;
+	double fy = -(extent / height);
 
 	if (width == 0 || height == 0)
 		lwerror("mvt_geom: bounds width or height cannot be 0");
@@ -529,13 +482,15 @@
 #endif
 	}
 
-	POINT4D factors;
-	factors.x = resx;
-	factors.y = resy;
-	factors.z = 1;
-	factors.m = 1;
+	AFFINE affine;
+	memset (&affine, 0, sizeof(affine));
+	affine.afac = fx;
+	affine.efac = fy;
+	affine.ifac = 1;
+	affine.xoff = -gbox->xmin * fx;
+	affine.yoff = -gbox->ymax * fy;
 
-	lwgeom_scale(lwgeom, &factors);
+	lwgeom_affine(lwgeom, &affine);
 
 	gridspec grid;
 	memset(&grid, 0, sizeof(gridspec));
@@ -550,7 +505,6 @@
 		lwgeom_out = lwgeom_grid(lwgeom_centroid(lwgeom), &grid);
 
 	lwgeom_force_clockwise(lwgeom_out);
-	lwgeom_mirror_y(lwgeom_out, extent);
 	lwgeom_out = lwgeom_make_valid(lwgeom_out);
 
 	return lwgeom_out;

Modified: trunk/regress/mvt.sql
===================================================================
--- trunk/regress/mvt.sql	2017-03-10 20:17:32 UTC (rev 15325)
+++ trunk/regress/mvt.sql	2017-03-10 21:54:09 UTC (rev 15326)
@@ -17,8 +17,12 @@
 	4096, 0, false));
 select 'PG5', ST_AsText(ST_AsMVTGeom(
 	ST_GeomFromText('POLYGON ((0 0, 10 0, 10 5, 0 -5, 0 0))'),
-	ST_MakeBox2D(ST_Point(0, 0), ST_Point(1, 1)),
+	ST_MakeBox2D(ST_Point(0, 0), ST_Point(4096*4096, 4096*4096)),
 	4096, 0, false));
+select 'PG6', ST_AsText(ST_AsMVTGeom(
+	ST_GeomFromText('POLYGON ((762780 6474467, 717821 6797045, 1052826 6797045, 762780 6474467))'),
+	ST_MakeBox2D(ST_Point(626172.135625, 6261721.35625), ST_Point(1252344.27125, 6887893.49188)),
+	4096, 0, false));
 
 -- geometry encoding tests
 SELECT 'TG1', encode(ST_AsMVT('test', 4096, 'geom', q), 'base64') FROM (SELECT 1 AS c1,

Modified: trunk/regress/mvt_expected
===================================================================
--- trunk/regress/mvt_expected	2017-03-10 20:17:32 UTC (rev 15325)
+++ trunk/regress/mvt_expected	2017-03-10 21:54:09 UTC (rev 15326)
@@ -1,16 +1,17 @@
 PG1|POINT(1 4094)
-PG2|POINT(2 4092)
-PG3|POINT(0 4095)
+PG2|POINT(0 4095)
+PG3|POINT(2 4092)
 PG4|MULTIPOLYGON(((5 4096,10 4096,10 4091,5 4096)),((0 4096,0 4101,5 4096,0 4096)))
 PG5|POINT(0 4096)
+PG6|POLYGON((894 2704,2791 594,600 594,894 2704))
 TG1|GiEKBHRlc3QSDBICAAAYASIECTLePxoCYzEiAigBKIAgeAI=
 TG2|GiMKBHRlc3QSDhICAAAYASIGETLePwIBGgJjMSICKAEogCB4Ag==
 TG3|GiYKBHRlc3QSERICAAAYAiIJCQCAQArQD88PGgJjMSICKAEogCB4Ag==
 TG4|GioKBHRlc3QSFRICAAAYAiINCQCAQBLoB+cH6AfnBxoCYzEiAigBKIAgeAI=
 TG5|GjgKBHRlc3QSIxICAAAYAiIbCQL+PxLoB+cH6AfnBwnND84PEugH5wfoB+cHGgJjMSICKAEogCB4
 Ag==
-TG6|GjIKBHRlc3QSHRICAAAYAyIVCUbsPxoxEwonPAkPCTEeEhQUCh0PGgJjMSICKAEogCB4Ag==
-TG7|Gj0KBHRlc3QSKBICAAAYAyIgCVCwPxIKFDEdDwkAFCIyHh0eJwkAJw8JKBQSEwkAFA8aAmMxIgIo
+TG6|GjIKBHRlc3QSHRICAAAYAyIVCUbsPxoURTsKCSgPCRQTEh4JCR4PGgJjMSICKAEogCB4Ag==
+TG7|Gj0KBHRlc3QSKBICAAAYAyIgCVCwPxInCTIeDwkxCSITCgAoKAoeHQ8JHQASEwoAEw8aAmMxIgIo
 ASiAIHgC
 TG8|GiEKBHRlc3QSDBICAAAYASIECTLePxoCYzEiAigBKIAgeAI=
 TG9|GiMKBHRlc3QSDhICAAAYASIGETLeP2VGGgJjMSICKAEogCB4Ag==



More information about the postgis-tickets mailing list