16 #include "../postgis_config.h" 19 #include "../liblwgeom/liblwgeom.h" 20 #include "../liblwgeom/lwgeom_log.h" 43 #define UTF8_GOOD_RESULT 0 44 #define UTF8_BAD_RESULT 1 45 #define UTF8_NO_RESULT 2 60 utf8(
const char *fromcode,
char *inputbuf,
char **outputbuf)
67 inbytesleft = strlen(inputbuf);
69 cd = iconv_open(
"UTF-8", fromcode);
70 if ( cd == ((iconv_t)(-1)) )
73 outbytesleft = inbytesleft * 3 + 1;
75 *outputbuf = (
char *)
malloc(outbytesleft);
79 memset(*outputbuf, 0, outbytesleft);
80 outputptr = *outputbuf;
83 if ( iconv(cd, &inputbuf, &inbytesleft, &outputptr, &outbytesleft) == -1 )
88 iconvctl(cd, ICONV_SET_TRANSLITERATE, &on);
89 if ( iconv(cd, &inputbuf, &inbytesleft, &outputptr, &outbytesleft) == -1 )
92 iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, &on);
93 if ( iconv(cd, &inputbuf, &inbytesleft, &outputptr, &outbytesleft) == -1 )
140 if (*ptr ==
'\t' || *ptr ==
'\\' || *ptr ==
'\n' || *ptr ==
'\r')
150 size = ptr - str + toescape + 1;
151 result = calloc(1, size);
157 if ( *ptr ==
'\t' || *ptr ==
'\\' || *ptr ==
'\n' || *ptr ==
'\r' )
203 size = ptr - str + toescape + 1;
204 result = calloc(1, size);
261 point4d.
x = obj->
padfX[u];
262 point4d.
y = obj->
padfY[u];
265 point4d.
z = obj->
padfZ[u];
267 point4d.
m = obj->
padfM[u];
284 lwgeom = lwmultipoints[0];
321 LWGEOM **lwmultilinestrings;
325 int u, v, start_vertex, end_vertex;
344 for (u = 0; u < obj->
nParts; u++)
358 for (v = start_vertex; v < end_vertex; v++)
361 point4d.
x = obj->
padfX[v];
362 point4d.
y = obj->
padfY[v];
365 point4d.
z = obj->
padfZ[v];
367 point4d.
m = obj->
padfM[v];
383 lwgeom = lwmultilinestrings[0];
384 lwfree(lwmultilinestrings);
421 for (i = 0; i < n-1; i++)
423 if (((V[i].
y <= P.
y) && (V[i + 1].
y > P.
y))
424 || ((V[i].
y > P.
y) && (V[i + 1].
y <= P.
y)))
426 double vt = (float)(P.
y - V[i].
y) / (V[i + 1].
y - V[i].
y);
427 if (P.
x < V[i].
x + vt * (V[i + 1].
x - V[i].
x))
445 #if POSTGIS_DEBUG_LEVEL > 0 446 static int call = -1;
450 LWDEBUGF(4,
"FindPolygons[%d]: allocated space for %d rings\n", call, obj->
nParts);
457 for (pi=0; pi < obj->
nParts; pi++)
467 if (pi == obj->
nParts - 1)
485 for (vi = vs; vi < ve; vi++)
507 if (area < 0.0 || obj->nParts == 1)
509 Outer[out_index] = ring;
515 Inner[in_index] = ring;
520 LWDEBUGF(4,
"FindPolygons[%d]: found %d Outer, %d Inners\n", call, out_index, in_index);
524 for (pi = 0; pi < in_index; pi++)
528 Ring *inner = Inner[pi], *outer = NULL;
543 for (i = out_index - 1; i >= 0; i--)
547 in =
PIP(pt, Outer[i]->
list, Outer[i]->
n);
548 if ( in ||
PIP(pt2, Outer[i]->
list, Outer[i]->
n) )
567 LWDEBUGF(4,
"FindPolygons[%d]: hole %d is orphan\n", call, pi);
569 Outer[out_index] = inner;
591 for (pi = 0; pi < npolys; pi++)
619 int polygon_total, ring_total;
639 snprintf(state->
message,
SHPLOADERMSGLEN,
_(
"We have a Multipolygon with %d parts, can't use -S switch!"), polygon_total);
648 for (pi = 0; pi < polygon_total; pi++)
657 polyring = Outer[pi];
661 polyring = polyring->
next;
665 polyring = Outer[pi];
672 for (vi = 0; vi < polyring->
n; vi++)
675 point4d.
x = polyring->
list[vi].
x;
676 point4d.
y = polyring->
list[vi].
y;
679 point4d.
z = polyring->
list[vi].
z;
681 point4d.
m = polyring->
list[vi].
m;
689 polyring = polyring->
next;
704 lwgeom = lwpolygons[0];
746 for (j = 0; j < strlen(s); j++)
747 s[j] = tolower(s[j]);
756 config->
table = NULL;
845 int field_precision, field_width;
848 DBFFieldType
type = -1;
890 char *newencoding = NULL;
941 state->
pgtype =
"MULTILINESTRING";
948 state->
pgtype =
"MULTIPOLYGON";
955 state->
pgtype =
"MULTIPOINT";
972 state->
pgtype =
"MULTILINESTRINGM";
980 state->
pgtype =
"MULTIPOLYGONM";
988 state->
pgtype =
"MULTIPOINTM";
1003 state->
pgtype =
"MULTILINESTRING";
1012 state->
pgtype =
"MULTIPOLYGON";
1021 state->
pgtype =
"MULTIPOINT";
1029 state->
pgtype =
"GEOMETRY";
1112 state->
widths[j] = field_width;
1118 char *encoding_msg =
_(
"Try \"LATIN1\" (Western European), or one of the values described at http://www.gnu.org/software/libiconv/.");
1125 snprintf(state->
message,
SHPLOADERMSGLEN,
_(
"Unable to convert field name \"%s\" to UTF-8 (iconv reports \"%s\"). Current encoding is \"%s\". %s"), utf8str, strerror(errno), state->
config->
encoding, encoding_msg);
1127 snprintf(state->
message,
SHPLOADERMSGLEN,
_(
"Unable to convert field name to UTF-8 (iconv reports \"%s\"). Current encoding is \"%s\". %s"), strerror(errno), state->
config->
encoding, encoding_msg);
1137 strncpy(name, utf8str, MAXFIELDNAMELEN);
1147 strncpy(name, mapped, MAXFIELDNAMELEN);
1148 name[MAXFIELDNAMELEN-1] =
'\0';
1164 if (name[0] ==
'_' ||
1165 ! strcmp(name,
"gid") || ! strcmp(name,
"tableoid") ||
1166 ! strcmp(name,
"cmin") ||
1167 ! strcmp(name,
"cmax") ||
1168 ! strcmp(name,
"xmin") ||
1169 ! strcmp(name,
"xmax") ||
1170 ! strcmp(name,
"primary") ||
1171 ! strcmp(name,
"oid") || ! strcmp(name,
"ctid"))
1173 strncpy(name2 + 2, name, MAXFIELDNAMELEN - 2);
1176 strcpy(name, name2);
1180 for (z = 0; z < j ; z++)
1184 strncat(name,
"__", MAXFIELDNAMELEN);
1185 snprintf(name + strlen(name), MAXFIELDNAMELEN,
"%i", j);
1193 switch (state->
types[j])
1209 else if (state->
widths[j] >=10 && state->
widths[j] < 19)
1213 else if (state->
widths[j] < 5)
1225 fprintf(stderr,
"Field %s is an FTDouble with width %d and precision %d\n",
1227 if (state->
widths[j] > 18)
1368 if (state->
types[j] != FTDouble)
1483 sprintf(copystr,
"COPY \"%s\".\"%s\" %s FROM stdin;\n",
1493 *strheader = copystr;
1499 snprintf(state->
message,
SHPLOADERMSGLEN,
_(
"Internal error: attempt to generate a COPY statement for data that hasn't been requested in COPY format"));
1523 char *geometry=NULL, *ret;
1591 switch (state->
types[i])
1610 if (val[strlen(val) - 1] ==
'.')
1611 val[strlen(val) - 1] =
'\0';
1637 char *encoding_msg =
_(
"Try \"LATIN1\" (Western European), or one of the values described at http://www.postgresql.org/docs/current/static/multibyte.html.");
1644 snprintf(state->
message,
SHPLOADERMSGLEN,
_(
"Unable to convert data value \"%s\" to UTF-8 (iconv reports \"%s\"). Current encoding is \"%s\". %s"), utf8str, strerror(errno), state->
config->
encoding, encoding_msg);
1646 snprintf(state->
message,
SHPLOADERMSGLEN,
_(
"Unable to convert data value to UTF-8 (iconv reports \"%s\"). Current encoding is \"%s\". %s"), strerror(errno), state->
config->
encoding, encoding_msg);
1698 char *oldlocale = setlocale(LC_NUMERIC,
"C");
1793 setlocale(LC_NUMERIC, oldlocale);
char * lwgeom_to_hexwkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
void colmap_clean(colmap *map)
int ShpLoaderGetSQLFooter(SHPLOADERSTATE *state, char **strfooter)
char * column_map_filename
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
int colmap_read(const char *filename, colmap *map, char *errbuf, size_t errbuflen)
Read the content of filename into a symbol map.
int PIP(Point P, Point *V, int n)
PIP(): crossing number test for a point in a polygon input: P = a point, V[] = vertex points of a pol...
int ShpLoaderOpenShape(SHPLOADERSTATE *state)
#define POLICY_NULL_INSERT
#define SHPLOADERRECISNULL
Datum area(PG_FUNCTION_ARGS)
const char SHPAPI_CALL1 * DBFReadStringAttribute(DBFHandle psDBF, int iRecord, int iField){ return((const char *) DBFReadAttribute(psDBF, iRecord, iField, 'C')
char * codepage2encoding(const char *cpg)
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
void lwgeom_free(LWGEOM *geom)
#define SHPLOADERRECDELETED
void SHPAPI_CALL SHPDestroyObject(SHPObject *psObject)
int GeneratePointGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry, int force_multi)
Generate an allocated geometry string for shapefile object obj using the state parameters if "force_m...
int GeneratePolygonGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry)
Generate an allocated geometry string for shapefile object obj using the state parameters.
int GenerateLineStringGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry)
Generate an allocated geometry string for shapefile object obj using the state parameters.
int ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
int SHPAPI_CALL DBFGetRecordCount(DBFHandle psDBF)
void ShpLoaderDestroy(SHPLOADERSTATE *state)
#define GEOGRAPHY_DEFAULT
void SHPAPI_CALL SHPGetInfo(SHPHandle hSHP, int *pnEntities, int *pnShapeType, double *padfMinBound, double *padfMaxBound)
#define POLICY_NULL_ABORT
char * escape_insert_string(char *str)
Escape input string suitable for INSERT.
int stringbuffer_aprintf(stringbuffer_t *s, const char *fmt,...)
Appends a formatted string to the current string buffer, using the format and argument list provided...
void colmap_init(colmap *map)
SHPHandle SHPAPI_CALL SHPOpen(const char *pszShapeFile, const char *pszAccess)
DBFFieldType SHPAPI_CALL DBFGetFieldInfo(DBFHandle psDBF, int iField, char *pszFieldName, int *pnWidth, int *pnDecimals)
#define FLAGS_SET_Z(flags, value)
int SHPAPI_CALL DBFGetFieldCount(DBFHandle psDBF)
DBFHandle SHPAPI_CALL DBFOpen(const char *pszFilename, const char *pszAccess)
void set_loader_config_defaults(SHPLOADERCONFIG *config)
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
int ShpLoaderGetRecordCount(SHPLOADERSTATE *state)
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE, then a duplicate point will not be added.
void stringbuffer_clear(stringbuffer_t *s)
Reset the stringbuffer_t.
int FindPolygons(SHPObject *obj, Ring ***Out)
SHPObject SHPAPI_CALL1 * SHPReadObject(SHPHandle hSHP, int iShape);int SHPAPI_CALL SHPWriteObject(SHPHandle hSHP, int iShape, SHPObject *psObject
struct struct_point Point
#define LW_TRUE
Return types for functions with status returns.
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
char * escape_copy_string(char *str)
Escape input string suitable for COPY.
#define SRID_UNKNOWN
Unknown SRID value.
struct struct_ring * next
SHPLOADERSTATE * ShpLoaderCreate(SHPLOADERCONFIG *config)
#define FORCE_OUTPUT_DISABLE
char message[SHPLOADERMSGLEN]
void ReleasePolygons(Ring **polys, int npolys)
void stringbuffer_destroy(stringbuffer_t *s)
Free the stringbuffer_t and all memory managed within it.
static int utf8(const char *fromcode, char *inputbuf, char **outputbuf)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
void SHPAPI_CALL DBFClose(DBFHandle psDBF)
const char * colmap_pg_by_dbf(colmap *map, const char *dbfname)
const char * stringbuffer_getstring(stringbuffer_t *s)
Returns a reference to the internal string being managed by the stringbuffer.
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
int ShpLoaderGetSQLCopyStatement(SHPLOADERSTATE *state, char **strheader)
int lwpoly_add_ring(LWPOLY *poly, POINTARRAY *pa)
Add a ring, allocating extra space if necessary.
int ShpLoaderGenerateSQLRowStatement(SHPLOADERSTATE *state, int item, char **strrecord)
int SHPAPI_CALL DBFIsAttributeNULL(DBFHandle psDBF, int iRecord, int iField)
void SHPAPI_CALL SHPClose(SHPHandle hSHP)
LWPOINT * lwpoint_construct(int srid, GBOX *bbox, POINTARRAY *point)
#define LWDEBUGF(level, msg,...)
int SHPAPI_CALL DBFIsRecordDeleted(DBFHandle psDBF, int iShape)
#define FLAGS_SET_M(flags, value)
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)