1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
#include "wkbtest.h"
/* example set-up
create table wkbreader_test (interesting bool,comments varchar(100), the_geom geometry);
insert into wkbreader_test values ( false, 'simple point', 'POINT( 10 10)' );
insert into wkbreader_test values ( false, 'simple line' , 'LINESTRING(0 0, 10 0, 10 10, 0 10, 0 0)');
insert into wkbreader_test values( true, 'polygon w/ hole','POLYGON( (0 0, 10 0, 10 10, 0 10, 0 0), (5 5, 7 5, 7 7 , 5 7 , 5 5))');
dont forget to set the port, dbname, host, and user in the
environment so the postgres connection will work
Also, change the "declare cursor" command so it returns the columns you want, and converts the geometry
into wkb.
*/
void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
void dump_bytes( char *a, int numb)
{
int t;
for (t=0; t<numb; t++)
{
printf(" + Byte #%i has value %i (%x)\n",t,a[t],a[t]);
}
}
// need to find the OID corresponding to the GEOMETRY type
//
// select OID from pg_type where typname = 'wkb';
int find_WKB_typeid(PGconn *conn)
{
PGresult *dbresult;
char *num;
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "no connection to db\n");
exit_nicely(conn);
}
dbresult = PQexec(conn, "select OID from pg_type where typname = 'bytea';");
if (PQresultStatus(dbresult) != PGRES_TUPLES_OK)
{
fprintf(stderr, "couldn't execute query to find oid of geometry type");
exit_nicely(conn); //command failed
}
if ( PQntuples(dbresult) != 1)
{
fprintf(stderr, "query to find oid of geometry didn't return 1 row!");
exit_nicely(conn);
}
num = PQgetvalue(dbresult, 0, 0); // first row, first field
PQclear(dbresult);
return ( atoi(num) );
}
main()
{
char *pghost,
*pgport,
*pgoptions,
*pgtty;
char *dbName;
int nFields;
int row,
field;
PGconn *conn;
PGresult *res;
int junk;
char *field_name;
int field_type;
int WKB_OID;
char conn_string[255];
bool *bool_val;
int *int_val;
float *float_val;
double *double_val;
char *char_val;
char *wkb_val;
char *table_name = "wkbreader_test";
char query_str[1000];
/* make a connection to the database */
conn = PQconnectdb("");
/*
* check to see that the backend connection was successfully made
*/
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
//what is the geometry type's OID #?
WKB_OID = find_WKB_typeid(conn);
/* start a transaction block */
res = PQexec(conn, "BEGIN");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "%s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
/*
* should PQclear PGresult whenever it is no longer needed to avoid
* memory leaks
*/
PQclear(res);
/*
* fetch rows from the pg_database, the system catalog of
* databases
*/
snprintf(query_str, sizeof(query_str), "DECLARE mycursor BINARY CURSOR FOR select text(num), asbinary(the_geom,'ndr') as wkb from %s", table_name);
printf(query_str);
printf("\n");
res = PQexec(conn, query_str);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "DECLARE CURSOR command failed\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
res = PQexec(conn, "FETCH ALL in mycursor");
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
for (row=0; row< PQntuples(res); row++)
{
printf("------------------------------row %i --------------------------\n",row);
//not so efficient, but...
for (field =0 ; field< PQnfields(res); field++)
{
field_type =PQftype(res,field);
field_name = PQfname(res, field);
//we just handle a few of the popular type since this is just an example
if (field_type ==16)// bool
{
bool_val = (bool *) PQgetvalue(res, row, field);
if (*bool_val)
printf("%s: TRUE\n",field_name);
else
printf("%s: FALSE\n",field_name);
}
else if (field_type ==23 )//int4 (int)
{
int_val = (int *) PQgetvalue(res, row, field);
printf("%s: %i\n",field_name,*int_val);
}
else if (field_type ==700 )//float4 (float)
{
float_val = (float *) PQgetvalue(res, row, field);
printf("%s: %g\n",field_name,*float_val);
}
else if (field_type ==701 )//float8 (double)
{
double_val = (double *) PQgetvalue(res, row, field);
printf("%s: %g\n",field_name,*double_val);
}
else if ( (field_type ==1043 ) || (field_type==25) )//varchar
{
char_val = (char *) PQgetvalue(res, row, field);
printf("%s: %s\n",field_name,char_val);
}
else if (field_type == WKB_OID ) //wkb
{
char_val = (char *) PQgetvalue(res, row, field);
printf("%s: ", field_name);
// skip 4 bytes varlena size
decode_wkb(char_val, &junk);
printf("\n");
}
}
}
PQclear(res);
/* close the cursor */
res = PQexec(conn, "CLOSE mycursor");
PQclear(res);
/* commit the transaction */
res = PQexec(conn, "COMMIT");
PQclear(res);
/* close the connection to the database and cleanup */
PQfinish(conn);
return 0;
}
|