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.
704 char *pszFullname, *pszBasename;
707 char *pszFilename = state->
shp_file;
708 char *schema = state->
schema;
709 char *table = state->
table;
716 char *esc_geo_col_name;
728 size += 3 * strlen(schema);
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);
737 query = (
char *)
malloc(size);
738 if ( ! query )
return 0;
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);
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);
771 free(esc_geo_col_name);
773 res = PQexec(state->
conn, query);
775 if ( !
res || PQresultStatus(
res) != PGRES_TUPLES_OK )
783 for (i=0; i < PQntuples(
res); i++)
785 srtext = PQgetvalue(
res, i, 0);
786 if (strcmp(srtext,
"m") == 0)
788 snprintf(state->
message,
SHPDUMPERMSGLEN,
_(
"WARNING: Mixed set of spatial references. No prj file will be generated"));
795 if (srtext[0] ==
' ')
797 snprintf(state->
message,
SHPDUMPERMSGLEN,
_(
"WARNING: Cannot determine spatial reference (empty table or unknown spatial ref). No prj file will be generated."));
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] !=
'\\';
815 if ( pszBasename[i] ==
'.' )
816 pszBasename[i] =
'\0';
818 pszFullname = (
char *)
malloc(strlen(pszBasename) + 5);
819 sprintf( pszFullname,
"%s.prj", pszBasename );
826 fp = fopen( pszFullname,
"wb" );
835 result = fputs (srtext,fp);
836 LWDEBUGF(3,
"\n result %d proj SRText is %s .\n", result, srtext);
#define LWDEBUGF(level, msg,...)
char message[SHPDUMPERMSGLEN]