PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ 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 723 of file pgsql2shp-core.c.

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