PostGIS  2.2.7dev-r@@SVN_REVISION@@
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 705 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().

706 {
707  FILE *fp;
708  char *pszFullname, *pszBasename;
709  int i;
710 
711  char *pszFilename = state->shp_file;
712  char *schema = state->schema;
713  char *table = state->table;
714  char *geo_col_name = state->geo_col_name;
715 
716  char *srtext;
717  char *query;
718  char *esc_schema;
719  char *esc_table;
720  char *esc_geo_col_name;
721 
722  int error, result;
723  PGresult *res;
724  int size;
725 
726  /***********
727  *** I'm multiplying by 2 instead of 3 because I am too lazy to figure out how many characters to add
728  *** after escaping if any **/
729  size = 1000;
730  if ( schema )
731  {
732  size += 3 * strlen(schema);
733  }
734  size += 1000;
735  esc_table = (char *) malloc(3 * strlen(table) + 1);
736  esc_geo_col_name = (char *) malloc(3 * strlen(geo_col_name) + 1);
737  PQescapeStringConn(state->conn, esc_table, table, strlen(table), &error);
738  PQescapeStringConn(state->conn, esc_geo_col_name, geo_col_name, strlen(geo_col_name), &error);
739 
741  query = (char *) malloc(size);
742  if ( ! query ) return 0; /* out of virtual memory */
743 
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  esc_schema = (char *) malloc(2 * strlen(schema) + 1);
754  PQescapeStringConn(state->conn, esc_schema, schema, strlen(schema), &error);
755  sprintf(query, "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  free(esc_schema);
762  }
763  else
764  {
765  sprintf(query, "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  free(esc_table);
775  free(esc_geo_col_name);
776 
777  res = PQexec(state->conn, query);
778 
779  if ( ! res || PQresultStatus(res) != PGRES_TUPLES_OK )
780  {
781  snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Could not execute prj query: %s"), PQresultErrorMessage(res));
782  PQclear(res);
783  free(query);
784  return SHPDUMPERWARN;
785  }
786 
787  for (i=0; i < PQntuples(res); i++)
788  {
789  srtext = PQgetvalue(res, i, 0);
790  if (strcmp(srtext,"m") == 0)
791  {
792  snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Mixed set of spatial references. No prj file will be generated"));
793  PQclear(res);
794  free(query);
795  return SHPDUMPERWARN;
796  }
797  else
798  {
799  if (srtext[0] == ' ')
800  {
801  snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Cannot determine spatial reference (empty table or unknown spatial ref). No prj file will be generated."));
802  PQclear(res);
803  free(query);
804  return SHPDUMPERWARN;
805  }
806  else
807  {
808  /* -------------------------------------------------------------------- */
809  /* Compute the base (layer) name. If there is any extension */
810  /* on the passed in filename we will strip it off. */
811  /* -------------------------------------------------------------------- */
812  pszBasename = (char *) malloc(strlen(pszFilename)+5);
813  strcpy( pszBasename, pszFilename );
814  for ( i = strlen(pszBasename)-1;
815  i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
816  && pszBasename[i] != '\\';
817  i-- ) {}
818 
819  if ( pszBasename[i] == '.' )
820  pszBasename[i] = '\0';
821 
822  pszFullname = (char *) malloc(strlen(pszBasename) + 5);
823  sprintf( pszFullname, "%s.prj", pszBasename );
824  free( pszBasename );
825 
826 
827  /* -------------------------------------------------------------------- */
828  /* Create the file. */
829  /* -------------------------------------------------------------------- */
830  fp = fopen( pszFullname, "wb" );
831  if ( fp == NULL )
832  {
833  free(pszFullname);
834  free(query);
835  return 0;
836  }
837  else
838  {
839  result = fputs (srtext,fp);
840  LWDEBUGF(3, "\n result %d proj SRText is %s .\n", result, srtext);
841  if (result == EOF)
842  {
843  fclose( fp );
844  free( pszFullname );
845  PQclear(res);
846  free(query);
847  return 0;
848  }
849  }
850  fclose( fp );
851  free( pszFullname );
852  }
853  }
854  }
855  PQclear(res);
856  free(query);
857  return SHPDUMPEROK;
858 }
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:55

Here is the call graph for this function:

Here is the caller graph for this function: