]> granicus.if.org Git - postgis/commitdiff
St_AsMVTGeom: Handle type changes in geometry collections
authorRaúl Marín Rodríguez <rmrodriguez@carto.com>
Wed, 3 Oct 2018 10:52:58 +0000 (10:52 +0000)
committerRaúl Marín Rodríguez <rmrodriguez@carto.com>
Wed, 3 Oct 2018 10:52:58 +0000 (10:52 +0000)
References #4181

git-svn-id: http://svn.osgeo.org/postgis/trunk@16876 b70326c6-7e19-0410-871a-916f4a2858ee

postgis/mvt.c
regress/mvt.sql
regress/mvt_expected

index 23749c02dab01ea2e48a5bd2fa86b61493d8c083..174f3c3cf8c7abe899c4deecfc0b3646bcd4d5dd 100644 (file)
@@ -788,6 +788,39 @@ static void parse_values(mvt_agg_context *ctx)
        POSTGIS_DEBUGF(3, "parse_values n_tags %zd", ctx->feature->n_tags);
 }
 
+/* For a given geometry, look for the highest dimensional basic type, that is,
+ * point, line or polygon */
+static uint8
+lwgeom_get_basic_type(LWGEOM *geom)
+{
+       switch(geom->type)
+       {
+       case POINTTYPE:
+       case LINETYPE:
+       case POLYGONTYPE:
+               return geom->type;
+       case MULTIPOINTTYPE:
+       case MULTILINETYPE:
+       case MULTIPOLYGONTYPE:
+               return geom->type - 3; /* Based on LWTYPE positions */
+       case COLLECTIONTYPE:
+       {
+               uint32_t i;
+               uint8 type = 0;
+               LWCOLLECTION *g = (LWCOLLECTION*)geom;
+               for (i = 0; i < g->ngeoms; i++)
+               {
+                       LWGEOM *sg = g->geoms[i];
+                       type = Max(type, lwgeom_get_basic_type(sg));
+               }
+               return type;
+       }
+       default:
+               elog(ERROR, "%s: Invalid type (%d)", __func__, geom->type);
+       }
+}
+
+
 /**
  * In place process a collection to find a concrete geometry
  * object and expose that as the actual object. Will some
@@ -795,27 +828,12 @@ static void parse_values(mvt_agg_context *ctx)
  * draw it anyways.
  */
 static void
-lwgeom_to_basic_type(LWGEOM *geom)
+lwgeom_to_basic_type(LWGEOM *geom, uint8 original_type)
 {
        if (lwgeom_get_type(geom) == COLLECTIONTYPE)
        {
-               /* MVT doesn't handle generic collections, so we */
-               /* need to strip them down to a typed collection */
-               /* by finding the largest basic type available and */
-               /* using that as the basis of a typed collection. */
                LWCOLLECTION *g = (LWCOLLECTION*)geom;
-               LWCOLLECTION *gc;
-               uint32_t i, maxtype = 0;
-               for (i = 0; i < g->ngeoms; i++)
-               {
-                       LWGEOM *sg = g->geoms[i];
-                       if (sg->type > maxtype && sg->type < COLLECTIONTYPE)
-                               maxtype = sg->type;
-               }
-               if (maxtype > 3) maxtype -= 3;
-               /* Force the working geometry to be a simpler version */
-               /* of itself */
-               gc = lwcollection_extract(g, maxtype);
+               LWCOLLECTION *gc = lwcollection_extract(g, original_type);
                *g = *gc;
        }
 }
