[postgis-tickets] r15316 - Improved integer encoding for ST_AsMVT

bjorn at wololo.org bjorn at wololo.org
Sun Feb 26 08:42:56 PST 2017


Author: bjornharrtell
Date: 2017-02-26 08:42:56 -0800 (Sun, 26 Feb 2017)
New Revision: 15316

Modified:
   trunk/postgis/mvt.c
   trunk/regress/mvt.sql
   trunk/regress/mvt_expected
Log:
Improved integer encoding for ST_AsMVT
Closes #3714

Modified: trunk/postgis/mvt.c
===================================================================
--- trunk/postgis/mvt.c	2017-02-26 16:23:00 UTC (rev 15315)
+++ trunk/postgis/mvt.c	2017-02-26 16:42:56 UTC (rev 15316)
@@ -60,12 +60,6 @@
 	UT_hash_handle hh;
 };
 
-struct mvt_kv_int_value {
-	int64_t int_value;
-	uint32_t id;
-	UT_hash_handle hh;
-};
-
 struct mvt_kv_uint_value {
 	uint64_t uint_value;
 	uint32_t id;
@@ -405,8 +399,6 @@
 		float_values_hash, has_float_value, float_value);
 	MVT_CREATE_VALUES(mvt_kv_double_value,
 		double_values_hash, has_double_value, double_value);
-	MVT_CREATE_VALUES(mvt_kv_int_value,
-		int_values_hash, has_int_value, int_value);
 	MVT_CREATE_VALUES(mvt_kv_uint_value,
 		uint_values_hash, has_uint_value, uint_value);
 	MVT_CREATE_VALUES(mvt_kv_sint_value,
@@ -418,11 +410,9 @@
 	ctx->layer->values = values;
 }
 
