PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ buffer()

Datum buffer ( PG_FUNCTION_ARGS  )

Definition at line 886 of file postgis/lwgeom_geos.c.

887 {
888  GEOSBufferParams *bufferparams;
889  GEOSGeometry *g1, *g3 = NULL;
890  GSERIALIZED *result;
891  LWGEOM *lwg;
892  int quadsegs = 8; /* the default */
893  int singleside = 0; /* the default */
894  enum
895  {
896  ENDCAP_ROUND = 1,
897  ENDCAP_FLAT = 2,
898  ENDCAP_SQUARE = 3
899  };
900  enum
901  {
902  JOIN_ROUND = 1,
903  JOIN_MITRE = 2,
904  JOIN_BEVEL = 3
905  };
906  const double DEFAULT_MITRE_LIMIT = 5.0;
907  const int DEFAULT_ENDCAP_STYLE = ENDCAP_ROUND;
908  const int DEFAULT_JOIN_STYLE = JOIN_ROUND;
909  double mitreLimit = DEFAULT_MITRE_LIMIT;
910  int endCapStyle = DEFAULT_ENDCAP_STYLE;
911  int joinStyle = DEFAULT_JOIN_STYLE;
912 
913  GSERIALIZED *geom1 = PG_GETARG_GSERIALIZED_P(0);
914  double size = PG_GETARG_FLOAT8(1);
915  text *params_text;
916 
917  if (PG_NARGS() > 2)
918  {
919  params_text = PG_GETARG_TEXT_P(2);
920  }
921  else
922  {
923  params_text = palloc(VARHDRSZ);
924  SET_VARSIZE(params_text, 0);
925  }
926 
927  /* Empty.Buffer() == Empty[polygon] */
928  if ( gserialized_is_empty(geom1) )
929  {
931  gserialized_get_srid(geom1),
932  0, 0)); // buffer wouldn't give back z or m anyway
933  PG_RETURN_POINTER(geometry_serialize(lwg));
934  }
935 
936  lwg = lwgeom_from_gserialized(geom1);
937 
938  if (!lwgeom_isfinite(lwg))
939  {
940  lwpgerror("Geometry contains invalid coordinates");
941  PG_RETURN_NULL();
942  }
943  lwgeom_free(lwg);
944 
945  initGEOS(lwpgnotice, lwgeom_geos_error);
946 
947  g1 = POSTGIS2GEOS(geom1);
948  if (!g1)
949  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
950 
951 
952  if (VARSIZE_ANY_EXHDR(params_text) > 0)
953  {
954  char *param;
955  char *params = text_to_cstring(params_text);
956 
957  for (param=params; ; param=NULL)
958  {
959  char *key, *val;
960  param = strtok(param, " ");
961  if (!param) break;
962  POSTGIS_DEBUGF(3, "Param: %s", param);
963 
964  key = param;
965  val = strchr(key, '=');
966  if (!val || *(val + 1) == '\0')
967  {
968  lwpgerror("Missing value for buffer parameter %s", key);
969  break;
970  }
971  *val = '\0';
972  ++val;
973 
974  POSTGIS_DEBUGF(3, "Param: %s : %s", key, val);
975 
976  if ( !strcmp(key, "endcap") )
977  {
978  /* Supported end cap styles:
979  * "round", "flat", "square"
980  */
981  if ( !strcmp(val, "round") )
982  {
983  endCapStyle = ENDCAP_ROUND;
984  }
985  else if ( !strcmp(val, "flat") ||
986  !strcmp(val, "butt") )
987  {
988  endCapStyle = ENDCAP_FLAT;
989  }
990  else if ( !strcmp(val, "square") )
991  {
992  endCapStyle = ENDCAP_SQUARE;
993  }
994  else
995  {
996  lwpgerror("Invalid buffer end cap "
997  "style: %s (accept: "
998  "'round', 'flat', 'butt' "
999  "or 'square'"
1000  ")", val);
1001  break;
1002  }
1003 
1004  }
1005  else if ( !strcmp(key, "join") )
1006  {
1007  if ( !strcmp(val, "round") )
1008  {
1009  joinStyle = JOIN_ROUND;
1010  }
1011  else if ( !strcmp(val, "mitre") ||
1012  !strcmp(val, "miter") )
1013  {
1014  joinStyle = JOIN_MITRE;
1015  }
1016  else if ( !strcmp(val, "bevel") )
1017  {
1018  joinStyle = JOIN_BEVEL;
1019  }
1020  else
1021  {
1022  lwpgerror("Invalid buffer end cap "
1023  "style: %s (accept: "
1024  "'round', 'mitre', 'miter' "
1025  " or 'bevel'"
1026  ")", val);
1027  break;
1028  }
1029  }
1030  else if ( !strcmp(key, "mitre_limit") ||
1031  !strcmp(key, "miter_limit") )
1032  {
1033  /* mitreLimit is a float */
1034  mitreLimit = atof(val);
1035  }
1036  else if ( !strcmp(key, "quad_segs") )
1037  {
1038  /* quadrant segments is an int */
1039  quadsegs = atoi(val);
1040  }
1041  else if ( !strcmp(key, "side") )
1042  {
1043  if ( !strcmp(val, "both") )
1044  {
1045  singleside = 0;
1046  }
1047  else if ( !strcmp(val, "left") )
1048  {
1049  singleside = 1;
1050  }
1051  else if ( !strcmp(val, "right") )
1052  {
1053  singleside = 1;
1054  size *= -1;
1055  }
1056  else
1057  {
1058  lwpgerror("Invalid side parameter: %s (accept: 'right', 'left', 'both')", val);
1059  break;
1060  }
1061  }
1062  else
1063  {
1064  lwpgerror(
1065  "Invalid buffer parameter: %s (accept: 'endcap', 'join', 'mitre_limit', 'miter_limit', 'quad_segs' and 'side')",
1066  key);
1067  break;
1068  }
1069  }
1070  pfree(params); /* was pstrduped */
1071  }
1072 
1073 
1074  POSTGIS_DEBUGF(3, "endCap:%d joinStyle:%d mitreLimit:%g",
1075  endCapStyle, joinStyle, mitreLimit);
1076 
1077  bufferparams = GEOSBufferParams_create();
1078  if (bufferparams)
1079  {
1080  if (GEOSBufferParams_setEndCapStyle(bufferparams, endCapStyle) &&
1081  GEOSBufferParams_setJoinStyle(bufferparams, joinStyle) &&
1082  GEOSBufferParams_setMitreLimit(bufferparams, mitreLimit) &&
1083  GEOSBufferParams_setQuadrantSegments(bufferparams, quadsegs) &&
1084  GEOSBufferParams_setSingleSided(bufferparams, singleside))
1085  {
1086  g3 = GEOSBufferWithParams(g1, bufferparams, size);
1087  }
1088  else
1089  {
1090  lwpgerror("Error setting buffer parameters.");
1091  }
1092  GEOSBufferParams_destroy(bufferparams);
1093  }
1094  else
1095  {
1096  lwpgerror("Error setting buffer parameters.");
1097  }
1098 
1099  GEOSGeom_destroy(g1);
1100 
1101  if (!g3) HANDLE_GEOS_ERROR("GEOSBuffer");
1102 
1103  POSTGIS_DEBUGF(3, "result: %s", GEOSGeomToWKT(g3));
1104 
1105  GEOSSetSRID(g3, gserialized_get_srid(geom1));
1106 
1107  result = GEOS2POSTGIS(g3, gserialized_has_z(geom1));
1108  GEOSGeom_destroy(g3);
1109 
1110  if (!result)
1111  {
1112  elog(ERROR,"GEOS buffer() threw an error (result postgis geometry formation)!");
1113  PG_RETURN_NULL(); /* never get here */
1114  }
1115 
1116  PG_FREE_IF_COPY(geom1, 0);
1117  PG_RETURN_POINTER(result);
1118 }
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:152
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized.c:174
void lwgeom_geos_error(const char *fmt,...)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:311
int lwgeom_isfinite(const LWGEOM *lwgeom)
Check if a LWGEOM has any non-finite (NaN or Inf) coordinates.
Definition: lwgeom.c:2529
LWPOLY * lwpoly_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwpoly.c:161
#define HANDLE_GEOS_ERROR(label)
GEOSGeometry * POSTGIS2GEOS(GSERIALIZED *pglwgeom)
GSERIALIZED * GEOS2POSTGIS(GEOSGeom geom, char want3d)
char * text_to_cstring(const text *textptr)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)

References geometry_serialize(), GEOS2POSTGIS(), gserialized_get_srid(), gserialized_has_z(), gserialized_is_empty(), HANDLE_GEOS_ERROR, lwgeom_free(), lwgeom_from_gserialized(), lwgeom_geos_error(), lwgeom_isfinite(), lwpoly_as_lwgeom(), lwpoly_construct_empty(), POSTGIS2GEOS(), and text_to_cstring().

Referenced by do_test_s64_roundtrip(), do_test_u64_roundtrip(), encode_ptarray(), encode_ptarray_initial(), init_homogenizebuffer(), lwcollection_build_buffer(), lwcollection_homogenize(), mvt_clip_and_validate(), mvt_clip_and_validate_geos(), mvt_geom(), spatial_index_read_extent(), and ST_AsMVTGeom().

Here is the call graph for this function:
Here is the caller graph for this function: