887{
888 GEOSBufferParams *bufferparams;
889 GEOSGeometry *g1, *g3 = NULL;
892 int quadsegs = 8;
893 int singleside = 0;
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
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
929 {
932 0, 0));
934 }
935
937
939 {
940 lwpgerror("Geometry contains invalid coordinates");
941 PG_RETURN_NULL();
942 }
944
946
948 if (!g1)
950
951
952 if (VARSIZE_ANY_EXHDR(params_text) > 0)
953 {
954 char *param;
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
979
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
1034 mitreLimit = atof(val);
1035 }
1036 else if ( !strcmp(key, "quad_segs") )
1037 {
1038
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);
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
1102
1103 POSTGIS_DEBUGF(3, "result: %s", GEOSGeomToWKT(g3));
1104
1106
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();
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)...
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
void lwgeom_geos_error(const char *fmt,...)
void lwgeom_free(LWGEOM *geom)
int lwgeom_isfinite(const LWGEOM *lwgeom)
Check if a LWGEOM has any non-finite (NaN or Inf) coordinates.
LWPOLY * lwpoly_construct_empty(int32_t srid, char hasz, char hasm)
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
GSERIALIZED * GEOS2POSTGIS(GEOSGeom geom, char want3d)
#define HANDLE_GEOS_ERROR(label)
GEOSGeometry * POSTGIS2GEOS(GSERIALIZED *pglwgeom)
char * text_to_cstring(const text *textptr)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)