PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ ShpLoaderOpenShape()

int ShpLoaderOpenShape ( SHPLOADERSTATE state)

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

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

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(), FTDate, FTDouble, FTInteger, FTInvalid, FTLogical, FTString, 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, tagSHPObject::nVertices, shp_loader_state::pgdims, shp_loader_state::pgfieldtypes, shp_loader_state::pgtype, POINTTYPE, POLICY_NULL_ABORT, shp_loader_state::precisions, DBFInfo::pszCodePage, 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, and shp_loader_state::widths.

Referenced by pgui_action_import(), and validate_remote_loader_columns().

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