PostGIS  3.0.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 701 of file pgsql2shp-core.c.

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