@@ -837,6 +855,7 @@ LWGEOM *mvt_geom(LWGEOM *lwgeom, const GBOX *gbox, uint32_t extent, uint32_t buf
        double height = gbox->ymax - gbox->ymin;
        double resx, resy, res, fx, fy;
        int preserve_collapsed = LW_TRUE;
+       const uint8_t basic_type = lwgeom_get_basic_type(lwgeom);
        POSTGIS_DEBUG(2, "mvt_geom called");
 
        /* Short circuit out on EMPTY */
@@ -855,13 +874,12 @@ LWGEOM *mvt_geom(LWGEOM *lwgeom, const GBOX *gbox, uint32_t extent, uint32_t buf
        fx = extent / width;
        fy = -(extent / height);
 
-       if (FLAGS_GET_BBOX(lwgeom->flags) && lwgeom->bbox &&
-               (lwgeom->type == LINETYPE || lwgeom->type == MULTILINETYPE ||
-               lwgeom->type == POLYGONTYPE || lwgeom->type == MULTIPOLYGONTYPE))
+       if (basic_type == LINETYPE || basic_type == POLYGONTYPE)
        {
                // Shortcut to drop geometries smaller than the resolution
-               double bbox_width = lwgeom->bbox->xmax - lwgeom->bbox->xmin;
-               double bbox_height = lwgeom->bbox->ymax - lwgeom->bbox->ymin;
+               const GBOX *lwgeom_gbox = lwgeom_get_bbox(lwgeom);
+               double bbox_width = lwgeom_gbox->xmax - lwgeom_gbox->xmin;
+               double bbox_height = lwgeom_gbox->ymax - lwgeom_gbox->ymin;
                if (bbox_height * bbox_height + bbox_width * bbox_width < res * res)
                        return NULL;
        }
@@ -904,9 +922,7 @@ LWGEOM *mvt_geom(LWGEOM *lwgeom, const GBOX *gbox, uint32_t extent, uint32_t buf
                        /* For some polygons, the simplify step might have left them
                         * as invalid, which can cause clipping to return the complementary
                         * geometry of what it should */
-                       if ((lwgeom->type == POLYGONTYPE ||
-                               lwgeom->type == MULTIPOLYGONTYPE ||
-                               lwgeom->type == COLLECTIONTYPE) &&
+                       if ((basic_type == POLYGONTYPE) &&
                            !gbox_contains_2d(&pre_clip_box, lwgeom_get_bbox(clipped_geom)))
                        {
                                /* TODO: Adapt this when and if Exception Policies are introduced.
@@ -942,22 +958,11 @@ LWGEOM *mvt_geom(LWGEOM *lwgeom, const GBOX *gbox, uint32_t extent, uint32_t buf
                return NULL;
 
 
-       if (lwgeom->type == POLYGONTYPE ||
-               lwgeom->type == MULTIPOLYGONTYPE ||
-               lwgeom->type == COLLECTIONTYPE)
+       if (basic_type == POLYGONTYPE)
        {
                /* Force validation as per MVT spec */
                lwgeom = lwgeom_make_valid(lwgeom);
 
-               /* Drop type changes tp play nice with MVT renderers */
-               if (!(lwgeom->type == POLYGONTYPE ||
-                       lwgeom->type == MULTIPOLYGONTYPE ||
-                       lwgeom->type == COLLECTIONTYPE))
-               {
-                       lwgeom_free(lwgeom);
-                       return NULL;
-               }
-
                /* In image coordinates CW actually comes out a CCW, so we reverse */
                lwgeom_force_clockwise(lwgeom);
                lwgeom_reverse_in_place(lwgeom);
@@ -965,7 +970,14 @@ LWGEOM *mvt_geom(LWGEOM *lwgeom, const GBOX *gbox, uint32_t extent, uint32_t buf
 
        /* if geometry collection extract highest dimensional geometry type */
        if (lwgeom->type == COLLECTIONTYPE)
-               lwgeom_to_basic_type(lwgeom);
+               lwgeom_to_basic_type(lwgeom, basic_type);
+
+       if (basic_type != lwgeom_get_basic_type(lwgeom))
+       {
+               /* Drop type changes to play nice with MVT renderers */
+               POSTGIS_DEBUG(3, "mvt_geom: Dropping geometry after type change");
+               return NULL;
+       }
 
        if (lwgeom == NULL || lwgeom_is_empty(lwgeom))
                return NULL;
index 068734801ee417724ac320f2dd557057d234c2fc..2c1218bfcd67f5f618ccd6b08730d9dad7705acb 100644 (file)
@@ -273,6 +273,14 @@ SELECT 'PG45', ST_AsEWKT(ST_AsMVTGeom(
        16,
        true));
 
+-- Geometry type change of one geometry of the multipolygon used to fallback to multilinestring
+SELECT 'PG46', St_AsEWKT(ST_AsMVTGeom(
+       'SRID=3857;MULTIPOLYGON(((-8230324.85567616 4984496.35685962,-8230307.1114228 4984654.46474466,-8230285.21085987 4984959.60349704,-8230324.85567616 4984496.35685962)),((-8230327.54013683 4984444.33052449,-8230327.23971431 4984450.39401942,-8230327.26833036 4984449.87731981,-8230327.54013683 4984444.33052449)))'::geometry,
+       'SRID=3857;POLYGON((-8238077.16046316 4989809.20645631,-8238077.16046316 4980025.2668358,-8228293.22084265 4980025.2668358,-8228293.22084265 4989809.20645631,-8238077.16046316 4989809.20645631))'::geometry,
+       4096,
+       16,
+       true));
+
 -- geometry encoding tests
 SELECT 'TG1', encode(ST_AsMVT(q, 'test', 4096, 'geom'), 'base64') FROM (SELECT 1 AS c1,
        ST_AsMVTGeom(ST_GeomFromText('POINT(25 17)'),
@@ -433,13 +441,13 @@ SELECT 'TU3', encode(ST_AsMVT(q, 'test', 4096, 'geom'), 'base64')
        FROM (SELECT NULL::integer AS c1, NULL AS geom) AS q;
 
 -- Ticket #3922
-SELECT '#3922', length(bytea(ST_AsMVTGeom(
+SELECT '#3922', St_AsEWKT(ST_AsMVTGeom(
                st_geomfromtwkb('\x06000104a501d0a7db06dad0940120ee030660229604109c010ed6011246143e76201a22b401a601f001b801580ef40122d803ca01de0cf00e80049204ac02b602be0138ca08bc02d605d201d2013cb804a401be013c9c03cd028608c106f001c1018601c7011e970125f10207439b02850a76ff0415030d0725431973132f227768671a5f133f17290865e405ba0154ab030415348502cc038d120c37d326c706850896109f01e201350f6f0a1903930165830121412d052928651d2b0d0f1107170b490c33120f010f0813034f47f50259190181031b3713ed04d901bd01439b02639507c10201021062054c1d3c101e6a0c2000684e6a7c1a681f443d160f044f0f490f03020b08051c01080c0e18013a012801380e08005808522d3a4c1c062a0f0e192a190a3b22194803261c1376122ac201d8011a101c065a17a8011c303206460c164a18a4015c56620801162d1404a402c601262a143002421222290f7b581d0719011d0311002908250a25021d030f0111030f05a3014315050d05110383011b9d011f3309a70347170325058b03417515130361190b4e19b40105fe208810041314041018270705c0039d0416251a1213241b0ffd02f5029408d001990218110607100c070a0b0819031c263432302454322a1e262a101e2a521426101c0c10101210121e18341c321c2c1c26222230162a10320c280e3202080e26123e0a2c041a002805360002051e010807161b281b1e1312010213101b14150e0906130c331e3518250e250c230a1d0a110e091407140718031a0008031a03140112093a199a01199801052e04100a0c120a4a0c7e1e2406220c4e20b60236c8013014020c0510090a110e1b0e11140d18021c08261054261417080f082504a702ea012c58068801180e0f1477209301082f062f00271f0b4b17170311020d100b180d180b0c1502190019031f09512d9b01592a1f120d0c0f0a15126b0637060f100b16032c0a2a1410120c120a240014011a03160314001482010100810900311753478d0117218b02b3027a692223745b2a111e031e130403061902691aa50118752ea70158045818562cae0170202614200e2e208a012e6c4a154c33448b01242f34651e2b221d481f2017122334a1010a510f2b6d8d021d5f073304351233242bce01772a251c3106291b4b032110351c2324239c016f260f5c138a01074e14261422282844221c7a24779a022db90127450b174319b501019b015a61b00105782a4e2a7615741305313a14422a4a079c01519001c9019c013388017352291c371c4110497e26442a8a0108502a2e2e19100980011984010b0e01840136042e1a6a22781f32356813280104370a5b12a5012b533e0748244e3e54355c064a45880115289d01103904453e810127290b5103a70152174841680b10254814402a4e305e82017232581792010522512e2516370023380b9801125434584c2a3e5202042f4e390f2f0e050205001f0801380d00430515421744fd01311b16614c038001241a482c3e44061e0a3881012605244d0e2d5d291a192c5710759d01284b20150f752308530a7f198101113d145d1f13534727290a291f490f4b0215246b196929752d2f2581012675371d432f090c4d2c0d080b141f0a0034051401110735152921055940010a023c0c0c35030519270825382f104512753e014001ae013b041708356ced012a0f7c2d041d0415631507e501012f0a491327411d1b310811072947493d0843125f4b7b16'),
                'SRID=3347;POLYGON((3658201 658873,3658201 5958872.97428571,8958201.49428571 5958872.97428571,8958201.49428571 658873,3658201 658873))'::geometry,
                4096,
                0,
                true
-               )));
+               ));
 
 
 -- Feature id encoding tests
index 7a8386fe785966efdfd48ad00d584230696b3208..52f9d972b3d8634a20d30926a839ab9756ce5463 100644 (file)
@@ -48,6 +48,7 @@ PG43 - ON |MULTIPOLYGON(((5 5,0 0,10 0,5 5)),((0 10,5 5,10 10,0 10)))
 PG43 - OFF|MULTIPOLYGON(((5 5,-1 -1,11 -1,5 5)),((5 5,11 11,-1 11,5 5)))
 PG44|
 PG45|
+PG46|SRID=3857;MULTIPOLYGON(((3245 2224,3262 2030,3253 2158,3245 2224)))
 TG1|GiEKBHRlc3QSDBICAAAYASIECTLePxoCYzEiAigBKIAgeAI=
 TG2|GiMKBHRlc3QSDhICAAAYASIGETLePwIBGgJjMSICKAEogCB4Ag==
 TG3|GiYKBHRlc3QSERICAAAYAiIJCQCAQArQD88PGgJjMSICKAEogCB4Ag==
@@ -97,7 +98,7 @@ D7|POINT(1 4094)
 TU2
 ERROR:  pgis_asmvt_transfn: parameter row cannot be other than a rowtype
 TU3|
-#3922|91
+#3922|MULTIPOLYGON(((2613 3664,2615 3662,2616 3662,2617 3665,2615 3665,2615 3664,2613 3664)))
 FI1|GicKBHRlc3QSDggBEgIAABgBIgQJMt4/GgJjMiIGCgRhYmNkKIAgeAI=
 FI2|GicKBHRlc3QSDggBEgIAABgBIgQJMt4/GgJjMiIGCgRhYmNkKIAgeAI=
 FI3|GicKBHRlc3QSDggBEgIAABgBIgQJMt4/GgJjMiIGCgRhYmNkKIAgeAI=