PostGIS  3.1.6dev-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 702 of file pgsql2shp-core.c.

703 {
704  FILE *fp;
705  char *pszFullname, *pszBasename;
706  int i;
707 
708  char *pszFilename = state->shp_file;
709  char *schema = state->schema;
710  char *table = state->table;
711  char *geo_col_name = state->geo_col_name;
712 
713  char *srtext;
714  char *query;
715  char *esc_schema;
716  char *esc_table;
717  char *esc_geo_col_name;
718 
719  int error, result;
720  PGresult *res;
721  int size;
722 
723  /***********
724  *** I'm multiplying by 2 instead of 3 because I am too lazy to figure out how many characters to add
725  *** after escaping if any **/
726  size = 1000;
727  if ( schema )
728  {
729  size += 3 * strlen(schema);
730  }
731  size += 1000;
732  esc_table = (char *) malloc(3 * strlen(table) + 1);
733  esc_geo_col_name = (char *) malloc(3 * strlen(geo_col_name) + 1);
734  PQescapeStringConn(state->conn, esc_table, table, strlen(table), &error);
735  PQescapeStringConn(state->conn, esc_geo_col_name, geo_col_name, strlen(geo_col_name), &error);
736 
738  query = (char *) malloc(size);
739  if ( ! query ) return 0; /* out of virtual memory */
740 
741  /**************************************************
742  * Get what kind of spatial ref is the selected geometry field
743  * We first check the geometry_columns table for a match and then if no match do a distinct against the table
744  * NOTE: COALESCE does a short-circuit check returning the faster query result and skipping the second if first returns something
745  * Escaping quotes in the schema and table in query may not be necessary except to prevent malicious attacks
746  * or should someone be crazy enough to have quotes or other weird character in their table, column or schema names
747  **************************************************/
748  if ( schema )
749  {
750  esc_schema = (char *) malloc(2 * strlen(schema) + 1);
751  PQescapeStringConn(state->conn, esc_schema, schema, strlen(schema), &error);
752  sprintf(query, "SELECT COALESCE((SELECT sr.srtext "
753  " FROM geometry_columns As gc INNER JOIN spatial_ref_sys sr ON sr.srid = gc.srid "
754  " WHERE gc.f_table_schema = '%s' AND gc.f_table_name = '%s' AND gc.f_geometry_column = '%s' LIMIT 1), "
755  " (SELECT CASE WHEN COUNT(DISTINCT sr.srid) > 1 THEN 'm' ELSE MAX(sr.srtext) END As srtext "
756  " FROM \"%s\".\"%s\" As g INNER JOIN spatial_ref_sys sr ON sr.srid = ST_SRID((g.\"%s\")::geometry)) , ' ') As srtext ",
757  esc_schema, esc_table,esc_geo_col_name, schema, table, geo_col_name);
758  free(esc_schema);
759  }
760  else
761  {
762  sprintf(query, "SELECT COALESCE((SELECT sr.srtext "
763  " FROM geometry_columns As gc INNER JOIN spatial_ref_sys sr ON sr.srid = gc.srid "
764  " 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), "
765  " (SELECT CASE WHEN COUNT(DISTINCT sr.srid) > 1 THEN 'm' ELSE MAX(sr.srtext) END as srtext "
766  " FROM \"%s\" As g INNER JOIN spatial_ref_sys sr ON sr.srid = ST_SRID((g.\"%s\")::geometry)), ' ') As srtext ",
767  esc_table, esc_geo_col_name, table, geo_col_name);
768  }
769 
770  LWDEBUGF(3,"%s\n",query);
771  free(esc_table);
772  free(esc_geo_col_name);
773 
774  res = PQexec(state->conn, query);
775 
776  if ( ! res || PQresultStatus(res) != PGRES_TUPLES_OK )
777  {
778  snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Could not execute prj query: %s"), PQresultErrorMessage(res));
779  PQclear(res);
780  free(query);
781  return SHPDUMPERWARN;
782  }
783 
784  for (i=0; i < PQntuples(res); i++)
785  {
786  srtext = PQgetvalue(res, i, 0);
787  if (strcmp(srtext,"m") == 0)
788  {
789  snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Mixed set of spatial references. No prj file will be generated"));
790  PQclear(res);
791  free(query);
792  return SHPDUMPERWARN;
793  }
794  else
795  {
796  if (srtext[0] == ' ')
797  {
798  snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Cannot determine spatial reference (empty table or unknown spatial ref). No prj file will be generated."));
799  PQclear(res);
800  free(query);
801  return SHPDUMPERWARN;
802  }
803  else
804  {
805  /* -------------------------------------------------------------------- */
806  /* Compute the base (layer) name. If there is any extension */
807  /* on the passed in filename we will strip it off. */
808  /* -------------------------------------------------------------------- */
809  pszBasename = (char *) malloc(strlen(pszFilename)+5);
810  strcpy( pszBasename, pszFilename );
811  for ( i = strlen(pszBasename)-1;
812  i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
813  && pszBasename[i] != '\\';
814  i-- ) {}
815 
816  if ( pszBasename[i] == '.' )
817  pszBasename[i] = '\0';
818 
819  pszFullname = (char *) malloc(strlen(pszBasename) + 5);
820  sprintf( pszFullname, "%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
#define SHPDUMPERMSGLEN
#define SHPDUMPEROK
#define SHPDUMPERWARN
#define _(String)
Definition: shpcommon.h:24
char message[SHPDUMPERMSGLEN]

References _, shp_dumper_state::conn, 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: