Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

fmapsimportshapefile.c

Go to the documentation of this file.
00001 /* fmapsimportshapefile.c - Import ShapeFiles to FMaps
00002  *
00003  * Copyright (C) 2000  Franck Martin <franck@sopac.org>, Steffen Macke
00004  *
00005  * This Program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This Program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public
00016  * License along with this Program; if not, write to the
00017  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 /*
00022  * Modified by the FMaps Team and others 2000.  See the AUTHORS
00023  * file for a list of people on the FMaps Team.  See the ChangeLog
00024  * files for a list of changes.  These files are distributed with
00025  * FMaps at http://FMaps.sourceforge.net/.
00026  */
00027  
00028 #include <gnome.h>
00029 #include <math.h>
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 
00033 #include "config.h"
00034 #include "fmaps.h"
00035 
00036 #ifdef HAVE_SHAPELIB
00037 #include <shapefil.h>
00038 
00039 void 
00040 FM_Import_ShapeFile_Point(PGconn *conn, SHPHandle shphandle, 
00041                           gchar *tablename, int num_entities);      
00042 void 
00043 FM_Import_ShapeFile_Polyline(PGconn *conn, SHPHandle shphandle, 
00044                              gchar *tablename, int num_entities);
00045 void
00046 FM_Import_ShapeFile_Polygon(PGconn *conn, SHPHandle shphandle,
00047                             gchar *tablename, int num_entities);                             
00048 #endif /* shapelib */
00049 
00050 void FM_Import_ShapeFile(PGconn* conn,const char *name)
00051 {
00052 #ifdef HAVE_SHAPELIB
00053   gchar *buffer;
00054   gchar *buffer2;
00055   gchar *buffer3;
00056   gchar *tablename;
00057   gchar *oid;
00058   
00059   SHPHandle shphandle;
00060   int num_entities;
00061   int shapetype;
00062   double min_bound[4];
00063   double max_bound[4];
00064   
00065   PGresult *qryResult;
00066   
00067 #endif /* SHAPELIB */  
00068 
00069   g_return_if_fail (conn!=NULL);
00070   g_return_if_fail (name!=NULL);
00071 
00072 #ifdef HAVE_SHAPELIB
00073   gnome_app_flash (GNOME_APP(Fmaps), "Importing ShapeFile");
00074   
00075   buffer = g_strndup(name, strlen(name)-4);
00076   
00077   tablename = g_strdup(g_basename(buffer));
00078 
00079 g_warning("here");
00080 
00081   shphandle = SHPOpen(name, "rb");
00082   if(shphandle != NULL) {
00083     SHPGetInfo(shphandle, &num_entities, &shapetype, min_bound, max_bound);
00084     printf("n=%d, type=%d,%f,%f,%f,%f\n",num_entities, shapetype, min_bound[0],max_bound[0],min_bound[1],max_bound[1]);
00085 
00086     /* create the geographic table as it does not exists */
00087     /* drop it first for security */
00088     buffer2=g_strdup_printf("DROP TABLE f_geo_%s",tablename);
00089     qryResult = PQexec(conn,buffer2);
00090     PQclear(qryResult);
00091     g_free(buffer2);
00092         
00093     /* then create it */
00094     buffer2=g_strdup_printf("CREATE TABLE f_geo_%s %s",tablename,F_GEO_TABLE);
00095     qryResult = PQexec(conn,buffer2);
00096     g_warning("%s",buffer2);
00097     PQclear(qryResult);
00098     g_free(buffer2);
00099     
00100     switch(shapetype) {
00101       case SHPT_POINT : FM_Import_ShapeFile_Point(conn, shphandle, tablename, num_entities);
00102       break;
00103       case SHPT_ARC : FM_Import_ShapeFile_Polyline(conn, shphandle, tablename, num_entities);
00104             break;
00105             case SHPT_POLYGON : FM_Import_ShapeFile_Polygon(conn, shphandle, tablename, num_entities);
00106       break;
00107       default: 
00108       break;
00109     }
00110 
00111     /* close the Shapefile */
00112     SHPClose(shphandle);
00113     
00114         /*remove the reference in the metadata it will be added at the end*/
00115                 buffer2=g_strdup_printf("select OID FROM f_catalogue WHERE meta_file_id='%s'",tablename);
00116                 qryResult = PQexec(conn,buffer2);
00117                 nbtuples = PQntuples(qryResult);
00118     oid=NULL;
00119     if (nbtuples>0)
00120     {
00121                   oid = g_strdup(PQgetvalue(qryResult,0,0));
00122                  }
00123                 g_free(buffer2);
00124                 PQclear(qryResult);     
00125           
00126   
00127                 buffer2=g_strdup_printf("DELETE FROM f_catalogue WHERE meta_file_id='%s'",tablename);
00128                 qryResult = PQexec(conn,buffer2);
00129                 g_free(buffer2);
00130                 PQclear(qryResult);     
00131 
00132           if (!(oid==NULL))
00133           {
00134                 buffer2=g_strdup_printf("DELETE FROM f_catalogue_spatial_rep_type_code WHERE CID=%s",oid);
00135                 qryResult = PQexec(conn,buffer2);
00136                 g_free(buffer2);
00137                 PQclear(qryResult);             
00138           };    
00139 
00140                 g_free(oid);
00141                         
00142           /* reference the table in the metadata */
00143 
00144           buffer2=g_strdup_printf("INSERT INTO f_catalogue (meta_file_id,series) VALUES ('%s','coasts')",tablename);
00145           qryResult = PQexec(conn,buffer2);
00146           g_free(buffer2);
00147           PQclear(qryResult);
00148 
00149                 oid=g_strdup(PQoidStatus(qryResult));
00150   
00151           buffer2=g_strdup_printf("INSERT INTO f_catalogue_spatial_rep_type_code (CID,spatial_rep_type_code) VALUES (%s,2)",oid);
00152           qryResult = PQexec(conn,buffer2);
00153           g_free(buffer2);
00154           PQclear(qryResult);
00155     g_free(oid);
00156         
00157   }
00158   g_free(buffer);
00159   g_free(tablename); 
00160 
00161    
00162   gnome_app_flash (GNOME_APP(Fmaps), "Importing ShapeFile Completed");
00163 #else /* !SHAPELIB */
00164   gnome_app_flash (GNOME_APP(Fmaps), "Shapefile Import not configured");
00165 #endif
00166   return;
00167 }
00168 
00169 #ifdef HAVE_SHAPELIB
00170 
00171 /* Import a shapefile containing points */
00172 void 
00173 FM_Import_ShapeFile_Point(PGconn *conn, SHPHandle shphandle, 
00174                           gchar *tablename, int num_entities) {
00175   int i;
00176   SHPObject *shpobject;
00177   double x, y;
00178   gchar *buffer2;
00179   PGresult *qryResult;
00180  
00181   for(i=0; i<num_entities; i++) {
00182     shpobject = SHPReadObject(shphandle, i);
00183     if(shpobject->nSHPType != SHPT_NULL) {
00184       x = shpobject->padfX[0];
00185       y = shpobject->padfY[0];
00186       buffer2=g_strdup_printf("INSERT INTO f_geo_%s (%s) VALUES(%d, 1, 0, 0, 0, 0, 0, 0, NULL, NULL, 'POINT (%f, %f, 0)',NULL ,NULL )", tablename, F_GEO_TABLE_FIELD, i, x, y);
00187       qryResult = PQexec(conn,buffer2);
00188 
00189         
00190         /* debug */
00191       if (!(g_str_equal(PQresultErrorMessage(qryResult),"")))
00192       {
00193         g_warning("%s : %s",buffer2, PQresultErrorMessage(qryResult));
00194       }
00195       PQclear(qryResult);
00196       g_free(buffer2);
00197     }
00198   } 
00199 }
00200 
00201 /* Import a shapefile containing Polylines */
00202 void 
00203 FM_Import_ShapeFile_Polyline(PGconn *conn, SHPHandle shphandle, 
00204                              gchar *tablename, int num_entities) {
00205   int i;
00206   SHPObject *shpobject;
00207   double x, y;
00208   gchar *buffer2;
00209   gchar *buffer3;
00210   gchar *buffer4;
00211   PGresult *qryResult;
00212   int num_vertex;
00213  
00214   for(i=0; i<num_entities; i++) {
00215     shpobject = SHPReadObject(shphandle, i);
00216     if(shpobject->nSHPType != SHPT_NULL) {
00217 
00218       buffer4=g_strdup("");
00219       for (num_vertex = 0; num_vertex < shpobject->nVertices; num_vertex++)
00220       {
00221         x = shpobject->padfX[num_vertex];
00222               y = shpobject->padfY[num_vertex]; 
00223         buffer3=g_strdup_printf("%s (%f, %f, 0)",buffer4, x, y);
00224         g_free(buffer4);
00225         buffer4=g_strdup(buffer3);
00226         g_free(buffer3);
00227       }
00228 
00229       buffer2=g_strdup_printf("INSERT INTO f_geo_%s (%s) VALUES(%d, 1, 0, 0, 0, 0, 0, 0, NULL, NULL, 'POLYLINE%s', NULL, NULL)",tablename,F_GEO_TABLE_FIELD,i,buffer4);
00230       g_warning(buffer2);
00231         qryResult = PQexec(conn,buffer2);
00232 
00233         g_free(buffer4);
00234 
00235       qryResult = PQexec(conn,buffer2);
00236         
00237         /* debug */
00238       if (!(g_str_equal(PQresultErrorMessage(qryResult),"")))
00239       {
00240         g_warning("%s : %s",buffer2, PQresultErrorMessage(qryResult));
00241       }
00242       PQclear(qryResult);
00243       g_free(buffer2);
00244     }
00245   } 
00246 }
00247 
00248 /* Import a shapefile containing Polygons */
00249 /* Note: Currently imported as Polylines */
00250 void 
00251 FM_Import_ShapeFile_Polygon(PGconn *conn, SHPHandle shphandle, 
00252                              gchar *tablename, int num_entities) {
00253   int i;
00254   SHPObject *shpobject;
00255   double x, y;
00256   gchar *buffer2;
00257   gchar *buffer3;
00258   gchar *buffer4;
00259   PGresult *qryResult;
00260   int num_vertex;
00261  
00262   for(i=0; i<num_entities; i++) {
00263     shpobject = SHPReadObject(shphandle, i);
00264     if(shpobject->nSHPType != SHPT_NULL) {
00265 
00266       buffer4=g_strdup("");
00267       for (num_vertex = 0; num_vertex < shpobject->nVertices; num_vertex++)
00268       {
00269         x = shpobject->padfX[num_vertex];
00270               y = shpobject->padfY[num_vertex]; 
00271         buffer3=g_strdup_printf("%s (%f, %f, 0)",buffer4, x, y);
00272         g_free(buffer4);
00273         buffer4=g_strdup(buffer3);
00274         g_free(buffer3);
00275       }
00276 
00277       buffer2=g_strdup_printf("INSERT INTO f_geo_%s (%s) VALUES(%d, 1, 0, 0, 0, 0, 0, 0, NULL, NULL, 'POLYLINE%s', NULL, NULL)",tablename,F_GEO_TABLE_FIELD,i,buffer4);
00278       g_warning(buffer2);
00279         qryResult = PQexec(conn,buffer2);
00280 
00281         g_free(buffer4);
00282 
00283       qryResult = PQexec(conn,buffer2);
00284         
00285         /* debug */
00286       if (!(g_str_equal(PQresultErrorMessage(qryResult),"")))
00287       {
00288         g_warning("%s : %s",buffer2, PQresultErrorMessage(qryResult));
00289       }
00290       PQclear(qryResult);
00291       g_free(buffer2);
00292     }
00293   } 
00294 }
00295 
00296 #endif /* shapelib */

Generated at Sat Jan 6 20:55:33 2001 for FMaps by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000