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

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().

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