PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ projFileCreate()

static int projFileCreate ( SHPDUMPERSTATE state)
static

Creates ESRI .prj file for this shp output It looks in the spatial_ref_sys table and outputs the srtext field for this data If data is a table will use geometry_columns, if a query or view will read SRID from query output.

Warning
Will give warning and not output a .prj file if SRID is -1, Unknown, mixed SRIDS or not found in spatial_ref_sys. The dbf and shp will still be output.

make our address space large enough to hold query with table/schema

Definition at line 716 of file pgsql2shp-core.c.

717 {
718  FILE *fp;
719  char *pszFullname, *pszBasename;
720  int i;
721 
722  char *pszFilename = state->shp_file;
723  char *schema = state->schema;
724  char *table = state->table;
725  char *geo_col_name = state->geo_col_name;
726 
727  char *srtext;
728  char *query;
729  char esc_schema[1024];
730  char esc_table[1024];
731  char esc_geo_col_name[1024];
732 
733  int error, result;
734  PGresult *res;
735 
736  /***********
737  *** I'm multiplying by 2 instead of 3 because I am too lazy to figure out how many characters to add
738  *** after escaping if any **/
739  PQescapeStringConn(state->conn, esc_table, table, strlen(table), &error);
740  PQescapeStringConn(state->conn, esc_geo_col_name, geo_col_name, strlen(geo_col_name), &error);
741 
744  /**************************************************
745  * Get what kind of spatial ref is the selected geometry field
746  * We first check the geometry_columns table for a match and then if no match do a distinct against the table
747  * NOTE: COALESCE does a short-circuit check returning the faster query result and skipping the second if first returns something
748  * Escaping quotes in the schema and table in query may not be necessary except to prevent malicious attacks
749  * or should someone be crazy enough to have quotes or other weird character in their table, column or schema names
750  **************************************************/
751  if (schema)
752  {
753  PQescapeStringConn(state->conn, esc_schema, schema, strlen(schema), &error);
754  query = core_asprintf(
755  "SELECT COALESCE((SELECT sr.srtext "
756  " FROM geometry_columns As gc INNER JOIN spatial_ref_sys sr ON sr.srid = gc.srid "
757  " WHERE gc.f_table_schema = '%s' AND gc.f_table_name = '%s' AND gc.f_geometry_column = '%s' LIMIT 1), "
758  " (SELECT CASE WHEN COUNT(DISTINCT sr.srid) > 1 THEN 'm' ELSE MAX(sr.srtext) END As srtext "
759  " FROM \"%s\".\"%s\" As g INNER JOIN spatial_ref_sys sr ON sr.srid = ST_SRID((g.\"%s\")::geometry)) , ' ') As srtext ",
760  esc_schema, esc_table, esc_geo_col_name, schema, table, geo_col_name);
761  }
762  else
763  {
764  query = core_asprintf(
765  "SELECT COALESCE((SELECT sr.srtext "
766  " FROM geometry_columns As gc INNER JOIN spatial_ref_sys sr ON sr.srid = gc.srid "
767  " WHERE gc.f_table_name = '%s' AND gc.f_geometry_column = '%s' AND pg_table_is_visible((gc.f_table_schema || '.' || gc.f_table_name)::regclass) LIMIT 1), "
768  " (SELECT CASE WHEN COUNT(DISTINCT sr.srid) > 1 THEN 'm' ELSE MAX(sr.srtext) END as srtext "
769  " FROM \"%s\" As g INNER JOIN spatial_ref_sys sr ON sr.srid = ST_SRID((g.\"%s\")::geometry)), ' ') As srtext ",
770  esc_table, esc_geo_col_name, table, geo_col_name);
771  }
772 
773  LWDEBUGF(3,"%s\n", query);
774 
775  res = PQexec(state->conn, query);
776 
777  if ( ! res || PQresultStatus(res) != PGRES_TUPLES_OK )
778  {
779  snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Could not execute prj query: %s"), PQresultErrorMessage(res));
780  PQclear(res);
781  free(query);
782  return SHPDUMPERWARN;
783  }
784 
785  for (i=0; i < PQntuples(res); i++)
786  {
787  srtext = PQgetvalue(res, i, 0);
788  if (strcmp(srtext,"m") == 0)
789  {
790  snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Mixed set of spatial references. No prj file will be generated"));
791  PQclear(res);
792  free(query);
793  return SHPDUMPERWARN;
794  }
795  else
796  {
797  if (srtext[0] == ' ')
798  {
799  snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Cannot determine spatial reference (empty table or unknown spatial ref). No prj file will be generated."));
800  PQclear(res);
801  free(query);
802  return SHPDUMPERWARN;
803  }
804  else
805  {
806  /* -------------------------------------------------------------------- */
807  /* Compute the base (layer) name. If there is any extension */
808  /* on the passed in filename we will strip it off. */
809  /* -------------------------------------------------------------------- */
810  pszBasename = (char *) malloc(strlen(pszFilename)+5);
811  strcpy( pszBasename, pszFilename );
812  for ( i = strlen(pszBasename)-1;
813  i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
814  && pszBasename[i] != '\\';
815  i-- ) {}
816 
817  if ( pszBasename[i] == '.' )
818  pszBasename[i] = '\0';
819 
820  pszFullname = core_asprintf("%s.prj", pszBasename);
821  free( pszBasename );
822 
823 
824  /* -------------------------------------------------------------------- */
825  /* Create the file. */
826  /* -------------------------------------------------------------------- */
827  fp = fopen( pszFullname, "wb" );
828  if ( fp == NULL )
829  {
830  free(pszFullname);
831  free(query);
832  return 0;
833  }
834  else
835  {
836  result = fputs (srtext,fp);
837  LWDEBUGF(3, "\n result %d proj SRText is %s .\n", result, srtext);
838  if (result == EOF)
839  {
840  fclose( fp );
841  free( pszFullname );
842  PQclear(res);
843  free(query);
844  return 0;
845  }
846  }
847  fclose( fp );
848  free( pszFullname );
849  }
850  }
851  }
852  PQclear(res);
853  free(query);
854  return SHPDUMPEROK;
855 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
void * malloc(YYSIZE_T)
void free(void *)
tuple res
Definition: window.py:79
static char * core_asprintf(const char *format,...)
#define SHPDUMPERMSGLEN
#define SHPDUMPEROK
#define SHPDUMPERWARN
#define _(String)
Definition: shpcommon.h:24
char message[SHPDUMPERMSGLEN]

References _, shp_dumper_state::conn, core_asprintf(), free(), shp_dumper_state::geo_col_name, LWDEBUGF, malloc(), shp_dumper_state::message, window::res, result, shp_dumper_state::schema, shp_dumper_state::shp_file, SHPDUMPERMSGLEN, SHPDUMPEROK, SHPDUMPERWARN, and shp_dumper_state::table.

Referenced by ShpDumperCloseTable().

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