-
-#define MVT_PARSE_VALUE(type, kvtype, hash, valuefield, datumfunc, size) \
+#define MVT_PARSE_VALUE(value, kvtype, hash, valuefield, size) \
 { \
 	struct kvtype *kv; \
-	type value = datumfunc(datum); \
 	HASH_FIND(hh, ctx->hash, &value, size, kv); \
 	if (!kv) { \
 		kv = palloc(sizeof(*kv)); \
@@ -434,6 +424,26 @@
 	tags[c*2+1] = kv->id; \
 }
 
+#define MVT_PARSE_DATUM(type, kvtype, hash, valuefield, datumfunc, size) \
+{ \
+	type value = datumfunc(datum); \
+	MVT_PARSE_VALUE(value, kvtype, hash, valuefield, size); \
+}
+
+#define MVT_PARSE_INT_DATUM(type, datumfunc) \
+{ \
+	type value = datumfunc(datum); \
+	if (value >= 0) { \
+		MVT_PARSE_VALUE(value, mvt_kv_uint_value, \
+				uint_values_hash, uint_value, \
+				sizeof(uint64_t)) \
+	} else { \
+		MVT_PARSE_VALUE(value, mvt_kv_sint_value, \
+				sint_values_hash, sint_value, \
+				sizeof(int64_t)) \
+	} \
+}
+
 static void parse_value_as_string(struct mvt_agg_context *ctx, Oid typoid,
 		Datum datum, uint32_t *tags, uint32_t c, uint32_t k) {
 	struct mvt_kv_string_value *kv;
@@ -472,37 +482,31 @@
 		Oid typoid = getBaseType(tupdesc->attrs[i]->atttypid);
 		switch (typoid) {
 		case BOOLOID:
-			MVT_PARSE_VALUE(bool, mvt_kv_bool_value,
+			MVT_PARSE_DATUM(bool, mvt_kv_bool_value,
 				bool_values_hash, bool_value,
 				DatumGetBool, sizeof(bool));
 			break;
 		case INT2OID:
-			MVT_PARSE_VALUE(int16_t, mvt_kv_int_value,
-				int_values_hash, int_value,
-				DatumGetInt16, sizeof(int64_t));
+			MVT_PARSE_INT_DATUM(int16_t, DatumGetInt16);
 			break;
 		case INT4OID:
-			MVT_PARSE_VALUE(int32_t, mvt_kv_int_value,
-				int_values_hash, int_value,
-				DatumGetInt32, sizeof(int64_t));
+			MVT_PARSE_INT_DATUM(int32_t, DatumGetInt32);
 			break;
 		case INT8OID:
-			MVT_PARSE_VALUE(int64_t, mvt_kv_int_value,
-				int_values_hash, int_value,
-				DatumGetInt64, sizeof(int64_t));
+			MVT_PARSE_INT_DATUM(int64_t, DatumGetInt64);
 			break;
 		case FLOAT4OID:
-			MVT_PARSE_VALUE(float, mvt_kv_float_value,
+			MVT_PARSE_DATUM(float, mvt_kv_float_value,
 				float_values_hash, float_value,
 				DatumGetFloat4, sizeof(float));
 			break;
 		case FLOAT8OID:
-			MVT_PARSE_VALUE(double, mvt_kv_double_value,
+			MVT_PARSE_DATUM(double, mvt_kv_double_value,
 				double_values_hash, double_value,
 				DatumGetFloat8, sizeof(double));
 			break;
 		case TEXTOID:
-			MVT_PARSE_VALUE(char *, mvt_kv_string_value,
+			MVT_PARSE_DATUM(char *, mvt_kv_string_value,
 				string_values_hash, string_value,
 				TextDatumGetCString, strlen(value));
 			break;

Modified: trunk/regress/mvt.sql
===================================================================
--- trunk/regress/mvt.sql	2017-02-26 16:23:00 UTC (rev 15315)
+++ trunk/regress/mvt.sql	2017-02-26 16:42:56 UTC (rev 15316)
@@ -58,6 +58,10 @@
 SELECT 'TA5', encode(ST_AsMVT('test',
     ST_MakeBox2D(ST_Point(0, 0), ST_Point(4096, 4096)), 4096, 0, false, 'geom', q
 ), 'base64') FROM (SELECT ST_GeomFromText('POINT(25 17)') AS geom, 1 AS c1, 'abcd'::text AS c2) AS q;
+SELECT 'TA6', encode(ST_AsMVT('test',
+    ST_MakeBox2D(ST_Point(0, 0), ST_Point(4096, 4096)), 4096, 0, false, 'geom', q
+), 'base64') FROM (SELECT 1 AS c1, -1 AS c2,
+    ST_GeomFromText('POINT(25 17)') AS geom) AS q;
 
 -- unsupported input
 SELECT 'TU1';

Modified: trunk/regress/mvt_expected
===================================================================
--- trunk/regress/mvt_expected	2017-02-26 16:23:00 UTC (rev 15315)
+++ trunk/regress/mvt_expected	2017-02-26 16:42:56 UTC (rev 15316)
@@ -1,18 +1,19 @@
-TG1|Gh4KBHRlc3QSDBICAAAYASIECTLePxoCYzEiAiABeAI=
-TG2|Gh4KBHRlc3QSDBICAAAYASIECTLePxoCYzEiAiABeAI=
-TG3|GiMKBHRlc3QSERICAAAYAiIJCQCAQArQD88PGgJjMSICIAF4Ag==
-TG4|GicKBHRlc3QSFRICAAAYAiINCQCAQBLoB+cH6AfnBxoCYzEiAiABeAI=
-TG5|GjUKBHRlc3QSIxICAAAYAiIbCQL+PxLoB+cH6AfnBwnND84PEugH5wfoB+cHGgJjMSICIAF4Ag==
-TG6|GjMKBHRlc3QSIRICAAAYAyIZCUbsPyIURTsKCSgyFA8JHScaHgkJHhMTDxoCYzEi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ICAAAYASIECTLePxIMEgIAARgBIgQJMt4/GgJjMSICKAEiAigCeAI=
+TA5|GiwKBHRlc3QSDhIEAAABARgBIgQJMt4/GgJjMRoCYzIiAigBIgYKBGFiY2R4Ag==
+TA6|GigKBHRlc3QSDhIEAAABARgBIgQJMt4/GgJjMRoCYzIiAigBIgIwAXgC
 TU1
 ERROR:  could not determine polymorphic type because input has type "unknown"
 TU2



More information about the postgis-tickets mailing list