PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ ShpLoaderOpenShape()

int ShpLoaderOpenShape ( SHPLOADERSTATE state)

Definition at line 839 of file shp2pgsql-core.c.

References _, codepage2encoding(), shp_loader_state::col_names, COLLECTIONTYPE, colmap_pg_by_dbf(), colmap_read(), shp_loader_state::column_map, shp_loader_config::column_map_filename, shp_loader_state::config, DBFGetFieldCount(), DBFGetFieldInfo(), DBFGetRecordCount(), DBFOpen(), shp_loader_config::encoding, ENCODING_DEFAULT, shp_loader_state::field_names, shp_loader_config::force_output, FORCE_OUTPUT_2D, FORCE_OUTPUT_3DM, FORCE_OUTPUT_3DZ, FORCE_OUTPUT_4D, shp_loader_config::forceint4, free(), shp_loader_state::geo_col, shp_loader_state::has_m, shp_loader_state::has_z, shp_loader_state::hDBFHandle, shp_loader_state::hSHPHandle, lwfree(), malloc(), MAXFIELDNAMELEN, shp_loader_state::message, MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, shp_loader_config::null_policy, shp_loader_state::num_entities, shp_loader_state::num_fields, shp_loader_state::num_records, SHPObject::nVertices, shp_loader_state::pgdims, shp_loader_state::pgfieldtypes, shp_loader_state::pgtype, POINTTYPE, POLICY_NULL_ABORT, shp_loader_state::precisions, shp_loader_config::quoteidentifiers, shp_loader_config::readshape, shp_loader_config::shp_file, SHPDestroyObject(), shp_loader_state::shpfiletype, SHPGetInfo(), SHPLOADERERR, SHPLOADERMSGLEN, SHPLOADEROK, SHPLOADERWARN, SHPOpen(), SHPReadObject(), SHPT_ARC, SHPT_ARCM, SHPT_ARCZ, SHPT_MULTIPOINT, SHPT_MULTIPOINTM, SHPT_MULTIPOINTZ, SHPT_POINT, SHPT_POINTM, SHPT_POINTZ, SHPT_POLYGON, SHPT_POLYGONM, SHPT_POLYGONZ, shp_loader_config::simple_geometries, strtolower(), ovdump::type, shp_loader_state::types, utf8(), UTF8_BAD_RESULT, UTF8_GOOD_RESULT, UTF8_NO_RESULT, shp_loader_state::widths, and struct_point::z.

Referenced by main(), pgui_action_import(), and validate_remote_loader_columns().

