PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum buffer ( PG_FUNCTION_ARGS  )

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

References geometry_serialize(), GEOS2POSTGIS(), gserialized_get_srid(), gserialized_has_z(), gserialized_is_empty(), lwerror(), lwgeom_geos_errmsg, lwgeom_geos_error(), lwnotice(), lwpoly_as_lwgeom(), lwpoly_construct_empty(), POSTGIS2GEOS(), POSTGIS_GEOS_VERSION, and result.

Referenced by lwcollection_homogenize(), and main().

1097 {
1098  GSERIALIZED *geom1;
1099  double size;
1100  GEOSGeometry *g1, *g3;
1102  int quadsegs = 8; /* the default */
1103  int nargs;
1104  enum
1105  {
1106  ENDCAP_ROUND = 1,
1107  ENDCAP_FLAT = 2,
1108  ENDCAP_SQUARE = 3
1109  };
1110  enum
1111  {
1112  JOIN_ROUND = 1,
1113  JOIN_MITRE = 2,
1114  JOIN_BEVEL = 3
1115  };
1116  static const double DEFAULT_MITRE_LIMIT = 5.0;
1117  static const int DEFAULT_ENDCAP_STYLE = ENDCAP_ROUND;
1118  static const int DEFAULT_JOIN_STYLE = JOIN_ROUND;
1119 
1120  double mitreLimit = DEFAULT_MITRE_LIMIT;
1121  int endCapStyle = DEFAULT_ENDCAP_STYLE;
1122  int joinStyle = DEFAULT_JOIN_STYLE;
1123  char *param;
1124  char *params = NULL;
1125  LWGEOM *lwg;
1126 
1127  geom1 = (GSERIALIZED *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1128  size = PG_GETARG_FLOAT8(1);
1129 
1130  /* Empty.Buffer() == Empty[polygon] */
1131  if ( gserialized_is_empty(geom1) )
1132  {
1134  gserialized_get_srid(geom1),
1135  0, 0)); // buffer wouldn't give back z or m anyway
1136  PG_RETURN_POINTER(geometry_serialize(lwg));
1137  }
1138 
1139  nargs = PG_NARGS();
1140 
1141  initGEOS(lwnotice, lwgeom_geos_error);
1142 
1143  g1 = (GEOSGeometry *)POSTGIS2GEOS(geom1);
1144  if ( 0 == g1 ) /* exception thrown at construction */
1145  {
1146  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
1147  PG_RETURN_NULL();
1148  }
1149 
1150  if (nargs > 2)
1151  {
1152  /* We strdup `cause we're going to modify it */
1153  params = pstrdup(PG_GETARG_CSTRING(2));
1154 
1155  POSTGIS_DEBUGF(3, "Params: %s", params);
1156 
1157  for (param=params; ; param=NULL)
1158  {
1159  char *key, *val;
1160  param = strtok(param, " ");
1161  if ( param == NULL ) break;
1162  POSTGIS_DEBUGF(3, "Param: %s", param);
1163 
1164  key = param;
1165  val = strchr(key, '=');
1166  if ( val == NULL || *(val+1) == '\0' )
1167  {
1168  lwerror("Missing value for buffer "
1169  "parameter %s", key);
1170  break;
1171  }
1172  *val = '\0';
1173  ++val;
1174 
1175  POSTGIS_DEBUGF(3, "Param: %s : %s", key, val);
1176 
1177  if ( !strcmp(key, "endcap") )
1178  {
1179  /* Supported end cap styles:
1180  * "round", "flat", "square"
1181  */
1182  if ( !strcmp(val, "round") )
1183  {
1184  endCapStyle = ENDCAP_ROUND;
1185  }
1186  else if ( !strcmp(val, "flat") ||
1187  !strcmp(val, "butt") )
1188  {
1189  endCapStyle = ENDCAP_FLAT;
1190  }
1191  else if ( !strcmp(val, "square") )
1192  {
1193  endCapStyle = ENDCAP_SQUARE;
1194  }
1195  else
1196  {
1197  lwerror("Invalid buffer end cap "
1198  "style: %s (accept: "
1199  "'round', 'flat', 'butt' "
1200  "or 'square'"
1201  ")", val);
1202  break;
1203  }
1204 
1205  }
1206  else if ( !strcmp(key, "join") )
1207  {
1208  if ( !strcmp(val, "round") )
1209  {
1210  joinStyle = JOIN_ROUND;
1211  }
1212  else if ( !strcmp(val, "mitre") ||
1213  !strcmp(val, "miter") )
1214  {
1215  joinStyle = JOIN_MITRE;
1216  }
1217  else if ( !strcmp(val, "bevel") )
1218  {
1219  joinStyle = JOIN_BEVEL;
1220  }
1221  else
1222  {
1223  lwerror("Invalid buffer end cap "
1224  "style: %s (accept: "
1225  "'round', 'mitre', 'miter' "
1226  " or 'bevel'"
1227  ")", val);
1228  break;
1229  }
1230  }
1231  else if ( !strcmp(key, "mitre_limit") ||
1232  !strcmp(key, "miter_limit") )
1233  {
1234  /* mitreLimit is a float */
1235  mitreLimit = atof(val);
1236  }
1237  else if ( !strcmp(key, "quad_segs") )
1238  {
1239  /* quadrant segments is an int */
1240  quadsegs = atoi(val);
1241  }
1242  else
1243  {
1244  lwerror("Invalid buffer parameter: %s (accept: "
1245  "'endcap', 'join', 'mitre_limit', "
1246  "'miter_limit and "
1247  "'quad_segs')", key);
1248  break;
1249  }
1250  }
1251 
1252  pfree(params); /* was pstrduped */
1253 
1254  POSTGIS_DEBUGF(3, "endCap:%d joinStyle:%d mitreLimit:%g",
1255  endCapStyle, joinStyle, mitreLimit);
1256 
1257  }
1258 
1259 #if POSTGIS_GEOS_VERSION >= 32
1260 
1261  g3 = GEOSBufferWithStyle(g1, size, quadsegs, endCapStyle, joinStyle, mitreLimit);
1262  GEOSGeom_destroy(g1);
1263 
1264 #else /* POSTGIS_GEOS_VERSION < 32 */
1265 
1266  if ( mitreLimit != DEFAULT_MITRE_LIMIT ||
1267  endCapStyle != DEFAULT_ENDCAP_STYLE ||
1268  joinStyle != DEFAULT_JOIN_STYLE )
1269  {
1270  lwerror("The GEOS version this PostGIS binary "
1271  "was compiled against (%d) doesn't support "
1272  "specifying a mitre limit != %d or styles different "
1273  "from 'round' (needs 3.2 or higher)",
1274  DEFAULT_MITRE_LIMIT, POSTGIS_GEOS_VERSION);
1275  }
1276 
1277  g3 = GEOSBuffer(g1,size,quadsegs);
1278  GEOSGeom_destroy(g1);
1279 
1280 #endif /* POSTGIS_GEOS_VERSION < 32 */
1281 
1282  if (g3 == NULL)
1283  {
1284  lwerror("GEOSBuffer: %s", lwgeom_geos_errmsg);
1285  PG_RETURN_NULL(); /* never get here */
1286  }
1287 
1288  POSTGIS_DEBUGF(3, "result: %s", GEOSGeomToWKT(g3));
1289 
1290  GEOSSetSRID(g3, gserialized_get_srid(geom1));
1291 
1292  result = GEOS2POSTGIS(g3, gserialized_has_z(geom1));
1293  GEOSGeom_destroy(g3);
1294 
1295  if (result == NULL)
1296  {
1297  elog(ERROR,"GEOS buffer() threw an error (result postgis geometry formation)!");
1298  PG_RETURN_NULL(); /* never get here */
1299  }
1300 
1301  PG_FREE_IF_COPY(geom1, 0);
1302  PG_RETURN_POINTER(result);
1303 }
#define POSTGIS_GEOS_VERSION
Definition: sqldefines.h:10
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:25
char ** result
Definition: liblwgeom.h:218
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:140
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:54
void lwgeom_geos_error(const char *fmt,...)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
GEOSGeometry * POSTGIS2GEOS(GSERIALIZED *pglwgeom)
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoly.c:66
GSERIALIZED * GEOS2POSTGIS(GEOSGeom geom, char want3d)
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:70

Here is the call graph for this function:

Here is the caller graph for this function: