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.
720 char *pszFullname, *pszBasename;
723 char *pszFilename = state->
shp_file;
724 char *schema = state->
schema;
725 char *table = state->
table;
732 char *esc_geo_col_name;
744 size += 3 * strlen(schema);
747 esc_table = (
char *)
malloc(3 * strlen(table) + 1);
748 esc_geo_col_name = (
char *)
malloc(3 * strlen(geo_col_name) + 1);
749 PQescapeStringConn(state->
conn, esc_table, table, strlen(table), &error);
750 PQescapeStringConn(state->
conn, esc_geo_col_name, geo_col_name, strlen(geo_col_name), &error);
753 query = (
char *)
malloc(size);
754 if ( ! query )
return 0;
765 esc_schema = (
char *)
malloc(2 * strlen(schema) + 1);
766 PQescapeStringConn(state->
conn, esc_schema, schema, strlen(schema), &error);
767 sprintf(query,
"SELECT COALESCE((SELECT sr.srtext " 768 " FROM geometry_columns As gc INNER JOIN spatial_ref_sys sr ON sr.srid = gc.srid " 769 " WHERE gc.f_table_schema = '%s' AND gc.f_table_name = '%s' AND gc.f_geometry_column = '%s' LIMIT 1), " 770 " (SELECT CASE WHEN COUNT(DISTINCT sr.srid) > 1 THEN 'm' ELSE MAX(sr.srtext) END As srtext " 771 " FROM \"%s\".\"%s\" As g INNER JOIN spatial_ref_sys sr ON sr.srid = ST_SRID((g.\"%s\")::geometry)) , ' ') As srtext ",
772 esc_schema, esc_table,esc_geo_col_name, schema, table, geo_col_name);
777 sprintf(query,
"SELECT COALESCE((SELECT sr.srtext " 778 " FROM geometry_columns As gc INNER JOIN spatial_ref_sys sr ON sr.srid = gc.srid " 779 " 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), " 780 " (SELECT CASE WHEN COUNT(DISTINCT sr.srid) > 1 THEN 'm' ELSE MAX(sr.srtext) END as srtext " 781 " FROM \"%s\" As g INNER JOIN spatial_ref_sys sr ON sr.srid = ST_SRID((g.\"%s\")::geometry)), ' ') As srtext ",
782 esc_table, esc_geo_col_name, table, geo_col_name);
787 free(esc_geo_col_name);
789 res = PQexec(state->
conn, query);
791 if ( ! res || PQresultStatus(res) != PGRES_TUPLES_OK )
793 snprintf(state->
message,
SHPDUMPERMSGLEN,
_(
"WARNING: Could not execute prj query: %s"), PQresultErrorMessage(res));
799 for (i=0; i < PQntuples(res); i++)
801 srtext = PQgetvalue(res, i, 0);
802 if (strcmp(srtext,
"m") == 0)
804 snprintf(state->
message,
SHPDUMPERMSGLEN,
_(
"WARNING: Mixed set of spatial references. No prj file will be generated"));
811 if (srtext[0] ==
' ')
813 snprintf(state->
message,
SHPDUMPERMSGLEN,
_(
"WARNING: Cannot determine spatial reference (empty table or unknown spatial ref). No prj file will be generated."));
824 pszBasename = (
char *)
malloc(strlen(pszFilename)+5);
825 strcpy( pszBasename, pszFilename );
826 for ( i = strlen(pszBasename)-1;
827 i > 0 && pszBasename[i] !=
'.' && pszBasename[i] !=
'/' 828 && pszBasename[i] !=
'\\';
831 if ( pszBasename[i] ==
'.' )
832 pszBasename[i] =
'\0';
834 pszFullname = (
char *)
malloc(strlen(pszBasename) + 5);
835 sprintf( pszFullname,
"%s.prj", pszBasename );
842 fp = fopen( pszFullname,
"wb" );
851 result = fputs (srtext,fp);
852 LWDEBUGF(3,
"\n result %d proj SRText is %s .\n", result, srtext);
char message[SHPDUMPERMSGLEN]
#define LWDEBUGF(level, msg,...)