840 {
841  SHPObject *obj = NULL;
842  int j, z;
843  int ret = SHPLOADEROK;
844 
845  int field_precision, field_width;
846  char name[MAXFIELDNAMELEN];
847  char name2[MAXFIELDNAMELEN];
848  DBFFieldType type = -1;
849  char *utf8str;
850 
851  /* If we are reading the entire shapefile, open it */
852  if (state->config->readshape == 1)
853  {
854  state->hSHPHandle = SHPOpen(state->config->shp_file, "rb");
855 
856  if (state->hSHPHandle == NULL)
857  {
858  snprintf(state->message, SHPLOADERMSGLEN, _("%s: shape (.shp) or index files (.shx) can not be opened, will just import attribute data."), state->config->shp_file);
859  state->config->readshape = 0;
860 
861  ret = SHPLOADERWARN;
862  }
863  }
864 
865  /* Open the DBF (attributes) file */
866  state->hDBFHandle = DBFOpen(state->config->shp_file, "rb");
867  if ((state->hSHPHandle == NULL && state->config->readshape == 1) || state->hDBFHandle == NULL)
868  {
869  snprintf(state->message, SHPLOADERMSGLEN, _("%s: dbf file (.dbf) can not be opened."), state->config->shp_file);
870 
871  return SHPLOADERERR;
872  }
873 
874 
875  /* Open the column map if one was specified */
876  if (state->config->column_map_filename)
877  {
878  ret = colmap_read(state->config->column_map_filename,
879  &state->column_map, state->message, SHPLOADERMSGLEN);
880  if (!ret) return SHPLOADERERR;
881  }
882 
883  /* User hasn't altered the default encoding preference... */
884  if ( strcmp(state->config->encoding, ENCODING_DEFAULT) == 0 )
885  {
886  /* But the file has a code page entry... */
887  if ( state->hDBFHandle->pszCodePage )
888  {
889  /* And we figured out what iconv encoding it maps to, so use it! */
890  char *newencoding = NULL;
891  if ( (newencoding = codepage2encoding(state->hDBFHandle->pszCodePage)) )
892  {
893  lwfree(state->config->encoding);
894  state->config->encoding = newencoding;
895  }
896  }
897  }
898 
899  /* If reading the whole shapefile (not just attributes)... */
900  if (state->config->readshape == 1)
901  {
902  SHPGetInfo(state->hSHPHandle, &state->num_entities, &state->shpfiletype, NULL, NULL);
903 
904  /* If null_policy is set to abort, check for NULLs */
905  if (state->config->null_policy == POLICY_NULL_ABORT)
906  {
907  /* If we abort on null items, scan the entire file for NULLs */
908  for (j = 0; j < state->num_entities; j++)
909  {
910  obj = SHPReadObject(state->hSHPHandle, j);
911 
912  if (!obj)
913  {
914  snprintf(state->message, SHPLOADERMSGLEN, _("Error reading shape object %d"), j);
915  return SHPLOADERERR;
916  }
917 
918  if (obj->nVertices == 0)
919  {
920  snprintf(state->message, SHPLOADERMSGLEN, _("Empty geometries found, aborted.)"));
921  return SHPLOADERERR;
922  }
923 
924  SHPDestroyObject(obj);
925  }
926  }
927 
928  /* Check the shapefile type */
929  int geomtype = 0;
930  switch (state->shpfiletype)
931  {
932  case SHPT_POINT:
933  /* Point */
934  state->pgtype = "POINT";
935  geomtype = POINTTYPE;
936  state->pgdims = 2;
937  break;
938 
939  case SHPT_ARC:
940  /* PolyLine */
941  state->pgtype = "MULTILINESTRING";
942  geomtype = MULTILINETYPE ;
943  state->pgdims = 2;
944  break;
945 
946  case SHPT_POLYGON:
947  /* Polygon */
948  state->pgtype = "MULTIPOLYGON";
949  geomtype = MULTIPOLYGONTYPE;
950  state->pgdims = 2;
951  break;
952 
953  case SHPT_MULTIPOINT:
954  /* MultiPoint */
955  state->pgtype = "MULTIPOINT";
956  geomtype = MULTIPOINTTYPE;
957  state->pgdims = 2;
958  break;
959 
960  case SHPT_POINTM:
961  /* PointM */
962  geomtype = POINTTYPE;
963  state->has_m = 1;
964  state->pgtype = "POINTM";
965  state->pgdims = 3;
966  break;
967 
968  case SHPT_ARCM:
969  /* PolyLineM */
970  geomtype = MULTILINETYPE;
971  state->has_m = 1;
972  state->pgtype = "MULTILINESTRINGM";
973  state->pgdims = 3;
974  break;
975 
976  case SHPT_POLYGONM:
977  /* PolygonM */
978  geomtype = MULTIPOLYGONTYPE;
979  state->has_m = 1;
980  state->pgtype = "MULTIPOLYGONM";
981  state->pgdims = 3;
982  break;
983 
984  case SHPT_MULTIPOINTM:
985  /* MultiPointM */
986  geomtype = MULTIPOINTTYPE;
987  state->has_m = 1;
988  state->pgtype = "MULTIPOINTM";
989  state->pgdims = 3;
990  break;
991 
992  case SHPT_POINTZ:
993  /* PointZ */
994  geomtype = POINTTYPE;
995  state->has_m = 1;
996  state->has_z = 1;
997  state->pgtype = "POINT";
998  state->pgdims = 4;
999  break;
1000 
1001  case SHPT_ARCZ:
1002  /* PolyLineZ */
1003  state->pgtype = "MULTILINESTRING";
1004  geomtype = MULTILINETYPE;
1005  state->has_z = 1;
1006  state->has_m = 1;
1007  state->pgdims = 4;
1008  break;
1009 
1010  case SHPT_POLYGONZ:
1011  /* MultiPolygonZ */
1012  state->pgtype = "MULTIPOLYGON";
1013  geomtype = MULTIPOLYGONTYPE;
1014  state->has_z = 1;
1015  state->has_m = 1;
1016  state->pgdims = 4;
1017  break;
1018 
1019  case SHPT_MULTIPOINTZ:
1020  /* MultiPointZ */
1021  state->pgtype = "MULTIPOINT";
1022  geomtype = MULTIPOINTTYPE;
1023  state->has_z = 1;
1024  state->has_m = 1;
1025  state->pgdims = 4;
1026  break;
1027 
1028  default:
1029  state->pgtype = "GEOMETRY";
1030  geomtype = COLLECTIONTYPE;
1031  state->has_z = 1;
1032  state->has_m = 1;
1033  state->pgdims = 4;
1034 
1035  snprintf(state->message, SHPLOADERMSGLEN, _("Unknown geometry type: %d\n"), state->shpfiletype);
1036  return SHPLOADERERR;
1037 
1038  break;
1039  }
1040 
1041  /* Force Z/M-handling if configured to do so */
1042  switch(state->config->force_output)
1043  {
1044  case FORCE_OUTPUT_2D:
1045  state->has_z = 0;
1046  state->has_m = 0;
1047  state->pgdims = 2;
1048  break;
1049 
1050  case FORCE_OUTPUT_3DZ:
1051  state->has_z = 1;
1052  state->has_m = 0;
1053  state->pgdims = 3;
1054  break;
1055 
1056  case FORCE_OUTPUT_3DM:
1057  state->has_z = 0;
1058  state->has_m = 1;
1059  state->pgdims = 3;
1060  break;
1061 
1062  case FORCE_OUTPUT_4D:
1063  state->has_z = 1;
1064  state->has_m = 1;
1065  state->pgdims = 4;
1066  break;
1067  default:
1068  /* Simply use the auto-detected values above */
1069  break;
1070  }
1071 
1072  /* If in simple geometry mode, alter names for CREATE TABLE by skipping MULTI */
1073  if (state->config->simple_geometries)
1074  {
1075  if ((geomtype == MULTIPOLYGONTYPE) || (geomtype == MULTILINETYPE) || (geomtype == MULTIPOINTTYPE))
1076  {
1077  /* Chop off the "MULTI" from the string. */
1078  state->pgtype += 5;
1079  }
1080  }
1081 
1082  }
1083  else
1084  {
1085  /* Otherwise just count the number of records in the DBF */
1086  state->num_entities = DBFGetRecordCount(state->hDBFHandle);
1087  }
1088 
1089 
1090  /* Get the field information from the DBF */
1091  state->num_fields = DBFGetFieldCount(state->hDBFHandle);
1092 
1093  state->num_records = DBFGetRecordCount(state->hDBFHandle);
1094 
1095  /* Allocate storage for field information */
1096  state->field_names = malloc(state->num_fields * sizeof(char*));
1097  state->types = (DBFFieldType *)malloc(state->num_fields * sizeof(int));
1098  state->widths = malloc(state->num_fields * sizeof(int));
1099  state->precisions = malloc(state->num_fields * sizeof(int));
1100  state->pgfieldtypes = malloc(state->num_fields * sizeof(char *));
1101  state->col_names = malloc((state->num_fields + 2) * sizeof(char) * MAXFIELDNAMELEN);
1102 
1103  /* Generate a string of comma separated column names of the form "(col1, col2 ... colN)" for the SQL
1104  insertion string */
1105  strcpy(state->col_names, "(" );
1106 
1107  for (j = 0; j < state->num_fields; j++)
1108  {
1109  type = DBFGetFieldInfo(state->hDBFHandle, j, name, &field_width, &field_precision);
1110 
1111  state->types[j] = type;
1112  state->widths[j] = field_width;
1113  state->precisions[j] = field_precision;
1114 /* fprintf(stderr, "XXX %s width:%d prec:%d\n", name, field_width, field_precision); */
1115 
1116  if (state->config->encoding)
1117  {
1118  char *encoding_msg = _("Try \"LATIN1\" (Western European), or one of the values described at http://www.gnu.org/software/libiconv/.");
1119 
1120  int rv = utf8(state->config->encoding, name, &utf8str);
1121 
1122  if (rv != UTF8_GOOD_RESULT)
1123  {
1124  if ( rv == UTF8_BAD_RESULT )
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);
1126  else if ( rv == UTF8_NO_RESULT )
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);
1128  else
1129  snprintf(state->message, SHPLOADERMSGLEN, _("Unexpected return value from utf8()"));
1130 
1131  if ( rv == UTF8_BAD_RESULT )
1132  free(utf8str);
1133 
1134  return SHPLOADERERR;
1135  }
1136 
1137  strncpy(name, utf8str, MAXFIELDNAMELEN);
1138  free(utf8str);
1139  }
1140 
1141  /* If a column map file has been passed in, use this to create the postgresql field name from
1142  the dbf column name */
1143  {
1144  const char *mapped = colmap_pg_by_dbf(&state->column_map, name);
1145  if (mapped)
1146  {
1147  strncpy(name, mapped, MAXFIELDNAMELEN);
1148  name[MAXFIELDNAMELEN-1] = '\0';
1149  }
1150  }
1151 
1152  /*
1153  * Make field names lowercase unless asked to
1154  * keep identifiers case.
1155  */
1156  if (!state->config->quoteidentifiers)
1157  strtolower(name);
1158 
1159  /*
1160  * Escape names starting with the
1161  * escape char (_), those named 'gid'
1162  * or after pgsql reserved attribute names
1163  */
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"))
1172  {
1173  strncpy(name2 + 2, name, MAXFIELDNAMELEN - 2);
1174  name2[0] = '_';
1175  name2[1] = '_';
1176  strcpy(name, name2);
1177  }
1178 
1179  /* Avoid duplicating field names */
1180  for (z = 0; z < j ; z++)
1181  {
1182  if (strcmp(state->field_names[z], name) == 0)
1183  {
1184  strncat(name, "__", MAXFIELDNAMELEN);
1185  snprintf(name + strlen(name), MAXFIELDNAMELEN, "%i", j);
1186  break;
1187  }
1188  }
1189 
1190  state->field_names[j] = strdup(name);
1191 
1192  /* Now generate the PostgreSQL type name string and width based upon the shapefile type */
1193  switch (state->types[j])
1194  {
1195  case FTString:
1196  state->pgfieldtypes[j] = strdup("varchar");
1197  break;
1198 
1199  case FTDate:
1200  state->pgfieldtypes[j] = strdup("date");
1201  break;
1202 
1203  case FTInteger:
1204  /* Determine exact type based upon field width */
1205  if (state->config->forceint4 || (state->widths[j] >=5 && state->widths[j] < 10))
1206  {
1207  state->pgfieldtypes[j] = strdup("int4");
1208  }
1209  else if (state->widths[j] >=10 && state->widths[j] < 19)
1210  {
1211  state->pgfieldtypes[j] = strdup("int8");
1212  }
1213  else if (state->widths[j] < 5)
1214  {
1215  state->pgfieldtypes[j] = strdup("int2");
1216  }
1217  else
1218  {
1219  state->pgfieldtypes[j] = strdup("numeric");
1220  }
1221  break;
1222 
1223  case FTDouble:
1224  /* Determine exact type based upon field width */
1225  fprintf(stderr, "Field %s is an FTDouble with width %d and precision %d\n",
1226  state->field_names[j], state->widths[j], state->precisions[j]);
1227  if (state->widths[j] > 18)
1228  {
1229  state->pgfieldtypes[j] = strdup("numeric");
1230  }
1231  else
1232  {
1233  state->pgfieldtypes[j] = strdup("float8");
1234  }
1235  break;
1236 
1237  case FTLogical:
1238  state->pgfieldtypes[j] = strdup("boolean");
1239  break;
1240 
1241  default:
1242  snprintf(state->message, SHPLOADERMSGLEN, _("Invalid type %x in DBF file"), state->types[j]);
1243  return SHPLOADERERR;
1244  }
1245 
1246  strcat(state->col_names, "\"");
1247  strcat(state->col_names, name);
1248 
1249  if (state->config->readshape == 1 || j < (state->num_fields - 1))
1250  {
1251  /* Don't include last comma if its the last field and no geometry field will follow */
1252  strcat(state->col_names, "\",");
1253  }
1254  else
1255  {
1256  strcat(state->col_names, "\"");
1257  }
1258  }
1259 
1260  /* Append the geometry column if required */
1261  if (state->config->readshape == 1)
1262  strcat(state->col_names, state->geo_col);
1263 
1264  strcat(state->col_names, ")");
1265 
1266 
1267  /* Return status */
1268  return ret;
1269 }
SHPLOADERCONFIG * config
#define SHPT_ARCM
Definition: shapefil.h:316
DBFFieldType * types
void lwfree(void *mem)
Definition: lwutil.c:244
#define FORCE_OUTPUT_3DM
#define SHPT_POLYGONM
Definition: shapefil.h:317
int colmap_read(const char *filename, colmap *map, char *errbuf, size_t errbuflen)
Read the content of filename into a symbol map.
Definition: shpcommon.c:213
#define FORCE_OUTPUT_3DZ
int nVertices
Definition: shapefil.h:348
#define _(String)
Definition: shpcommon.h:24
char * codepage2encoding(const char *cpg)
Definition: shpcommon.c:288
#define UTF8_BAD_RESULT
#define MULTIPOINTTYPE
Definition: liblwgeom.h:88
#define SHPLOADERWARN
#define SHPT_MULTIPOINT
Definition: shapefil.h:310
#define SHPT_POLYGON
Definition: shapefil.h:309
void SHPAPI_CALL SHPDestroyObject(SHPObject *psObject)
Definition: shpopen.c:2182
#define SHPT_MULTIPOINTZ
Definition: shapefil.h:314
DBFHandle hDBFHandle
#define UTF8_GOOD_RESULT
int SHPAPI_CALL DBFGetRecordCount(DBFHandle psDBF)
Definition: dbfopen.c:1208
#define SHPT_ARCZ
Definition: shapefil.h:312
void SHPAPI_CALL SHPGetInfo(SHPHandle hSHP, int *pnEntities, int *pnShapeType, double *padfMinBound, double *padfMaxBound)
Definition: shpopen.c:796
#define POLICY_NULL_ABORT
SHPHandle SHPAPI_CALL SHPOpen(const char *pszShapeFile, const char *pszAccess)
Definition: shpopen.c:464
DBFFieldType SHPAPI_CALL DBFGetFieldInfo(DBFHandle psDBF, int iField, char *pszFieldName, int *pnWidth, int *pnDecimals)
Definition: dbfopen.c:1221
#define MAXFIELDNAMELEN
int SHPAPI_CALL DBFGetFieldCount(DBFHandle psDBF)
Definition: dbfopen.c:1195
DBFHandle SHPAPI_CALL DBFOpen(const char *pszFilename, const char *pszAccess)
Definition: dbfopen.c:365
#define FORCE_OUTPUT_2D
SHPObject SHPAPI_CALL1 * SHPReadObject(SHPHandle hSHP, int iShape);int SHPAPI_CALL SHPWriteObject(SHPHandle hSHP, int iShape, SHPObject *psObject
#define SHPLOADEROK
#define UTF8_NO_RESULT
#define SHPT_MULTIPOINTM
Definition: shapefil.h:318
#define ENCODING_DEFAULT
#define FORCE_OUTPUT_4D
#define SHPT_POINTZ
Definition: shapefil.h:311
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
SHPHandle hSHPHandle
#define SHPT_POINTM
Definition: shapefil.h:315
#define SHPT_POLYGONZ
Definition: shapefil.h:313
char message[SHPLOADERMSGLEN]
#define SHPT_POINT
Definition: shapefil.h:307
static int utf8(const char *fromcode, char *inputbuf, char **outputbuf)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
const char * colmap_pg_by_dbf(colmap *map, const char *dbfname)
Definition: shpcommon.c:199
type
Definition: ovdump.py:41
void free(void *)
void * malloc(YYSIZE_T)
void strtolower(char *s)
#define SHPLOADERERR
#define MULTILINETYPE
Definition: liblwgeom.h:89
#define SHPT_ARC
Definition: shapefil.h:308
#define SHPLOADERMSGLEN
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
Here is the call graph for this function:
Here is the caller graph for this function: