write_pg.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  1. /*!
  2. \file lib/vector/Vlib/write_pg.c
  3. \brief Vector library - write vector feature (PostGIS format)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. Write subroutine inspired by OGR PostgreSQL driver.
  6. \todo PostGIS version of V2__delete_area_cats_from_cidx_nat()
  7. \todo function to delete corresponding entry in fidx
  8. \todo PostGIS version of V2__add_area_cats_to_cidx_nat
  9. (C) 2012-2014 by Martin Landa, and the GRASS Development Team
  10. This program is free software under the GNU General Public License
  11. (>=v2). Read the file COPYING that comes with GRASS for details.
  12. \author Martin Landa <landa.martin gmail.com>
  13. */
  14. #include <string.h>
  15. #include <grass/vector.h>
  16. #include <grass/glocale.h>
  17. #include "local_proto.h"
  18. #ifdef HAVE_POSTGRES
  19. #include "pg_local_proto.h"
  20. #define WKBSRIDFLAG 0x20000000
  21. #define TOPOGEOM_COLUMN "topo"
  22. /*! Use SQL statements from PostGIS Topology extension (this options
  23. is quite slow. By default are used simple SQL statements (INSERT, UPDATE)
  24. */
  25. #define USE_TOPO_STMT 0
  26. static int create_table(struct Format_info_pg *);
  27. static int check_schema(const struct Format_info_pg *);
  28. static int create_topo_schema(struct Format_info_pg *, int);
  29. static int create_pg_layer(struct Map_info *, int);
  30. static char *get_sftype(SF_FeatureType);
  31. static off_t write_line_sf(struct Map_info *, int,
  32. const struct line_pnts **, int,
  33. const struct line_cats *);
  34. static off_t write_line_tp(struct Map_info *, int, int,
  35. const struct line_pnts *,
  36. const struct line_cats *);
  37. static char *binary_to_hex(int, const unsigned char *);
  38. static unsigned char *point_to_wkb(int, const struct line_pnts *, int, int *);
  39. static unsigned char *linestring_to_wkb(int, const struct line_pnts *,
  40. int, int *);
  41. static unsigned char *polygon_to_wkb(int, const struct line_pnts **, int,
  42. int, int *);
  43. static char *line_to_wkb(struct Format_info_pg *, const struct line_pnts **,
  44. int, int, int);
  45. static int write_feature(struct Map_info *, int, int,
  46. const struct line_pnts **, int, int);
  47. static char *build_insert_stmt(const struct Format_info_pg *, const char *, int, int);
  48. static int insert_topo_element(struct Map_info *, int, int, const char *);
  49. static int update_next_edge(struct Map_info*, int, int);
  50. static int delete_face(const struct Map_info *, int);
  51. static int update_topo_edge(struct Map_info *, int);
  52. static int update_topo_face(struct Map_info *, int);
  53. static int add_line_to_topo_pg(struct Map_info *, off_t, int, const struct line_pnts *);
  54. static int delete_line_from_topo_pg(struct Map_info *, int, int, const struct line_pnts *);
  55. static int set_constraint_to_deferrable(struct Format_info_pg *, const char *, const char *,
  56. const char *, const char *, const char *);
  57. static dbDriver *open_db(struct Format_info_pg *);
  58. #endif
  59. static struct line_pnts *Points;
  60. /*!
  61. \brief Writes feature on level 1 (PostGIS interface)
  62. Notes for simple feature access:
  63. - centroids are not supported in PostGIS, pseudotopo holds virtual
  64. centroids
  65. - boundaries are not supported in PostGIS, pseudotopo treats polygons
  66. as boundaries
  67. Notes for PostGIS Topology access:
  68. - centroids are stored as isolated nodes
  69. - boundaries are stored as edges
  70. \param Map pointer to Map_info structure
  71. \param type feature type (GV_POINT, GV_LINE, ...)
  72. \param points pointer to line_pnts structure (feature geometry)
  73. \param cats pointer to line_cats structure (feature categories)
  74. \return feature offset into file
  75. \return -1 on error
  76. */
  77. off_t V1_write_line_pg(struct Map_info *Map, int type,
  78. const struct line_pnts *points,
  79. const struct line_cats *cats)
  80. {
  81. #ifdef HAVE_POSTGRES
  82. struct Format_info_pg *pg_info;
  83. pg_info = &(Map->fInfo.pg);
  84. if (pg_info->feature_type == SF_GEOMETRY) {
  85. /* create PostGIS table if doesn't exist */
  86. if (create_pg_layer(Map, type) < 0)
  87. return -1;
  88. }
  89. if (!points)
  90. return 0;
  91. if (!pg_info->toposchema_name) { /* simple features access */
  92. return write_line_sf(Map, type, &points, 1, cats);
  93. }
  94. /* PostGIS Topology access */
  95. return write_line_tp(Map, type, FALSE, points, cats);
  96. #else
  97. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  98. return -1;
  99. #endif
  100. }
  101. /*!
  102. \brief Writes feature on topological level (PostGIS interface)
  103. Calls V2_write_line_sfa() for simple features access.
  104. \param Map pointer to Map_info structure
  105. \param type feature type (GV_POINT, GV_LINE, ...)
  106. \param points pointer to line_pnts structure (feature geometry)
  107. \param cats pointer to line_cats structure (feature categories)
  108. \return feature offset into file
  109. \return -1 on error
  110. */
  111. off_t V2_write_line_pg(struct Map_info *Map, int type,
  112. const struct line_pnts *points,
  113. const struct line_cats *cats)
  114. {
  115. #ifdef HAVE_POSTGRES
  116. struct Format_info_pg *pg_info;
  117. pg_info = &(Map->fInfo.pg);
  118. if (!pg_info->toposchema_name) { /* pseudo-topology */
  119. return V2_write_line_sfa(Map, type, points, cats);
  120. }
  121. /* PostGIS Topology */
  122. return write_line_tp(Map, type, FALSE, points, cats);
  123. #else
  124. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  125. return -1;
  126. #endif
  127. }
  128. /*!
  129. \brief Rewrites feature at the given offset (level 1) (PostGIS interface, internal use only)
  130. Only for simple feature access. PostGIS Topology requires level 2.
  131. \todo Use UPDATE statement ?
  132. \param Map pointer to Map_info structure
  133. \param offset feature offset
  134. \param type feature type (GV_POINT, GV_LINE, ...)
  135. \param points feature geometry
  136. \param cats feature categories
  137. \return feature offset (rewritten feature)
  138. \return -1 on error
  139. */
  140. off_t V1_rewrite_line_pg(struct Map_info * Map,
  141. off_t offset, int type,
  142. const struct line_pnts * points,
  143. const struct line_cats * cats)
  144. {
  145. G_debug(3, "V1_rewrite_line_pg(): type=%d offset=%"PRI_OFF_T,
  146. type, offset);
  147. #ifdef HAVE_POSTGRES
  148. if (type != V1_read_line_pg(Map, NULL, NULL, offset)) {
  149. G_warning(_("Unable to rewrite feature (incompatible feature types)"));
  150. return -1;
  151. }
  152. /* delete old */
  153. V1_delete_line_pg(Map, offset);
  154. return V1_write_line_pg(Map, type, points, cats);
  155. #else
  156. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  157. return -1;
  158. #endif
  159. }
  160. /*!
  161. \brief Rewrites feature at topological level (PostGIS interface, internal use only)
  162. Note: Topology must be built at level >= GV_BUILD_BASE
  163. \todo Handle also categories
  164. \todo Store original geometry in tmp table for restore
  165. \param Map pointer to Map_info structure
  166. \param line feature id
  167. \param type feature type (GV_POINT, GV_LINE, ...)
  168. \param points feature geometry
  169. \param cats feature categories
  170. \return offset where feature was rewritten
  171. \return -1 on error
  172. */
  173. off_t V2_rewrite_line_pg(struct Map_info *Map, off_t line, int type,
  174. const struct line_pnts *points, const struct line_cats *cats)
  175. {
  176. G_debug(3, "V2_rewrite_line_pg(): line=%d type=%d",
  177. (int)line, type);
  178. #ifdef HAVE_POSTGRES
  179. const char *schema_name, *table_name, *keycolumn;
  180. char *stmt, *geom_data;
  181. struct Format_info_pg *pg_info;
  182. struct P_line *Line;
  183. off_t offset;
  184. geom_data = NULL;
  185. stmt = NULL;
  186. pg_info = &(Map->fInfo.pg);
  187. if (line < 1 || line > Map->plus.n_lines) {
  188. G_warning(_("Attempt to access feature with invalid id (%d)"), (int)line);
  189. return -1;
  190. }
  191. Line = Map->plus.Line[line];
  192. if (Line == NULL) {
  193. G_warning(_("Attempt to access dead feature %d"), (int)line);
  194. return -1;
  195. }
  196. offset = Line->offset;
  197. if (!(Map->plus.update_cidx)) {
  198. Map->plus.cidx_up_to_date = FALSE; /* category index will be outdated */
  199. }
  200. if (!Points)
  201. Points = Vect_new_line_struct();
  202. if (type != V2_read_line_pg(Map, Points, NULL, line)) {
  203. G_warning(_("Unable to rewrite feature (incompatible feature types)"));
  204. return -1;
  205. }
  206. /* remove line from topology */
  207. if (0 != delete_line_from_topo_pg(Map, line, type, Points))
  208. return -1;
  209. if (pg_info->toposchema_name) { /* PostGIS Topology */
  210. schema_name = pg_info->toposchema_name;
  211. if (type & GV_POINTS) {
  212. table_name = keycolumn = "node";
  213. }
  214. else {
  215. table_name = "edge_data";
  216. keycolumn = "edge";
  217. }
  218. }
  219. else { /* simple features access */
  220. schema_name = pg_info->schema_name;
  221. table_name = pg_info->table_name;
  222. keycolumn = pg_info->fid_column;
  223. }
  224. geom_data = line_to_wkb(pg_info, &points, 1, type, Map->head.with_z);
  225. G_asprintf(&stmt, "UPDATE \"%s\".\"%s\" SET geom = '%s'::GEOMETRY WHERE %s_id = %ld",
  226. schema_name, table_name, geom_data, keycolumn, line);
  227. G_free(geom_data);
  228. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  229. G_warning(_("Unable to rewrite feature %d"), (int)line);
  230. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  231. return -1;
  232. }
  233. /* update topology
  234. note: offset is not changed */
  235. return add_line_to_topo_pg(Map, offset, type, points);
  236. #else
  237. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  238. return -1;
  239. #endif
  240. }
  241. /*!
  242. \brief Deletes feature at the given offset (level 1)
  243. Only for simple feature access. PostGIS Topology requires level 2.
  244. \param Map pointer Map_info structure
  245. \param offset feature offset
  246. \return 0 on success
  247. \return -1 on error
  248. */
  249. int V1_delete_line_pg(struct Map_info *Map, off_t offset)
  250. {
  251. #ifdef HAVE_POSTGRES
  252. long fid;
  253. char stmt[DB_SQL_MAX];
  254. struct Format_info_pg *pg_info;
  255. pg_info = &(Map->fInfo.pg);
  256. if (!pg_info->conn || !pg_info->table_name) {
  257. G_warning(_("No connection defined"));
  258. return -1;
  259. }
  260. if (offset >= pg_info->offset.array_num) {
  261. G_warning(_("Invalid offset (%ld)"), offset);
  262. return -1;
  263. }
  264. fid = pg_info->offset.array[offset];
  265. G_debug(3, "V1_delete_line_pg(): offset = %lu -> fid = %ld",
  266. (unsigned long)offset, fid);
  267. if (!pg_info->inTransaction) {
  268. /* start transaction */
  269. pg_info->inTransaction = TRUE;
  270. if (Vect__execute_pg(pg_info->conn, "BEGIN") == -1)
  271. return -1;
  272. }
  273. sprintf(stmt, "DELETE FROM %s WHERE %s = %ld",
  274. pg_info->table_name, pg_info->fid_column, fid);
  275. G_debug(3, "SQL: %s", stmt);
  276. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  277. G_warning(_("Unable to delete feature"));
  278. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  279. return -1;
  280. }
  281. return 0;
  282. #else
  283. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  284. return -1;
  285. #endif
  286. }
  287. /*!
  288. \brief Deletes feature on topological level (PostGIS interface)
  289. Note: Topology must be built at level >= GV_BUILD_BASE
  290. Calls V2_delete_line_sfa() for simple feature access.
  291. \param Map pointer to Map_info structure
  292. \param line feature id to be deleted
  293. \return 0 on success
  294. \return -1 on error
  295. */
  296. int V2_delete_line_pg(struct Map_info *Map, off_t line)
  297. {
  298. #ifdef HAVE_POSTGRES
  299. int ret;
  300. struct Format_info_pg *pg_info;
  301. pg_info = &(Map->fInfo.pg);
  302. if (line < 1 || line > Map->plus.n_lines) {
  303. G_warning(_("Attempt to access feature with invalid id (%d)"), (int)line);
  304. return -1;
  305. }
  306. if (!pg_info->toposchema_name) { /* pseudo-topology */
  307. return V2_delete_line_sfa(Map, line);
  308. }
  309. else { /* PostGIS topology */
  310. int type;
  311. char stmt[DB_SQL_MAX];
  312. const char *table_name, *keycolumn;
  313. struct P_line *Line;
  314. if (line < 1 || line > Map->plus.n_lines) {
  315. G_warning(_("Attempt to access feature with invalid id (%d)"), (int)line);
  316. return -1;
  317. }
  318. Line = Map->plus.Line[line];
  319. if (!Line) {
  320. G_warning(_("Attempt to access dead feature %d"), (int)line);
  321. return -1;
  322. }
  323. if (!(Map->plus.update_cidx)) {
  324. Map->plus.cidx_up_to_date = FALSE; /* category index will be outdated */
  325. }
  326. Vect__execute_pg(pg_info->conn, "BEGIN");
  327. if (Line->type & GV_POINTS) {
  328. table_name = keycolumn = "node";
  329. }
  330. else {
  331. table_name = "edge_data";
  332. keycolumn = "edge";
  333. /* first remove references to this edge */
  334. /* (1) left next edge */
  335. sprintf(stmt, "UPDATE \"%s\".\"%s\" SET abs_next_left_edge = edge_id, "
  336. "next_left_edge = -edge_id WHERE abs_next_left_edge = %d",
  337. pg_info->toposchema_name, table_name, (int)Line->offset);
  338. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  339. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  340. return -1;
  341. }
  342. /* (2) right next edge */
  343. sprintf(stmt, "UPDATE \"%s\".\"%s\" SET abs_next_right_edge = edge_id, "
  344. "next_right_edge = edge_id WHERE abs_next_right_edge = %d",
  345. pg_info->toposchema_name, table_name, (int)Line->offset);
  346. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  347. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  348. return -1;
  349. }
  350. }
  351. /* read the line */
  352. if (!Points)
  353. Points = Vect_new_line_struct();
  354. type = V2_read_line_pg(Map, Points, NULL, line);
  355. if (type < 0)
  356. return -1;
  357. /* delete record from topology table */
  358. sprintf(stmt, "DELETE FROM \"%s\".\"%s\" WHERE %s_id = %d",
  359. pg_info->toposchema_name, table_name, keycolumn, (int)Line->offset);
  360. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  361. G_warning(_("Unable to delete feature (%s) %d"), keycolumn,
  362. (int)line);
  363. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  364. return -1;
  365. }
  366. if (pg_info->cache.ctype == CACHE_MAP) {
  367. /* delete from cache */
  368. Vect_destroy_line_struct(pg_info->cache.lines[line-1]);
  369. pg_info->cache.lines[line-1] = NULL;
  370. pg_info->cache.lines_types[line-1] = 0;
  371. pg_info->cache.lines_cats[line-1] = 0;
  372. }
  373. /* update topology */
  374. ret = delete_line_from_topo_pg(Map, line, type, Points);
  375. if (ret == 0)
  376. Vect__execute_pg(pg_info->conn, "COMMIT");
  377. return ret;
  378. }
  379. #else
  380. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  381. return -1;
  382. #endif
  383. }
  384. #ifdef HAVE_POSTGRES
  385. /*!
  386. \brief Writes node on topological level (PostGIS Topology
  387. interface, internal use only)
  388. The vector map must be open on level 2 at least with
  389. GV_BUILD_BASE. PostGIS Topology schema must be defined.
  390. \param Map pointer to Map_info structure
  391. \param node node id (starts at 1)
  392. \param points pointer to line_pnts structure
  393. \return 0 on success
  394. \return -1 on error
  395. */
  396. off_t V2__write_node_pg(struct Map_info *Map, const struct line_pnts *points)
  397. {
  398. struct Format_info_pg *pg_info;
  399. pg_info = &(Map->fInfo.pg);
  400. if (!pg_info->toposchema_name)
  401. return -1; /* PostGIS Topology required */
  402. return write_line_tp(Map, GV_POINT, TRUE, points, NULL);
  403. }
  404. /*!
  405. \brief Writes area on topological level (PostGIS Simple Features
  406. interface, internal use only)
  407. \param Map pointer to Map_info structure
  408. \param points feature geometry (exterior + interior rings)
  409. \param nparts number of parts including exterior ring
  410. \param cats feature categories
  411. \return feature offset
  412. \return -1 on error
  413. */
  414. off_t V2__write_area_pg(struct Map_info *Map,
  415. const struct line_pnts **points, int nparts,
  416. const struct line_cats *cats)
  417. {
  418. return write_line_sf(Map, GV_BOUNDARY, points, nparts, cats);
  419. }
  420. /*!
  421. \brief Updates simple features geometry from GRASS-like topo
  422. \param Map pointer to Map_info structure
  423. \param points feature geometry (exterior + interior rings)
  424. \param nparts number of parts including exterior ring
  425. \param cat area category
  426. \return 0 on success
  427. \return -1 on error
  428. */
  429. int V2__update_area_pg(struct Map_info *Map,
  430. const struct line_pnts **points, int nparts,
  431. int cat)
  432. {
  433. int part, npoints;
  434. char *stmt, *geom_data;
  435. struct Format_info_pg *pg_info;
  436. pg_info = &(Map->fInfo.pg);
  437. for (part = 0; part < nparts; part++) {
  438. npoints = points[part]->n_points - 1;
  439. if (points[part]->x[0] != points[part]->x[npoints] ||
  440. points[part]->y[0] != points[part]->y[npoints] ||
  441. points[part]->z[0] != points[part]->z[npoints]) {
  442. G_warning(_("Boundary is not closed. Skipping."));
  443. return -1;
  444. }
  445. }
  446. geom_data = line_to_wkb(pg_info, points, nparts, GV_AREA, Vect_is_3d(Map));
  447. if (!geom_data)
  448. return -1;
  449. stmt = NULL;
  450. G_asprintf(&stmt, "UPDATE \"%s\".\"%s\" SET %s = '%s'::GEOMETRY WHERE %s = %d",
  451. pg_info->schema_name, pg_info->table_name, pg_info->geom_column,
  452. geom_data, pg_info->fid_column,
  453. cat);
  454. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  455. /* rollback transaction */
  456. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  457. G_free(geom_data);
  458. G_free(stmt);
  459. return -1;
  460. }
  461. G_free(geom_data);
  462. G_free(stmt);
  463. return 0;
  464. }
  465. /*!
  466. \brief Create new feature table
  467. \param pg_info pointer to Format_info_pg
  468. \return -1 on error
  469. \return 0 on success
  470. */
  471. int create_table(struct Format_info_pg *pg_info)
  472. {
  473. int spatial_index, primary_key;
  474. char stmt[DB_SQL_MAX];
  475. char *geom_type, *def_file;
  476. struct field_info *Fi;
  477. PGresult *result;
  478. def_file = getenv("GRASS_VECTOR_PGFILE");
  479. /* by default create spatial index & add primary key */
  480. spatial_index = primary_key = TRUE;
  481. if (G_find_file2("", def_file ? def_file : "PG", G_mapset())) {
  482. FILE *fp;
  483. const char *p;
  484. struct Key_Value *key_val;
  485. fp = G_fopen_old("", def_file ? def_file : "PG", G_mapset());
  486. if (!fp) {
  487. G_warning(_("Unable to open PG file"));
  488. }
  489. else {
  490. key_val = G_fread_key_value(fp);
  491. fclose(fp);
  492. /* disable spatial index ? */
  493. p = G_find_key_value("spatial_index", key_val);
  494. if (p && G_strcasecmp(p, "no") == 0)
  495. spatial_index = FALSE;
  496. /* disable primary key ? */
  497. p = G_find_key_value("primary_key", key_val);
  498. if (p && G_strcasecmp(p, "no") == 0)
  499. primary_key = FALSE;
  500. G_free_key_value(key_val);
  501. }
  502. }
  503. /* create schema if not exists */
  504. if (G_strcasecmp(pg_info->schema_name, "public") != 0) {
  505. if (check_schema(pg_info) != 0)
  506. return -1;
  507. }
  508. /* prepare CREATE TABLE statement */
  509. sprintf(stmt, "CREATE TABLE \"%s\".\"%s\" (%s SERIAL%s, %s INTEGER",
  510. pg_info->schema_name, pg_info->table_name, pg_info->fid_column,
  511. primary_key ? " PRIMARY KEY" : "", GV_KEY_COLUMN);
  512. Fi = pg_info->fi;
  513. if (Fi) {
  514. /* append attributes */
  515. int col, ncols, sqltype, length;
  516. char stmt_col[DB_SQL_MAX];
  517. const char *colname;
  518. dbString dbtable_name;
  519. dbDriver *driver;
  520. dbTable *table;
  521. dbColumn *column;
  522. db_init_string(&dbtable_name);
  523. driver = open_db(pg_info);
  524. if (driver == NULL)
  525. return -1;
  526. /* describe table */
  527. db_set_string(&dbtable_name, Fi->table);
  528. if (db_describe_table(driver, &dbtable_name, &table) != DB_OK) {
  529. G_warning(_("Unable to describe table <%s>"),
  530. Fi->table);
  531. db_close_database_shutdown_driver(driver);
  532. pg_info->dbdriver = NULL;
  533. return -1;
  534. }
  535. ncols = db_get_table_number_of_columns(table);
  536. G_debug(3,
  537. "copying attributes: driver = %s database = %s table = %s cols = %d",
  538. Fi->driver, Fi->database, Fi->table, ncols);
  539. for (col = 0; col < ncols; col++) {
  540. column = db_get_table_column(table, col);
  541. colname = db_get_column_name(column);
  542. sqltype = db_get_column_sqltype(column);
  543. length = db_get_column_length(column);
  544. G_debug(3, "\tcolumn = %d name = %s type = %d length = %d",
  545. col, colname, sqltype, length);
  546. if (G_strcasecmp(pg_info->fid_column, colname) == 0 ||
  547. G_strcasecmp(GV_KEY_COLUMN, colname) == 0) {
  548. /* skip fid column if exists */
  549. G_debug(3, "\t%s skipped", colname);
  550. continue;
  551. }
  552. /* append column */
  553. sprintf(stmt_col, ",%s %s", colname, db_sqltype_name(sqltype));
  554. strcat(stmt, stmt_col);
  555. if (sqltype == DB_SQL_TYPE_CHARACTER) {
  556. /* length only for string columns */
  557. sprintf(stmt_col, "(%d)", length);
  558. strcat(stmt, stmt_col);
  559. }
  560. }
  561. db_free_string(&dbtable_name);
  562. }
  563. strcat(stmt, ")"); /* close CREATE TABLE statement */
  564. /* begin transaction (create table) */
  565. if (Vect__execute_pg(pg_info->conn, "BEGIN") == -1) {
  566. return -1;
  567. }
  568. /* create table */
  569. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  570. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  571. return -1;
  572. }
  573. /* determine geometry type (string) */
  574. switch (pg_info->feature_type) {
  575. case (SF_POINT):
  576. geom_type = "POINT";
  577. break;
  578. case (SF_LINESTRING):
  579. geom_type = "LINESTRING";
  580. break;
  581. case (SF_POLYGON):
  582. geom_type = "POLYGON";
  583. break;
  584. case (SF_POLYGON25D):
  585. geom_type = "POLYGONZ";
  586. break;
  587. case (SF_GEOMETRY):
  588. geom_type = "GEOMETRY";
  589. break;
  590. default:
  591. G_warning(_("Unsupported feature type %d"), pg_info->feature_type);
  592. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  593. return -1;
  594. }
  595. /* add geometry column */
  596. sprintf(stmt, "SELECT AddGeometryColumn('%s', '%s', "
  597. "'%s', %d, '%s', %d)",
  598. pg_info->schema_name, pg_info->table_name,
  599. pg_info->geom_column, pg_info->srid,
  600. geom_type, pg_info->coor_dim);
  601. G_debug(2, "SQL: %s", stmt);
  602. result = PQexec(pg_info->conn, stmt);
  603. if (!result || PQresultStatus(result) != PGRES_TUPLES_OK) {
  604. G_warning("%s", PQresultErrorMessage(result));
  605. PQclear(result);
  606. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  607. return -1;
  608. }
  609. /* create indices
  610. - GV_KEY_COLUMN
  611. - geometry column
  612. */
  613. sprintf(stmt,
  614. "CREATE INDEX %s_%s_idx ON \"%s\".\"%s\" (%s)",
  615. pg_info->table_name, GV_KEY_COLUMN,
  616. pg_info->schema_name, pg_info->table_name,
  617. GV_KEY_COLUMN);
  618. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  619. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  620. return -1;
  621. }
  622. if (spatial_index) {
  623. G_verbose_message(_("Building spatial index on <%s>..."),
  624. pg_info->geom_column);
  625. sprintf(stmt,
  626. "CREATE INDEX %s_%s_idx ON \"%s\".\"%s\" USING GIST (%s)",
  627. pg_info->table_name, pg_info->geom_column,
  628. pg_info->schema_name, pg_info->table_name,
  629. pg_info->geom_column);
  630. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  631. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  632. return -1;
  633. }
  634. }
  635. /* close transaction (create table) */
  636. if (Vect__execute_pg(pg_info->conn, "COMMIT") == -1) {
  637. return -1;
  638. }
  639. return 0;
  640. }
  641. /*!
  642. \brief Creates new schema for feature table if not exists
  643. \param pg_info pointer to Format_info_pg
  644. \return -1 on error
  645. \return 0 on success
  646. */
  647. int check_schema(const struct Format_info_pg *pg_info)
  648. {
  649. int i, found, nschema;
  650. char stmt[DB_SQL_MAX];
  651. PGresult *result;
  652. if (!pg_info->conn || !pg_info->table_name) {
  653. G_warning(_("No connection defined"));
  654. return -1;
  655. }
  656. /* add geometry column */
  657. sprintf(stmt, "SELECT nspname FROM pg_namespace");
  658. G_debug(2, "SQL: %s", stmt);
  659. result = PQexec(pg_info->conn, stmt);
  660. if (!result || PQresultStatus(result) != PGRES_TUPLES_OK) {
  661. PQclear(result);
  662. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  663. return -1;
  664. }
  665. found = FALSE;
  666. nschema = PQntuples(result);
  667. for (i = 0; i < nschema && !found; i++) {
  668. if (strcmp(pg_info->schema_name, PQgetvalue(result, i, 0)) == 0)
  669. found = TRUE;
  670. }
  671. PQclear(result);
  672. if (!found) {
  673. sprintf(stmt, "CREATE SCHEMA %s", pg_info->schema_name);
  674. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  675. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  676. return -1;
  677. }
  678. G_warning(_("Schema <%s> doesn't exist, created"),
  679. pg_info->schema_name);
  680. }
  681. return 0;
  682. }
  683. /*!
  684. \brief Create new PostGIS topology schema
  685. - create topology schema
  686. - add topology column to the feature table
  687. \todo Add constraints for grass-like tables
  688. \param pg_info pointer to Format_info_pg
  689. \return 0 on success
  690. \return 1 topology disable, nothing to do
  691. \return -1 on failure
  692. */
  693. int create_topo_schema(struct Format_info_pg *pg_info, int with_z)
  694. {
  695. double tolerance;
  696. char stmt[DB_SQL_MAX];
  697. char *def_file;
  698. def_file = getenv("GRASS_VECTOR_PGFILE");
  699. /* read default values from PG file*/
  700. tolerance = 0.;
  701. if (G_find_file2("", def_file ? def_file : "PG", G_mapset())) {
  702. FILE *fp;
  703. const char *p;
  704. struct Key_Value *key_val;
  705. fp = G_fopen_old("", def_file ? def_file : "PG", G_mapset());
  706. if (!fp) {
  707. G_fatal_error(_("Unable to open PG file"));
  708. }
  709. key_val = G_fread_key_value(fp);
  710. fclose(fp);
  711. /* tolerance */
  712. p = G_find_key_value("topo_tolerance", key_val);
  713. if (p)
  714. tolerance = atof(p);
  715. G_debug(1, "PG: tolerance: %f", tolerance);
  716. /* topogeom column */
  717. p = G_find_key_value("topogeom_name", key_val);
  718. if (p)
  719. pg_info->topogeom_column = G_store(p);
  720. else
  721. pg_info->topogeom_column = G_store(TOPOGEOM_COLUMN);
  722. G_debug(1, "PG: topogeom_column :%s", pg_info->topogeom_column);
  723. /* topo-geo only (default: no) */
  724. p = G_find_key_value("topo_geo_only", key_val);
  725. if (p && G_strcasecmp(p, "yes") == 0)
  726. pg_info->topo_geo_only = TRUE;
  727. G_debug(1, "PG: topo_geo_only :%d", pg_info->topo_geo_only);
  728. /* build simple features from topogeometry data */
  729. p = G_find_key_value("simple_feature", key_val);
  730. if (p && G_strcasecmp(p, "yes") == 0)
  731. pg_info->topo_geo_only = TRUE;
  732. G_debug(1, "PG: topo_geo_only :%d", pg_info->topo_geo_only);
  733. G_free_key_value(key_val);
  734. }
  735. /* begin transaction (create topo schema) */
  736. if (Vect__execute_pg(pg_info->conn, "BEGIN") == -1) {
  737. return -1;
  738. }
  739. /* create topology schema */
  740. G_verbose_message(_("Creating topology schema <%s>..."),
  741. pg_info->toposchema_name);
  742. sprintf(stmt, "SELECT topology.createtopology('%s', "
  743. "find_srid('%s', '%s', '%s'), %f, '%s')",
  744. pg_info->toposchema_name, pg_info->schema_name,
  745. pg_info->table_name, pg_info->geom_column, tolerance,
  746. with_z == WITH_Z ? "t" : "f");
  747. pg_info->toposchema_id = Vect__execute_get_value_pg(pg_info->conn, stmt);
  748. if (pg_info->toposchema_id == -1) {
  749. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  750. return -1;
  751. }
  752. /* add topo column to the feature table */
  753. G_verbose_message(_("Adding new topology column <%s>..."),
  754. pg_info->topogeom_column);
  755. sprintf(stmt, "SELECT topology.AddTopoGeometryColumn('%s', '%s', '%s', "
  756. "'%s', '%s')", pg_info->toposchema_name, pg_info->schema_name,
  757. pg_info->table_name, pg_info->topogeom_column,
  758. get_sftype(pg_info->feature_type));
  759. if (-1 == Vect__execute_get_value_pg(pg_info->conn, stmt)) {
  760. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  761. return -1;
  762. }
  763. /* create index on topo column */
  764. sprintf(stmt, "CREATE INDEX \"%s_%s_%s_idx\" ON \"%s\".\"%s\" (((%s).id))",
  765. pg_info->schema_name, pg_info->table_name, pg_info->topogeom_column,
  766. pg_info->schema_name, pg_info->table_name, pg_info->topogeom_column);
  767. if (-1 == Vect__execute_pg(pg_info->conn, stmt)) {
  768. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  769. return -1;
  770. }
  771. /* change constraints to deferrable initially deferred */
  772. if (!pg_info->topo_geo_only) {
  773. if (-1 == set_constraint_to_deferrable(pg_info, "node", "face_exists",
  774. "containing_face", "face", "face_id") ||
  775. -1 == set_constraint_to_deferrable(pg_info, "edge_data", "end_node_exists",
  776. "end_node", "node", "node_id") ||
  777. -1 == set_constraint_to_deferrable(pg_info, "edge_data", "left_face_exists",
  778. "left_face", "face", "face_id") ||
  779. -1 == set_constraint_to_deferrable(pg_info, "edge_data", "right_face_exists",
  780. "right_face", "face", "face_id") ||
  781. -1 == set_constraint_to_deferrable(pg_info, "edge_data", "start_node_exists",
  782. "start_node", "node", "node_id"))
  783. return -1;
  784. }
  785. /* create additional tables in topological schema to store
  786. GRASS topology in DB */
  787. if (!pg_info->topo_geo_only) {
  788. /* (1) create 'node_grass' (see P_node struct)
  789. todo: add constraints for lines and angles
  790. */
  791. sprintf(stmt, "CREATE TABLE \"%s\".%s (node_id SERIAL PRIMARY KEY, "
  792. "lines integer[], angles float[])", pg_info->toposchema_name, TOPO_TABLE_NODE);
  793. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  794. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  795. return -1;
  796. }
  797. sprintf(stmt, "ALTER TABLE \"%s\".%s ADD CONSTRAINT node_exists "
  798. "FOREIGN KEY (node_id) REFERENCES \"%s\".node (node_id) "
  799. "DEFERRABLE INITIALLY DEFERRED",
  800. pg_info->toposchema_name, TOPO_TABLE_NODE, pg_info->toposchema_name);
  801. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  802. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  803. return -1;
  804. }
  805. /* (2) create 'line_grass' (see P_line struct)
  806. */
  807. sprintf(stmt, "CREATE TABLE \"%s\".%s (line_id SERIAL PRIMARY KEY, "
  808. "left_area integer, right_area integer)",
  809. pg_info->toposchema_name, TOPO_TABLE_LINE);
  810. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  811. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  812. return -1;
  813. }
  814. sprintf(stmt, "ALTER TABLE \"%s\".%s ADD CONSTRAINT line_exists "
  815. "FOREIGN KEY (line_id) REFERENCES \"%s\".edge_data (edge_id) "
  816. "DEFERRABLE INITIALLY DEFERRED",
  817. pg_info->toposchema_name, TOPO_TABLE_LINE, pg_info->toposchema_name);
  818. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  819. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  820. return -1;
  821. }
  822. /* (3) create 'area_grass' (see P_area struct)
  823. todo: add constraints for lines, centtroid and isles
  824. */
  825. sprintf(stmt, "CREATE TABLE \"%s\".%s (area_id SERIAL PRIMARY KEY, "
  826. "lines integer[], centroid integer, isles integer[])",
  827. pg_info->toposchema_name, TOPO_TABLE_AREA);
  828. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  829. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  830. return -1;
  831. }
  832. /* (4) create 'isle_grass' (see P_isle struct)
  833. todo: add constraints for lines and area
  834. */
  835. sprintf(stmt, "CREATE TABLE \"%s\".%s (isle_id SERIAL PRIMARY KEY, "
  836. "lines integer[], area integer)",
  837. pg_info->toposchema_name, TOPO_TABLE_ISLE);
  838. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  839. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  840. return -1;
  841. }
  842. }
  843. /* close transaction (create topo schema) */
  844. if (Vect__execute_pg(pg_info->conn, "COMMIT") == -1) {
  845. return -1;
  846. }
  847. return 0;
  848. }
  849. /*!
  850. \brief Create new PostGIS layer in given database (internal use only)
  851. V1_open_new_pg() must be called before this function.
  852. List of currently supported types:
  853. - GV_POINT (SF_POINT)
  854. - GV_LINE (SF_LINESTRING)
  855. - GV_BOUNDARY (SF_POLYGON)
  856. When PostGIS Topology the map level is updated to topological level
  857. and build level set to GV_BUILD_BASE.
  858. \param[in,out] Map pointer to Map_info structure
  859. \param type feature type (GV_POINT, GV_LINE, ...)
  860. \return 0 success
  861. \return -1 error
  862. */
  863. int create_pg_layer(struct Map_info *Map, int type)
  864. {
  865. int ndblinks;
  866. struct Format_info_pg *pg_info;
  867. pg_info = &(Map->fInfo.pg);
  868. if (!pg_info->conninfo) {
  869. G_warning(_("Connection string not defined"));
  870. return -1;
  871. }
  872. if (!pg_info->table_name) {
  873. G_warning(_("PostGIS feature table not defined"));
  874. return -1;
  875. }
  876. G_debug(1, "Vect__open_new_pg(): conninfo='%s' table='%s' -> type = %d",
  877. pg_info->conninfo, pg_info->table_name, type);
  878. /* determine geometry type */
  879. switch (type) {
  880. case GV_POINT:
  881. pg_info->feature_type = SF_POINT;
  882. break;
  883. case GV_LINE:
  884. pg_info->feature_type = SF_LINESTRING;
  885. break;
  886. case GV_BOUNDARY:
  887. case GV_AREA:
  888. pg_info->feature_type = SF_POLYGON;
  889. break;
  890. case GV_FACE:
  891. pg_info->feature_type = SF_POLYGON25D;
  892. break;
  893. case -2:
  894. pg_info->feature_type = SF_GEOMETRY;
  895. break;
  896. default:
  897. G_warning(_("Unsupported geometry type (%d)"), type);
  898. return -1;
  899. }
  900. /* coordinate dimension */
  901. pg_info->coor_dim = Vect_is_3d(Map) ? 3 : 2;
  902. /* create new PostGIS table */
  903. ndblinks = Vect_get_num_dblinks(Map);
  904. if (ndblinks > 0) {
  905. pg_info->fi = Vect_get_dblink(Map, 0); /* TODO: support more layers */
  906. if (pg_info->fi) {
  907. if (ndblinks > 1)
  908. G_warning(_("More layers defined, using driver <%s> and "
  909. "database <%s>"),
  910. pg_info->fi->driver, pg_info->fi->database);
  911. }
  912. else {
  913. G_warning(_("Database connection not defined. "
  914. "Unable to write attributes."));
  915. }
  916. }
  917. /* create new feature table */
  918. if (create_table(pg_info) == -1) {
  919. G_warning(_("Unable to create new PostGIS feature table"));
  920. return -1;
  921. }
  922. /* create new topology schema (if PostGIS topology support is enabled) */
  923. if (pg_info->toposchema_name) {
  924. /* force topological level */
  925. Map->level = LEVEL_2;
  926. Map->plus.built = GV_BUILD_BASE;
  927. /* track updated features, used in V2__add_line_to_topo_nat() */
  928. Vect_set_updated(Map, TRUE);
  929. if (create_topo_schema(pg_info, Vect_is_3d(Map)) == -1) {
  930. G_warning(_("Unable to create new PostGIS topology schema"));
  931. return -1;
  932. }
  933. }
  934. return 0;
  935. }
  936. /*!
  937. \brief Get simple feature type as a string
  938. Used for AddTopoGeometryColumn().
  939. Valid types:
  940. - SF_POINT
  941. - SF_LINESTRING
  942. - SF_POLYGON
  943. \return string with feature type
  944. \return empty string
  945. */
  946. char *get_sftype(SF_FeatureType sftype)
  947. {
  948. if (sftype == SF_POINT)
  949. return "POINT";
  950. else if (sftype == SF_LINESTRING)
  951. return "LINE";
  952. else if (sftype == SF_POLYGON)
  953. return "POLYGON";
  954. else if (sftype == SF_GEOMETRY || sftype == SF_GEOMETRYCOLLECTION)
  955. return "COLLECTION";
  956. else
  957. G_warning(_("Unsupported feature type %d"), sftype);
  958. return "";
  959. }
  960. /*!
  961. \brief Write vector features as PostGIS simple feature element
  962. \param Map pointer to Map_info structure
  963. \param type feature type (GV_POINT, GV_LINE, ...)
  964. \param points feature geometry (exterior + interior rings for polygonsx)
  965. \param nparts number of parts
  966. \param cats feature categories
  967. \return feature offset
  968. \return -1 on error
  969. */
  970. off_t write_line_sf(struct Map_info *Map, int type,
  971. const struct line_pnts **points, int nparts,
  972. const struct line_cats *cats)
  973. {
  974. int cat;
  975. off_t offset;
  976. SF_FeatureType sf_type;
  977. struct Format_info_pg *pg_info;
  978. struct Format_info_offset *offset_info;
  979. pg_info = &(Map->fInfo.pg);
  980. offset_info = &(pg_info->offset);
  981. if (nparts < 1)
  982. return -1;
  983. /* check required PG settings */
  984. if (!pg_info->conn) {
  985. G_warning(_("No connection defined"));
  986. return -1;
  987. }
  988. if (!pg_info->table_name) {
  989. G_warning(_("PostGIS feature table not defined"));
  990. return -1;
  991. }
  992. /* create PostGIS table if doesn't exist */
  993. if (pg_info->feature_type == SF_GEOMETRY) {
  994. if (create_pg_layer(Map, type) < 0)
  995. return -1;
  996. }
  997. /* get category & check for attributes */
  998. cat = -1;
  999. if (cats->n_cats > 0) {
  1000. int field;
  1001. if (pg_info->fi)
  1002. field = pg_info->fi->number;
  1003. else
  1004. field = 1;
  1005. if (!Vect_cat_get(cats, field, &cat))
  1006. G_warning(_("No category defined for layer %d"), field);
  1007. if (cats->n_cats > 1) {
  1008. G_warning(_("Feature has more categories, using "
  1009. "category %d (from layer %d)"),
  1010. cat, field);
  1011. }
  1012. }
  1013. sf_type = pg_info->feature_type;
  1014. /* determine matching PostGIS feature geometry type */
  1015. if (type & (GV_POINT | GV_KERNEL)) {
  1016. if (sf_type != SF_POINT && sf_type != SF_POINT25D) {
  1017. G_warning(_("Point skipped (output feature type: %s)"),
  1018. Vect_get_finfo_geometry_type(Map));
  1019. return 0;
  1020. }
  1021. }
  1022. else if (type & GV_LINE) {
  1023. if (sf_type != SF_LINESTRING && sf_type != SF_LINESTRING25D) {
  1024. G_warning(_("Line skipped (output feature type: %s)"),
  1025. Vect_get_finfo_geometry_type(Map));
  1026. return 0;
  1027. }
  1028. }
  1029. else if (type & GV_BOUNDARY || type & GV_CENTROID) {
  1030. if (sf_type != SF_POLYGON) {
  1031. G_warning(_("Boundary/centroid skipped (output feature type: %s)"),
  1032. Vect_get_finfo_geometry_type(Map));
  1033. G_warning(_("Feature is not a polygon. Skipping."));
  1034. return 0;
  1035. }
  1036. }
  1037. else if (type & GV_FACE) {
  1038. if (sf_type != SF_POLYGON25D) {
  1039. G_warning(_("Face skipped (output feature type: %s)"),
  1040. Vect_get_finfo_geometry_type(Map));
  1041. return 0;
  1042. }
  1043. }
  1044. else {
  1045. G_warning(_("Unsupported feature type %d"), type);
  1046. return -1;
  1047. }
  1048. G_debug(3, "write_line_sf(): type = %d n_points = %d cat = %d",
  1049. type, points[0]->n_points, cat);
  1050. if (sf_type == SF_POLYGON || sf_type == SF_POLYGON25D) {
  1051. /* skip this check when writing PostGIS topology */
  1052. int part, npoints;
  1053. for (part = 0; part < nparts; part++) {
  1054. npoints = points[part]->n_points - 1;
  1055. if (points[part]->x[0] != points[part]->x[npoints] ||
  1056. points[part]->y[0] != points[part]->y[npoints] ||
  1057. points[part]->z[0] != points[part]->z[npoints]) {
  1058. G_warning(_("Boundary is not closed. Skipping."));
  1059. return -1;
  1060. }
  1061. }
  1062. }
  1063. /* write feature's geometry and fid */
  1064. if (-1 == write_feature(Map, -1, type, points, nparts, cat)) {
  1065. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1066. return -1;
  1067. }
  1068. /* update offset array */
  1069. if (offset_info->array_num >= offset_info->array_alloc) {
  1070. offset_info->array_alloc += 1000;
  1071. offset_info->array = (int *)G_realloc(offset_info->array,
  1072. offset_info->array_alloc *
  1073. sizeof(int));
  1074. }
  1075. offset = offset_info->array_num;
  1076. offset_info->array[offset_info->array_num++] = cat;
  1077. if (sf_type == SF_POLYGON || sf_type == SF_POLYGON25D) {
  1078. /* register first part in offset array */
  1079. offset_info->array[offset_info->array_num++] = 0;
  1080. }
  1081. G_debug(3, "write_line_sf(): -> offset = %lu offset_num = %d cat = %d",
  1082. (unsigned long)offset, offset_info->array_num, cat);
  1083. return offset;
  1084. }
  1085. /*!
  1086. \brief Write vector feature in PostGIS topology schema and
  1087. updates internal topology structures
  1088. \param Map vector map
  1089. \param type feature type to be written
  1090. \param points feature geometry
  1091. \param is_node TRUE for nodes (written as points)
  1092. \return feature id (build level >= GV_BUILD_BASE otherwise 0)
  1093. \return 0 for nodes
  1094. \return -1 on error
  1095. */
  1096. off_t write_line_tp(struct Map_info *Map, int type, int is_node,
  1097. const struct line_pnts *points,
  1098. const struct line_cats *cats)
  1099. {
  1100. int line, cat, line_id;
  1101. struct Format_info_pg *pg_info;
  1102. struct Plus_head *plus;
  1103. struct field_info *Fi;
  1104. pg_info = &(Map->fInfo.pg);
  1105. plus = &(Map->plus);
  1106. if (!(plus->update_cidx)) {
  1107. plus->cidx_up_to_date = FALSE; /* category index will be outdated */
  1108. }
  1109. /* check type for nodes */
  1110. if (is_node && type != GV_POINT) {
  1111. G_warning(_("Invalid feature type (%d) for nodes"), type);
  1112. return -1;
  1113. }
  1114. /* check required PG settings */
  1115. if (!pg_info->conn) {
  1116. G_warning(_("No connection defined"));
  1117. return -1;
  1118. }
  1119. if (!pg_info->table_name) {
  1120. G_warning(_("PostGIS feature table not defined"));
  1121. return -1;
  1122. }
  1123. if (!pg_info->toposchema_name) {
  1124. G_warning(_("PostGIS topology schema not defined"));
  1125. return -1;
  1126. }
  1127. /* create PostGIS table if doesn't exist */
  1128. if (pg_info->feature_type == SF_GEOMETRY) {
  1129. if (create_pg_layer(Map, type) < 0)
  1130. return -1;
  1131. }
  1132. if (!points)
  1133. return 0;
  1134. G_debug(3, "write_line_pg(): type = %d n_points = %d",
  1135. type, points->n_points);
  1136. Fi = pg_info->fi;
  1137. cat = -1;
  1138. if (cats && cats->n_cats > 0) {
  1139. if (Fi) {
  1140. if (!pg_info->dbdriver)
  1141. open_db(pg_info);
  1142. if (!Vect_cat_get(cats, Fi->number, &cat))
  1143. G_warning(_("No category defined for layer %d"), Fi->number);
  1144. if (cats->n_cats > 1) {
  1145. G_warning(_("Feature has more categories, using "
  1146. "category %d (from layer %d)"),
  1147. cat, cats->field[0]);
  1148. }
  1149. }
  1150. /* assume layer=1 */
  1151. Vect_cat_get(cats, 1, &cat);
  1152. }
  1153. /* update GRASS topology before writing PostGIS feature */
  1154. line = 0;
  1155. if (plus->built >= GV_BUILD_BASE) {
  1156. if (is_node) {
  1157. /* nodes are given with negative id */
  1158. line = -1 * dig_add_node(plus, points->x[0], points->y[0], points->z[0]);
  1159. }
  1160. else {
  1161. off_t offset;
  1162. /* better is probably to check nextval directly */
  1163. if (type & GV_POINTS) {
  1164. offset = Vect_get_num_primitives(Map, GV_POINTS) + 1; /* next */
  1165. offset += Vect_get_num_nodes(Map); /* nodes are also stored in 'node' table */
  1166. }
  1167. else { /* LINES */
  1168. offset = Vect_get_num_primitives(Map, GV_LINES) + 1; /* next */
  1169. }
  1170. line = add_line_to_topo_pg(Map, offset, type, points);
  1171. }
  1172. }
  1173. /* write new feature to PostGIS
  1174. - feature table for simple features
  1175. - feature table and topo schema for topological access
  1176. */
  1177. line_id = write_feature(Map, line, type, &points, 1, cat);
  1178. if (line_id < 0) {
  1179. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1180. return -1;
  1181. }
  1182. if (pg_info->cache.ctype == CACHE_MAP) {
  1183. /* add line to the cache */
  1184. Vect__reallocate_cache(&(pg_info->cache), 1, TRUE);
  1185. pg_info->cache.lines[line-1] = Vect_new_line_struct();
  1186. pg_info->cache.lines_types[line-1] = type;
  1187. pg_info->cache.lines_cats[line-1] = cat;
  1188. }
  1189. /* update offset array for nodes */
  1190. if (is_node) {
  1191. int node;
  1192. struct Format_info_offset *offset;
  1193. offset = &(pg_info->offset);
  1194. node = abs(line);
  1195. if (node > offset->array_alloc) {
  1196. offset->array_alloc += 1000;
  1197. offset->array = (int *) G_realloc (offset->array, offset->array_alloc * sizeof(int));
  1198. }
  1199. offset->array_num = node;
  1200. offset->array[node-1] = (int) line_id; /* node id starts at 1 */
  1201. }
  1202. /* update PostGIS-line topo */
  1203. if (plus->built >= GV_BUILD_AREAS && type == GV_BOUNDARY)
  1204. update_topo_face(Map, line); /* TODO: avoid extra statements */
  1205. return !is_node ? line : 0;
  1206. }
  1207. /*!
  1208. \brief Binary data to HEX
  1209. Allocated buffer should be freed by G_free().
  1210. \param nbytes number of bytes to allocate
  1211. \param wkb_data WKB data
  1212. \return allocated buffer with HEX data
  1213. */
  1214. char *binary_to_hex(int nbytes, const unsigned char *wkb_data)
  1215. {
  1216. char *hex_data;
  1217. int i, nlow, nhigh;
  1218. static const char ach_hex[] = "0123456789ABCDEF";
  1219. hex_data = (char *)G_malloc(nbytes * 2 + 1);
  1220. hex_data[nbytes * 2] = '\0';
  1221. for (i = 0; i < nbytes; i++) {
  1222. nlow = wkb_data[i] & 0x0f;
  1223. nhigh = (wkb_data[i] & 0xf0) >> 4;
  1224. hex_data[i * 2] = ach_hex[nhigh];
  1225. hex_data[i * 2 + 1] = ach_hex[nlow];
  1226. }
  1227. return hex_data;
  1228. }
  1229. /*!
  1230. \brief Write point into WKB buffer
  1231. See OGRPoint::exportToWkb from GDAL/OGR library
  1232. \param byte_order byte order (ENDIAN_LITTLE or BIG_ENDIAN)
  1233. \param points feature geometry
  1234. \param with_z WITH_Z for 3D data
  1235. \param[out] nsize buffer size
  1236. \return allocated WKB buffer
  1237. \return NULL on error
  1238. */
  1239. unsigned char *point_to_wkb(int byte_order,
  1240. const struct line_pnts *points, int with_z,
  1241. int *nsize)
  1242. {
  1243. unsigned char *wkb_data;
  1244. unsigned int sf_type;
  1245. if (points->n_points != 1)
  1246. return NULL;
  1247. /* allocate buffer */
  1248. *nsize = with_z ? 29 : 21;
  1249. wkb_data = G_malloc(*nsize);
  1250. G_zero(wkb_data, *nsize);
  1251. G_debug(5, "\t->point size=%d (with_z = %d)", *nsize, with_z);
  1252. /* set the byte order */
  1253. if (byte_order == ENDIAN_LITTLE)
  1254. wkb_data[0] = '\001';
  1255. else
  1256. wkb_data[0] = '\000';
  1257. /* set the geometry feature type */
  1258. sf_type = with_z ? SF_POINT25D : SF_POINT;
  1259. if (byte_order == ENDIAN_LITTLE)
  1260. sf_type = LSBWORD32(sf_type);
  1261. else
  1262. sf_type = MSBWORD32(sf_type);
  1263. memcpy(wkb_data + 1, &sf_type, 4);
  1264. /* copy in the raw data */
  1265. memcpy(wkb_data + 5, &(points->x[0]), 8);
  1266. memcpy(wkb_data + 5 + 8, &(points->y[0]), 8);
  1267. if (with_z) {
  1268. memcpy(wkb_data + 5 + 16, &(points->z[0]), 8);
  1269. }
  1270. /* swap if needed */
  1271. if (byte_order == ENDIAN_BIG) {
  1272. SWAPDOUBLE(wkb_data + 5);
  1273. SWAPDOUBLE(wkb_data + 5 + 8);
  1274. if (with_z)
  1275. SWAPDOUBLE(wkb_data + 5 + 16);
  1276. }
  1277. return wkb_data;
  1278. }
  1279. /*!
  1280. \bried Write linestring into WKB buffer
  1281. See OGRLineString::exportToWkb from GDAL/OGR library
  1282. \param byte_order byte order (ENDIAN_LITTLE or ENDIAN_BIG)
  1283. \param points feature geometry
  1284. \param with_z WITH_Z for 3D data
  1285. \param[out] nsize buffer size
  1286. \return allocated WKB buffer
  1287. \return NULL on error
  1288. */
  1289. unsigned char *linestring_to_wkb(int byte_order,
  1290. const struct line_pnts *points, int with_z,
  1291. int *nsize)
  1292. {
  1293. int i, point_size;
  1294. unsigned char *wkb_data;
  1295. unsigned int sf_type;
  1296. if (points->n_points < 1)
  1297. return NULL;
  1298. /* allocate buffer */
  1299. point_size = 8 * (with_z ? 3 : 2);
  1300. *nsize = 5 + 4 + points->n_points * point_size;
  1301. wkb_data = G_malloc(*nsize);
  1302. G_zero(wkb_data, *nsize);
  1303. G_debug(5, "\t->linestring size=%d (with_z = %d)", *nsize, with_z);
  1304. /* set the byte order */
  1305. if (byte_order == ENDIAN_LITTLE)
  1306. wkb_data[0] = '\001';
  1307. else
  1308. wkb_data[0] = '\000';
  1309. /* set the geometry feature type */
  1310. sf_type = with_z ? SF_LINESTRING25D : SF_LINESTRING;
  1311. if (byte_order == ENDIAN_LITTLE)
  1312. sf_type = LSBWORD32(sf_type);
  1313. else
  1314. sf_type = MSBWORD32(sf_type);
  1315. memcpy(wkb_data + 1, &sf_type, 4);
  1316. /* copy in the data count */
  1317. memcpy(wkb_data + 5, &(points->n_points), 4);
  1318. /* copy in the raw data */
  1319. for (i = 0; i < points->n_points; i++) {
  1320. memcpy(wkb_data + 9 + point_size * i, &(points->x[i]), 8);
  1321. memcpy(wkb_data + 9 + 8 + point_size * i, &(points->y[i]), 8);
  1322. if (with_z) {
  1323. memcpy(wkb_data + 9 + 16 + point_size * i, &(points->z[i]), 8);
  1324. }
  1325. }
  1326. /* swap if needed */
  1327. if (byte_order == ENDIAN_BIG) {
  1328. int npoints, nitems;
  1329. npoints = SWAP32(points->n_points);
  1330. memcpy(wkb_data + 5, &npoints, 4);
  1331. nitems = (with_z ? 3 : 2) * points->n_points;
  1332. for (i = 0; i < nitems; i++) {
  1333. SWAPDOUBLE(wkb_data + 9 + 4 + 8 * i);
  1334. }
  1335. }
  1336. return wkb_data;
  1337. }
  1338. /*!
  1339. \bried Write polygon into WKB buffer
  1340. See OGRPolygon::exportToWkb from GDAL/OGR library
  1341. \param byte_order byte order (ENDIAN_LITTLE or ENDIAN_BIG)
  1342. \param ipoints list of ring geometries (first is outer ring)
  1343. \param nrings number of rings
  1344. \param with_z WITH_Z for 3D data
  1345. \param[out] nsize buffer size
  1346. \return allocated WKB buffer
  1347. \return NULL on error
  1348. */
  1349. unsigned char *polygon_to_wkb(int byte_order,
  1350. const struct line_pnts** points, int nrings,
  1351. int with_z, int *nsize)
  1352. {
  1353. int i, ring, point_size, offset;
  1354. unsigned char *wkb_data;
  1355. unsigned int sf_type;
  1356. /* check data validity */
  1357. if (nrings < 1)
  1358. return NULL;
  1359. for (ring = 0; ring < nrings; ring++) {
  1360. if (points[ring]->n_points < 3)
  1361. return NULL;
  1362. }
  1363. /* allocate buffer */
  1364. point_size = 8 * (with_z ? 3 : 2);
  1365. *nsize = 9;
  1366. for (ring = 0; ring < nrings; ring++)
  1367. *nsize += 4 + point_size * points[ring]->n_points;
  1368. wkb_data = G_malloc(*nsize);
  1369. G_zero(wkb_data, *nsize);
  1370. G_debug(5, "\t->polygon size=%d (with_z = %d)", *nsize, with_z);
  1371. /* set the byte order */
  1372. if (byte_order == ENDIAN_LITTLE)
  1373. wkb_data[0] = '\001';
  1374. else
  1375. wkb_data[0] = '\000';
  1376. /* set the geometry feature type */
  1377. sf_type = with_z ? SF_POLYGON25D : SF_POLYGON;
  1378. if (byte_order == ENDIAN_LITTLE)
  1379. sf_type = LSBWORD32(sf_type);
  1380. else
  1381. sf_type = MSBWORD32(sf_type);
  1382. memcpy(wkb_data + 1, &sf_type, 4);
  1383. /* copy in the raw data */
  1384. if (byte_order == ENDIAN_BIG) {
  1385. int ncount;
  1386. ncount = SWAP32(nrings);
  1387. memcpy(wkb_data + 5, &ncount, 4);
  1388. }
  1389. else {
  1390. memcpy(wkb_data + 5, &nrings, 4);
  1391. }
  1392. /* serialize rings */
  1393. offset = 9;
  1394. for (ring = 0; ring < nrings; ring++) {
  1395. memcpy(wkb_data + offset, &(points[ring]->n_points), 4);
  1396. for (i = 0; i < points[ring]->n_points; i++) {
  1397. memcpy(wkb_data + offset +
  1398. 4 + point_size * i, &(points[ring]->x[i]), 8);
  1399. memcpy(wkb_data + offset +
  1400. 4 + 8 + point_size * i, &(points[ring]->y[i]), 8);
  1401. if (with_z) {
  1402. memcpy(wkb_data + offset +
  1403. 4 + 16 + point_size * i, &(points[ring]->z[i]), 8);
  1404. }
  1405. }
  1406. offset += 4 + point_size * points[ring]->n_points;
  1407. /* swap if needed */
  1408. if (byte_order == ENDIAN_BIG) {
  1409. int npoints, nitems;
  1410. npoints = SWAP32(points[ring]->n_points);
  1411. memcpy(wkb_data + 5, &npoints, 4);
  1412. nitems = (with_z ? 3 : 2) * points[ring]->n_points;
  1413. for (i = 0; i < nitems; i++) {
  1414. SWAPDOUBLE(wkb_data + offset + 4 + 8 * i);
  1415. }
  1416. }
  1417. }
  1418. return wkb_data;
  1419. }
  1420. /*!
  1421. \brief Write feature to WKB buffer
  1422. Allocated string buffer should be freed by G_free().
  1423. \param pg_info pointer to Format_info_pg struct
  1424. \param points array of geometries which form feature
  1425. \param nparts number of geometries in array
  1426. \param type feature type (GV_POINT, GV_LINE, ...)
  1427. \param with_z WITH_Z for 3D data
  1428. \return allocated string buffer
  1429. \return NULL on error
  1430. */
  1431. char *line_to_wkb(struct Format_info_pg *pg_info,
  1432. const struct line_pnts **points, int nparts, int type, int with_z)
  1433. {
  1434. int byte_order, nbytes, nsize;
  1435. unsigned int sf_type;
  1436. unsigned char *wkb_data;
  1437. char *text_data, *text_data_p, *hex_data;
  1438. byte_order = dig__byte_order_out();
  1439. /* get wkb data */
  1440. nbytes = -1;
  1441. wkb_data = NULL;
  1442. if (type & GV_POINTS) /* point or centroid */
  1443. wkb_data = point_to_wkb(byte_order, points[0], with_z, &nbytes);
  1444. else if (type == GV_LINE)
  1445. wkb_data = linestring_to_wkb(byte_order, points[0], with_z, &nbytes);
  1446. else if (type & (GV_BOUNDARY | GV_FACE | GV_AREA)) {
  1447. if (!pg_info->toposchema_name || type == GV_AREA) {
  1448. /* PostGIS simple feature access */
  1449. wkb_data = polygon_to_wkb(byte_order, points, nparts,
  1450. with_z, &nbytes);
  1451. }
  1452. else {
  1453. /* PostGIS topology access */
  1454. wkb_data = linestring_to_wkb(byte_order, points[0], with_z, &nbytes);
  1455. }
  1456. }
  1457. if (!wkb_data || nbytes < 1) {
  1458. G_warning(_("Unsupported feature type %d"), type);
  1459. return NULL;
  1460. }
  1461. /* When converting to hex, each byte takes 2 hex characters. In
  1462. addition we add in 8 characters to represent the SRID integer
  1463. in hex, and one for a null terminator */
  1464. nsize = nbytes * 2 + 8 + 1;
  1465. text_data = text_data_p = (char *)G_malloc(nsize);
  1466. /* convert the 1st byte, which is the endianess flag, to hex */
  1467. hex_data = binary_to_hex(1, wkb_data);
  1468. strcpy(text_data_p, hex_data);
  1469. G_free(hex_data);
  1470. text_data_p += 2;
  1471. /* get the geom type which is bytes 2 through 5 */
  1472. memcpy(&sf_type, wkb_data + 1, 4);
  1473. /* add the SRID flag if an SRID is provided */
  1474. if (pg_info->srid > 0) {
  1475. unsigned int srs_flag;
  1476. /* change the flag to little endianess */
  1477. srs_flag = LSBWORD32(WKBSRIDFLAG);
  1478. /* apply the flag */
  1479. sf_type = sf_type | srs_flag;
  1480. }
  1481. /* write the geom type which is 4 bytes */
  1482. hex_data = binary_to_hex(4, (unsigned char *)&sf_type);
  1483. strcpy(text_data_p, hex_data);
  1484. G_free(hex_data);
  1485. text_data_p += 8;
  1486. /* include SRID if provided */
  1487. if (pg_info->srid > 0) {
  1488. unsigned int srs_id;
  1489. /* force the srsid to little endianess */
  1490. srs_id = LSBWORD32(pg_info->srid);
  1491. hex_data = binary_to_hex(sizeof(srs_id), (unsigned char *)&srs_id);
  1492. strcpy(text_data_p, hex_data);
  1493. G_free(hex_data);
  1494. text_data_p += 8;
  1495. }
  1496. /* copy the rest of the data over - subtract 5 since we already
  1497. copied 5 bytes above */
  1498. hex_data = binary_to_hex(nbytes - 5, wkb_data + 5);
  1499. strcpy(text_data_p, hex_data);
  1500. G_free(hex_data);
  1501. return text_data;
  1502. }
  1503. /*!
  1504. \brief Insert feature into table
  1505. \param Map pointer to Map_info structure
  1506. \param line feature id (topo access only)
  1507. \param type feature type (GV_POINT, GV_LINE, ...)
  1508. \param points pointer to line_pnts struct
  1509. \param nparts number of parts (rings for polygon)
  1510. \param cat category number (-1 for no category)
  1511. \return topo_id for PostGIS Topology
  1512. \return 0 for simple features access
  1513. \return -1 on error
  1514. */
  1515. int write_feature(struct Map_info *Map, int line, int type,
  1516. const struct line_pnts **points, int nparts, int cat)
  1517. {
  1518. int with_z, topo_id;
  1519. char *stmt, *geom_data;
  1520. struct Format_info_pg *pg_info;
  1521. pg_info = &(Map->fInfo.pg);
  1522. with_z = Map->head.with_z;
  1523. if (with_z && pg_info->coor_dim != 3) {
  1524. G_warning(_("Trying to insert 3D data into feature table "
  1525. "which store 2D data only"));
  1526. return -1;
  1527. }
  1528. if (!with_z && pg_info->coor_dim != 2) {
  1529. G_warning(_("Trying to insert 2D data into feature table "
  1530. "which store 3D data only"));
  1531. return -1;
  1532. }
  1533. /* build WKB geometry from line_pnts structures */
  1534. geom_data = line_to_wkb(pg_info, points, nparts, type, with_z);
  1535. if (!geom_data)
  1536. return -1;
  1537. /* start transaction */
  1538. if (!pg_info->inTransaction) {
  1539. pg_info->inTransaction = TRUE;
  1540. if (Vect__execute_pg(pg_info->conn, "BEGIN") == -1) {
  1541. G_free(geom_data);
  1542. return -1;
  1543. }
  1544. }
  1545. /* write feature in PostGIS topology schema if enabled */
  1546. topo_id = -1;
  1547. if (pg_info->toposchema_name) {
  1548. /* insert feature into topology schema (node or edge) */
  1549. topo_id = insert_topo_element(Map, line, type, geom_data);
  1550. if (topo_id < 0) {
  1551. G_warning(_("Unable to insert topological element into PostGIS Topology schema"));
  1552. G_free(geom_data);
  1553. return -1;
  1554. }
  1555. }
  1556. /* build INSERT statement
  1557. simple feature geometry + attributes
  1558. */
  1559. stmt = build_insert_stmt(pg_info, geom_data, topo_id, cat);
  1560. /* stmt can NULL when writing PostGIS topology with no attributes
  1561. * attached */
  1562. if (stmt && Vect__execute_pg(pg_info->conn, stmt) == -1) {
  1563. /* rollback transaction */
  1564. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1565. G_free(geom_data);
  1566. G_free(stmt);
  1567. return -1;
  1568. }
  1569. G_free(geom_data);
  1570. G_free(stmt);
  1571. return pg_info->toposchema_name ? topo_id : 0;
  1572. }
  1573. /*!
  1574. \brief Build INSERT statement to add new feature to the feature
  1575. table
  1576. Note: Allocated string should be freed.
  1577. \param pg_info pointer to Format_info_pg structure
  1578. \param geom_data geometry data
  1579. \param type feature type (GV_POINT, GV_LINE, ...) - (only for PostGIS Topology)
  1580. \param id topology element id (only for PostGIS Topology)
  1581. \param cat category number (or -1 for no category)
  1582. \param Fi pointer to field_info structure (NULL for no attributes)
  1583. \return allocated string with INSERT statement
  1584. */
  1585. char *build_insert_stmt(const struct Format_info_pg *pg_info,
  1586. const char *geom_data, int topo_id, int cat)
  1587. {
  1588. int topogeom_type;
  1589. char *stmt, buf[DB_SQL_MAX];
  1590. struct field_info *Fi;
  1591. topogeom_type = -1;
  1592. if (pg_info->toposchema_name) {
  1593. switch (pg_info->feature_type) {
  1594. case SF_POINT:
  1595. topogeom_type = 1;
  1596. break;
  1597. case SF_LINESTRING:
  1598. topogeom_type = 2;
  1599. break;
  1600. case SF_POLYGON:
  1601. topogeom_type = 3;
  1602. break;
  1603. default:
  1604. G_warning(_("Unsupported feature type %d"), pg_info->feature_type);
  1605. return NULL;
  1606. }
  1607. }
  1608. Fi = pg_info->fi;
  1609. stmt = NULL;
  1610. if (Fi && cat > -1) {
  1611. /* write attributes (simple features and topology elements) */
  1612. int col, ncol, more;
  1613. int sqltype, ctype, is_fid;
  1614. char buf_val[DB_SQL_MAX], buf_tmp[DB_SQL_MAX];
  1615. const char *colname;
  1616. dbString dbstmt;
  1617. dbCursor cursor;
  1618. dbTable *table;
  1619. dbColumn *column;
  1620. dbValue *value;
  1621. db_init_string(&dbstmt);
  1622. buf_val[0] = '\0';
  1623. /* read & set attributes */
  1624. sprintf(buf, "SELECT * FROM %s WHERE %s = %d", Fi->table, Fi->key,
  1625. cat);
  1626. G_debug(4, "SQL: %s", buf);
  1627. db_set_string(&dbstmt, buf);
  1628. /* prepare INSERT statement */
  1629. sprintf(buf, "INSERT INTO \"%s\".\"%s\" (",
  1630. pg_info->schema_name, pg_info->table_name);
  1631. /* select data */
  1632. if (db_open_select_cursor(pg_info->dbdriver, &dbstmt,
  1633. &cursor, DB_SEQUENTIAL) != DB_OK) {
  1634. G_warning(_("Unable to select attributes for category %d"), cat);
  1635. }
  1636. else {
  1637. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
  1638. G_warning(_("Unable to fetch data from table <%s>"),
  1639. Fi->table);
  1640. }
  1641. if (!more) {
  1642. G_warning(_("No database record for category %d, "
  1643. "no attributes will be written"), cat);
  1644. }
  1645. else {
  1646. table = db_get_cursor_table(&cursor);
  1647. ncol = db_get_table_number_of_columns(table);
  1648. for (col = 0; col < ncol; col++) {
  1649. column = db_get_table_column(table, col);
  1650. colname = db_get_column_name(column);
  1651. /* -> values */
  1652. value = db_get_column_value(column);
  1653. /* for debug only */
  1654. db_convert_column_value_to_string(column, &dbstmt);
  1655. G_debug(3, "col %d : val = %s", col,
  1656. db_get_string(&dbstmt));
  1657. sqltype = db_get_column_sqltype(column);
  1658. ctype = db_sqltype_to_Ctype(sqltype);
  1659. is_fid = strcmp(pg_info->fid_column, colname) == 0;
  1660. /* check fid column (must be integer) */
  1661. if (is_fid == TRUE &&
  1662. ctype != DB_C_TYPE_INT) {
  1663. G_warning(_("FID column must be integer, column <%s> ignored!"),
  1664. colname);
  1665. continue;
  1666. }
  1667. /* -> columns */
  1668. sprintf(buf_tmp, "%s", colname);
  1669. strcat(buf, buf_tmp);
  1670. if (col < ncol - 1)
  1671. strcat(buf, ",");
  1672. /* prevent writing NULL values */
  1673. if (!db_test_value_isnull(value)) {
  1674. switch (ctype) {
  1675. case DB_C_TYPE_INT:
  1676. sprintf(buf_tmp, "%d", db_get_value_int(value));
  1677. break;
  1678. case DB_C_TYPE_DOUBLE:
  1679. sprintf(buf_tmp, "%.14f",
  1680. db_get_value_double(value));
  1681. break;
  1682. case DB_C_TYPE_STRING: {
  1683. char *value_tmp;
  1684. value_tmp = G_str_replace(db_get_value_string(value), "'", "''");
  1685. sprintf(buf_tmp, "'%s'", value_tmp);
  1686. G_free(value_tmp);
  1687. break;
  1688. }
  1689. case DB_C_TYPE_DATETIME:
  1690. db_convert_column_value_to_string(column,
  1691. &dbstmt);
  1692. sprintf(buf_tmp, "%s", db_get_string(&dbstmt));
  1693. break;
  1694. default:
  1695. G_warning(_("Unsupported column type %d"), ctype);
  1696. sprintf(buf_tmp, "NULL");
  1697. break;
  1698. }
  1699. }
  1700. else {
  1701. if (is_fid == TRUE)
  1702. G_warning(_("Invalid value for FID column: NULL"));
  1703. sprintf(buf_tmp, "NULL");
  1704. }
  1705. strcat(buf_val, buf_tmp);
  1706. if (col < ncol - 1)
  1707. strcat(buf_val, ",");
  1708. }
  1709. if (!pg_info->toposchema_name) {
  1710. /* simple feature access */
  1711. G_asprintf(&stmt, "%s,%s) VALUES (%s,'%s'::GEOMETRY)",
  1712. buf, pg_info->geom_column, buf_val, geom_data);
  1713. }
  1714. else {
  1715. /* PostGIS topology access, write geometry in
  1716. * topology schema, skip geometry at this point */
  1717. if (buf[strlen(buf)-1] == ',') { /* last column skipped */
  1718. buf[strlen(buf)-1] = '\0';
  1719. buf_val[strlen(buf_val)-1] = '\0';
  1720. }
  1721. G_asprintf(&stmt, "%s, %s) VALUES (%s, '(%d, 1, %d, %d)'::topology.TopoGeometry)",
  1722. buf, pg_info->topogeom_column, buf_val,
  1723. pg_info->toposchema_id, topo_id, topogeom_type);
  1724. }
  1725. }
  1726. }
  1727. }
  1728. else {
  1729. /* no attributes */
  1730. if (!pg_info->toposchema_name) {
  1731. /* no attributes (simple features access) */
  1732. if (cat > 0) {
  1733. /* cetegory defined */
  1734. G_asprintf(&stmt, "INSERT INTO \"%s\".\"%s\" (%s,%s) VALUES "
  1735. "(%d, '%s'::GEOMETRY)",
  1736. pg_info->schema_name, pg_info->table_name,
  1737. GV_KEY_COLUMN, pg_info->geom_column,
  1738. cat, geom_data);
  1739. }
  1740. else {
  1741. /* no category */
  1742. G_asprintf(&stmt, "INSERT INTO \"%s\".\"%s\" (%s) VALUES "
  1743. "('%s'::GEOMETRY)",
  1744. pg_info->schema_name, pg_info->table_name,
  1745. pg_info->geom_column, geom_data);
  1746. }
  1747. }
  1748. else {
  1749. if (cat > 0) {
  1750. /* no attributes (topology elements) */
  1751. G_asprintf(&stmt, "INSERT INTO \"%s\".\"%s\" (%s,%s) VALUES "
  1752. "(%d, '(%d, 1, %d, %d)'::topology.TopoGeometry)",
  1753. pg_info->schema_name, pg_info->table_name,
  1754. GV_KEY_COLUMN, pg_info->topogeom_column, cat,
  1755. pg_info->toposchema_id, topo_id, topogeom_type);
  1756. }
  1757. }
  1758. }
  1759. return stmt;
  1760. }
  1761. /*!
  1762. \brief Insert topological element into 'node' or 'edge' table
  1763. Negative id for nodes, 0 for next value.
  1764. \param Map pointer to Map_info struct
  1765. \param id feature id (0 for next val)
  1766. \param type feature type (GV_POINT, GV_LINE, ...)
  1767. \param geom_data geometry in wkb
  1768. \return new topo id
  1769. \return -1 on error
  1770. */
  1771. int insert_topo_element(struct Map_info *Map, int id, int type,
  1772. const char *geom_data)
  1773. {
  1774. int ret, topo_id;
  1775. char *stmt, stmt_id[DB_SQL_MAX];
  1776. struct Format_info_pg *pg_info;
  1777. struct Plus_head *plus;
  1778. struct P_line *Line;
  1779. pg_info = &(Map->fInfo.pg);
  1780. plus = &(Map->plus);
  1781. Line = NULL;
  1782. if (plus->built >= GV_BUILD_BASE) {
  1783. if (id > 0) { /* -> feature */
  1784. topo_id = id;
  1785. if (topo_id > Map->plus.n_lines) {
  1786. G_warning(_("Invalid feature %d (max: %d)"), topo_id, Map->plus.n_lines);
  1787. return -1;
  1788. }
  1789. Line = Map->plus.Line[topo_id];
  1790. if (Line->type & GV_POINTS) {
  1791. /* set topo_id for points */
  1792. topo_id = Vect_get_num_primitives(Map, GV_POINTS) + Vect_get_num_nodes(Map);
  1793. }
  1794. }
  1795. else if (id < 0) { /* node */
  1796. topo_id = abs(id);
  1797. if (type != GV_POINT) {
  1798. G_warning(_("Invalid feature type (%d) for node"), type);
  1799. return -1;
  1800. }
  1801. if (topo_id > Map->plus.n_nodes) {
  1802. G_warning(_("Invalid node %d (%d)"), topo_id, Map->plus.n_nodes);
  1803. return -1;
  1804. }
  1805. /* increment topo_id - also points and centroids are
  1806. * stored in 'node' table */
  1807. topo_id += Vect_get_num_primitives(Map, GV_POINTS);
  1808. }
  1809. }
  1810. stmt = NULL;
  1811. switch(type) {
  1812. case GV_POINT: {
  1813. /* insert new node */
  1814. #if USE_TOPO_STMT
  1815. G_asprintf(&stmt, "SELECT topology.AddNode('%s', '%s'::GEOMETRY)",
  1816. pg_info->toposchema_name, geom_data);
  1817. #else
  1818. if (id == 0) {
  1819. /* get node_id */
  1820. sprintf(stmt_id, "SELECT nextval('\"%s\".node_node_id_seq')",
  1821. pg_info->toposchema_name);
  1822. topo_id = Vect__execute_get_value_pg(pg_info->conn, stmt_id);
  1823. }
  1824. /* build insert statement */
  1825. G_asprintf(&stmt, "INSERT INTO \"%s\".node (node_id, geom) VALUES "
  1826. "(%d, '%s'::GEOMETRY)", pg_info->toposchema_name, topo_id, geom_data);
  1827. #endif
  1828. break;
  1829. }
  1830. case GV_LINE:
  1831. case GV_BOUNDARY: {
  1832. /* insert new edge */
  1833. #if USE_TOPO_STMT
  1834. G_asprintf(&stmt, "SELECT topology.AddEdge('%s', '%s'::GEOMETRY)",
  1835. pg_info->toposchema_name, geom_data);
  1836. #else
  1837. int n1, n2, nle, nre;
  1838. struct Format_info_offset *offset;
  1839. offset = &(pg_info->offset);
  1840. if (id == 0) {
  1841. /* get edge_id */
  1842. sprintf(stmt_id, "SELECT nextval('\"%s\".edge_data_edge_id_seq')",
  1843. pg_info->toposchema_name);
  1844. topo_id = Vect__execute_get_value_pg(pg_info->conn, stmt_id);
  1845. }
  1846. nle = -topo_id; /* assuming isolated lines */
  1847. nre = topo_id;
  1848. if (Line) {
  1849. int i, n, next_edge;
  1850. struct P_topo_l *topo = (struct P_topo_l *) Line->topo;
  1851. topo_id = (int)Line->offset;
  1852. /* start & end node */
  1853. n1 = topo->N1;
  1854. n2 = topo->N2;
  1855. /* next left & right edge */
  1856. for (i = 0; i < 2; i++) {
  1857. n = Vect_get_node_n_lines(Map, i == 0 ? n1 : n2);
  1858. if (n < 2) /* no connection */
  1859. continue;
  1860. next_edge = update_next_edge(Map, n,
  1861. i == 0 ? topo_id : -topo_id);
  1862. if (next_edge != 0) {
  1863. if (i == 0)
  1864. nre = next_edge; /* update next right edge for start node */
  1865. else
  1866. nle = next_edge; /* update next left edge for end node */
  1867. }
  1868. else {
  1869. G_warning(_("Unable to determine next left/right edge for edge %d"), topo_id);
  1870. }
  1871. }
  1872. }
  1873. else {
  1874. G_warning(_("Unable to insert new edge. Topology not available."));
  1875. return -1;
  1876. }
  1877. G_debug(3, "new edge: id=%d next_left_edge=%d next_right_edge=%d",
  1878. topo_id, nle, nre);
  1879. if (n1 > offset->array_num || n2 > offset->array_num) /* node id starts at 1 */
  1880. return -1;
  1881. /* build insert statement */
  1882. G_asprintf(&stmt, "INSERT INTO \"%s\".edge_data (edge_id, start_node, end_node, "
  1883. "next_left_edge, abs_next_left_edge, next_right_edge, abs_next_right_edge, "
  1884. "left_face, right_face, geom) VALUES "
  1885. "(%d, %d, %d, %d, %d, %d, %d, 0, 0, '%s'::GEOMETRY)",
  1886. pg_info->toposchema_name, topo_id, offset->array[n1-1], offset->array[n2-1],
  1887. nle, abs(nle), nre, abs(nre), geom_data);
  1888. #endif
  1889. break;
  1890. }
  1891. case GV_CENTROID: {
  1892. /* insert new node (with containing_face) */
  1893. #if USE_TOPO_STMT
  1894. G_asprintf(&stmt, "SELECT topology.AddNode('%s', '%s'::GEOMETRY)",
  1895. pg_info->toposchema_name, geom_data);
  1896. #else
  1897. int area;
  1898. if (id == 0) {
  1899. /* get node_id */
  1900. sprintf(stmt_id, "SELECT nextval('\"%s\".node_node_id_seq')",
  1901. pg_info->toposchema_name);
  1902. topo_id = Vect__execute_get_value_pg(pg_info->conn, stmt_id);
  1903. }
  1904. if (Line) {
  1905. struct P_topo_c *topo = (struct P_topo_c *) Line->topo;
  1906. area = topo->area;
  1907. }
  1908. else {
  1909. area = 0;
  1910. }
  1911. G_asprintf(&stmt, "INSERT INTO \"%s\".node (node_id, containing_face, geom) VALUES "
  1912. "(%d, %d, '%s'::GEOMETRY)", pg_info->toposchema_name,
  1913. topo_id, area, geom_data);
  1914. #endif
  1915. break;
  1916. }
  1917. default:
  1918. G_warning(_("Unsupported feature type %d"), type);
  1919. break;
  1920. }
  1921. /* execute insert statement */
  1922. ret = Vect__execute_pg(pg_info->conn, stmt);
  1923. G_free(stmt);
  1924. if (ret == -1) {
  1925. /* rollback transaction */
  1926. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1927. return -1;
  1928. }
  1929. return topo_id;
  1930. }
  1931. /*!
  1932. \brief Find next line (topo only)
  1933. \param Map pointer to Map_info struct
  1934. \param nlines number of lines
  1935. \param line current line
  1936. \param[out] left left line
  1937. \param[out] right right line
  1938. \return left (line < 0) or right (line > 0) next edge
  1939. \return 0 on failure
  1940. */
  1941. int update_next_edge(struct Map_info* Map, int nlines, int line)
  1942. {
  1943. int ret, next_line, edge;
  1944. char stmt[DB_SQL_MAX];
  1945. const struct Format_info_pg *pg_info;
  1946. struct P_line *Line_next, *Line;
  1947. Line = Line_next = NULL;
  1948. pg_info = &(Map->fInfo.pg);
  1949. /* find next line
  1950. start node -> next on the left
  1951. end node -> next on the right
  1952. */
  1953. next_line = dig_angle_next_line(&(Map->plus), line, GV_LEFT, GV_LINES, NULL);
  1954. G_debug(3, "line=%d next_line=%d", line, next_line);
  1955. if (next_line == 0) {
  1956. G_warning(_("Invalid topology"));
  1957. return 0;
  1958. }
  1959. Line = Map->plus.Line[abs(line)];
  1960. Line_next = Map->plus.Line[abs(next_line)];
  1961. if (!Line || !Line_next) {
  1962. G_warning(_("Invalid topology"));
  1963. return 0;
  1964. }
  1965. if (line > 0) {
  1966. edge = Line->offset;
  1967. ret = next_line > 0 ? Line_next->offset : -Line_next->offset;
  1968. }
  1969. else {
  1970. edge = -Line->offset;
  1971. ret = next_line > 0 ? Line_next->offset : -Line_next->offset;
  1972. }
  1973. if (next_line < 0) {
  1974. sprintf(stmt, "UPDATE \"%s\".edge_data SET next_left_edge = %d, "
  1975. "abs_next_left_edge = %d WHERE edge_id = %d AND abs_next_left_edge = %d",
  1976. pg_info->toposchema_name, edge, abs(edge), (int)Line_next->offset, (int)Line_next->offset);
  1977. G_debug(3, "update edge=%d next_left_edge=%d (?)", (int)Line_next->offset, edge);
  1978. }
  1979. else {
  1980. sprintf(stmt, "UPDATE \"%s\".edge_data SET next_right_edge = %d, "
  1981. "abs_next_right_edge = %d WHERE edge_id = %d AND abs_next_right_edge = %d",
  1982. pg_info->toposchema_name, edge, abs(edge), (int)Line_next->offset, (int)Line_next->offset);
  1983. G_debug(3, "update edge=%d next_right_edge=%d (?)", (int)Line_next->offset, edge);
  1984. }
  1985. if(Vect__execute_pg(pg_info->conn, stmt) == -1) {
  1986. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1987. return 0;
  1988. }
  1989. if (nlines > 2) {
  1990. /* more lines connected to the node
  1991. start node -> next on the right
  1992. end node -> next on the left
  1993. */
  1994. next_line = dig_angle_next_line(&(Map->plus), line, GV_RIGHT, GV_LINES, NULL);
  1995. Line_next = Map->plus.Line[abs(next_line)];
  1996. if (next_line < 0) {
  1997. sprintf(stmt, "UPDATE \"%s\".edge_data SET next_left_edge = %d, "
  1998. "abs_next_left_edge = %d WHERE edge_id = %d",
  1999. pg_info->toposchema_name, edge, abs(edge), (int)Line_next->offset);
  2000. G_debug(3, "update edge=%d next_left_edge=%d", (int)Line_next->offset, edge);
  2001. }
  2002. else {
  2003. sprintf(stmt, "UPDATE \"%s\".edge_data SET next_right_edge = %d, "
  2004. "abs_next_right_edge = %d WHERE edge_id = %d",
  2005. pg_info->toposchema_name, edge, abs(edge), (int)Line_next->offset);
  2006. G_debug(3, "update edge=%d next_right_edge=%d", (int)Line_next->offset, edge);
  2007. }
  2008. if(Vect__execute_pg(pg_info->conn, stmt) == -1) {
  2009. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2010. return 0;
  2011. }
  2012. }
  2013. return ret;
  2014. }
  2015. /*!
  2016. \brief Insert new face to the 'face' table (topo only)
  2017. \param Map pointer to Map_info struct
  2018. \param area area id (negative id for isles)
  2019. \return 0 on error
  2020. \return area id on success (>0)
  2021. */
  2022. int Vect__insert_face_pg(struct Map_info *Map, int area)
  2023. {
  2024. char *stmt;
  2025. struct Format_info_pg *pg_info;
  2026. struct bound_box box;
  2027. if (area == 0)
  2028. return 0; /* universal face has id '0' in PostGIS Topology */
  2029. stmt = NULL;
  2030. pg_info = &(Map->fInfo.pg);
  2031. /* check if face exists */
  2032. /* get mbr of the area */
  2033. if (area > 0)
  2034. Vect_get_area_box(Map, area, &box);
  2035. else
  2036. Vect_get_isle_box(Map, abs(area), &box);
  2037. /* insert face if not exists */
  2038. G_asprintf(&stmt, "INSERT INTO \"%s\".face (face_id, mbr) VALUES "
  2039. "(%d, ST_GeomFromText('POLYGON((%.12f %.12f, %.12f %.12f, %.12f %.12f, %.12f %.12f, "
  2040. "%.12f %.12f))', %d))", pg_info->toposchema_name, area,
  2041. box.W, box.S, box.W, box.N, box.E, box.N,
  2042. box.E, box.S, box.W, box.S, pg_info->srid);
  2043. G_debug(3, "new face id=%d", area);
  2044. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  2045. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2046. return 0;
  2047. }
  2048. G_free(stmt);
  2049. return area;
  2050. }
  2051. /*!
  2052. \brief Delete existing face (currently unused)
  2053. \todo Set foreign keys as DEFERRABLE INITIALLY DEFERRED and use SET
  2054. CONSTRAINTS ALL DEFERRED
  2055. \param Map pointer to Map_info struct
  2056. \param area area id to delete
  2057. \return 0 on success
  2058. \return -1 on error
  2059. */
  2060. int delete_face(const struct Map_info *Map, int area)
  2061. {
  2062. char stmt[DB_SQL_MAX];
  2063. const struct Format_info_pg *pg_info;
  2064. pg_info = &(Map->fInfo.pg);
  2065. /* update centroids first */
  2066. sprintf(stmt, "UPDATE \"%s\".node SET containing_face = 0 "
  2067. "WHERE containing_face = %d",
  2068. pg_info->toposchema_name, area);
  2069. G_debug(3, "SQL: %s", stmt);
  2070. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  2071. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2072. return -1;
  2073. }
  2074. /* update also edges (left face) */
  2075. sprintf(stmt, "UPDATE \"%s\".edge_data SET left_face = 0 "
  2076. "WHERE left_face = %d",
  2077. pg_info->toposchema_name, area);
  2078. G_debug(3, "SQL: %s", stmt);
  2079. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  2080. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2081. return -1;
  2082. }
  2083. /* update also edges (left face) */
  2084. sprintf(stmt, "UPDATE \"%s\".edge_data SET right_face = 0 "
  2085. "WHERE right_face = %d",
  2086. pg_info->toposchema_name, area);
  2087. G_debug(3, "SQL: %s", stmt);
  2088. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  2089. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2090. return -1;
  2091. }
  2092. /* delete face */
  2093. sprintf(stmt, "DELETE FROM \"%s\".face WHERE face_id = %d",
  2094. pg_info->toposchema_name, area);
  2095. G_debug(3, "delete face id=%d", area);
  2096. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  2097. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2098. return -1;
  2099. }
  2100. return 0;
  2101. }
  2102. /*!
  2103. \brief Update lines (next left and right edges)
  2104. - isolated edges
  2105. next left edge: -edge
  2106. next right edge: edge
  2107. - connected edges
  2108. next left edge: next edge or -edge
  2109. next right edge: next edge or edge
  2110. \param Map pointer to Map_info struct
  2111. \param line feature id
  2112. \return 0 on success
  2113. \return -1 on error
  2114. */
  2115. int update_topo_edge(struct Map_info *Map, int line)
  2116. {
  2117. int i, n;
  2118. int nle, nre, next_edge;
  2119. char stmt[DB_SQL_MAX];
  2120. struct Format_info_pg *pg_info;
  2121. struct P_line *Line;
  2122. pg_info = &(Map->fInfo.pg);
  2123. if (line < 1 || line > Map->plus.n_lines) {
  2124. G_warning(_("Attempt to access non-existing feature %d"), line);
  2125. return -1;
  2126. }
  2127. Line = Map->plus.Line[line];
  2128. if (!Line) {
  2129. G_warning(_("Attempt to access dead feature %d"), line);
  2130. return -1;
  2131. }
  2132. struct P_topo_l *topo = (struct P_topo_l *) Line->topo;
  2133. nre = nle = 0; /* edge = 0 is an illegal value */
  2134. /* check for line connection */
  2135. for (i = 0; i < 2; i++) {
  2136. /* first check start node then end node */
  2137. n = i == 0 ? Vect_get_node_n_lines(Map, topo->N1)
  2138. : Vect_get_node_n_lines(Map, topo->N2);
  2139. if (n < 2) /* no connection */
  2140. continue;
  2141. next_edge = update_next_edge(Map, n,
  2142. i == 0 ? line : -line);
  2143. if (next_edge != 0) {
  2144. if (i == 0)
  2145. nre = next_edge; /* update next right edge for start node */
  2146. else
  2147. nle = next_edge; /* update next left edge for end node */
  2148. }
  2149. else {
  2150. G_warning(_("Unable to determine next left/right edge"));
  2151. return -1;
  2152. }
  2153. }
  2154. if (nle == 0 && nre == 0) /* nothing changed */
  2155. return 0;
  2156. if (nle != 0 && nre != 0) {
  2157. /* update both next left and right edge */
  2158. sprintf(stmt, "UPDATE \"%s\".edge_data SET "
  2159. "next_left_edge = %d, abs_next_left_edge = %d, "
  2160. "next_right_edge = %d, abs_next_right_edge = %d "
  2161. "WHERE edge_id = %d", pg_info->toposchema_name,
  2162. nle, abs(nle), nre, abs(nre), (int)Line->offset);
  2163. }
  2164. else if (nle != 0) {
  2165. /* update next left edge only */
  2166. sprintf(stmt, "UPDATE \"%s\".edge_data SET "
  2167. "next_left_edge = %d, abs_next_left_edge = %d "
  2168. "WHERE edge_id = %d", pg_info->toposchema_name,
  2169. nle, abs(nle), (int)Line->offset);
  2170. }
  2171. else {
  2172. /* update next right edge only */
  2173. sprintf(stmt, "UPDATE \"%s\".edge_data SET "
  2174. "next_right_edge = %d, abs_next_right_edge = %d "
  2175. "WHERE edge_id = %d", pg_info->toposchema_name,
  2176. nre, abs(nre), (int)Line->offset);
  2177. }
  2178. G_debug(3, "update edge=%d next_left_edge=%d next_right_edge=%d",
  2179. (int)Line->offset, nle, nre);
  2180. if(Vect__execute_pg(pg_info->conn, stmt) == -1) {
  2181. /* rollback transaction */
  2182. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2183. return -1;
  2184. }
  2185. return 0;
  2186. }
  2187. /*!
  2188. \brief Update lines (left and right faces)
  2189. TODO: handle isles
  2190. \param Map pointer to Map_info struct
  2191. \param line feature id
  2192. \return 0 on success
  2193. \return -1 on error
  2194. */
  2195. int update_topo_face(struct Map_info *Map, int line)
  2196. {
  2197. int i, s, area, face[2];
  2198. char stmt[DB_SQL_MAX];
  2199. struct Format_info_pg *pg_info;
  2200. struct P_line *Line, *Line_i;
  2201. struct P_area *Area;
  2202. struct P_topo_b *topo, *topo_i;
  2203. pg_info = &(Map->fInfo.pg);
  2204. if (line < 1 || line > Map->plus.n_lines) {
  2205. G_warning(_("Attempt to access non-existing feature %d"), line);
  2206. return -1;
  2207. }
  2208. Line = Map->plus.Line[line];
  2209. if (!Line) {
  2210. G_warning(_("Attempt to access dead feature %d"), line);
  2211. return -1;
  2212. }
  2213. topo = (struct P_topo_b *)Line->topo;
  2214. /* for both side on the current boundary (line) */
  2215. /* create new faces */
  2216. for (s = 0; s < 2; s++) { /* for each side */
  2217. area = s == 0 ? topo->left : topo->right;
  2218. if (area <= 0) /* no area - skip */
  2219. continue;
  2220. face[s] = Vect__insert_face_pg(Map, area);
  2221. if (face[s] < 1) {
  2222. G_warning(_("Unable to create new face"));
  2223. return -1;
  2224. }
  2225. }
  2226. /* update edges forming faces */
  2227. for (s = 0; s < 2; s++) { /* for each side */
  2228. area = s == 0 ? topo->left : topo->right;
  2229. if (area <= 0) /* no area - skip */
  2230. continue;
  2231. Area = Map->plus.Area[area];
  2232. for (i = 0; i < Area->n_lines; i++) {
  2233. Line_i = Map->plus.Line[abs(Area->lines[i])];
  2234. topo_i = (struct P_topo_b *)Line_i->topo;
  2235. sprintf(stmt, "UPDATE \"%s\".edge_data SET "
  2236. "left_face = %d, right_face = %d "
  2237. "WHERE edge_id = %d", pg_info->toposchema_name,
  2238. topo_i->left > 0 ? topo_i->left : 0,
  2239. topo_i->right > 0 ? topo_i->right : 0,
  2240. (int) Line_i->offset);
  2241. G_debug(2, "SQL: %s", stmt);
  2242. if(Vect__execute_pg(pg_info->conn, stmt) == -1) {
  2243. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2244. return -1;
  2245. }
  2246. }
  2247. /* update also centroids (stored as nodes) */
  2248. if (Area->centroid > 0) {
  2249. Line_i = Map->plus.Line[Area->centroid];
  2250. sprintf(stmt, "UPDATE \"%s\".node SET containing_face = %d "
  2251. "WHERE node_id = %d", pg_info->toposchema_name,
  2252. face[s], (int)Line_i->offset);
  2253. G_debug(2, "SQL: %s", stmt);
  2254. if(Vect__execute_pg(pg_info->conn, stmt) == -1) {
  2255. /* rollback transaction */
  2256. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2257. return -1;
  2258. }
  2259. }
  2260. }
  2261. return 0;
  2262. }
  2263. /*!
  2264. \brief Add line to native and PostGIS topology
  2265. \param Map vector map
  2266. \param offset ???
  2267. \param type feature type
  2268. \param Points feature vertices
  2269. \return feature id
  2270. \return -1 on error
  2271. */
  2272. int add_line_to_topo_pg(struct Map_info *Map, off_t offset, int type,
  2273. const struct line_pnts *points)
  2274. {
  2275. int line, n_nodes;
  2276. struct Plus_head *plus;
  2277. plus = &(Map->plus);
  2278. Vect_reset_updated(Map);
  2279. line = V2__add_line_to_topo_nat(Map, offset, type, points, NULL,
  2280. -1, NULL);
  2281. /* insert new nodes into 'node' table */
  2282. n_nodes = Vect_get_num_updated_nodes(Map);
  2283. if (n_nodes > 0) {
  2284. int i, node;
  2285. double x, y, z;
  2286. if (!Points)
  2287. Points = Vect_new_line_struct();
  2288. for (i = 0; i < n_nodes; i++) {
  2289. node = Vect_get_updated_node(Map, i);
  2290. /* skip updated and deleted nodes */
  2291. if (node > 0 || plus->Node[abs(node)] == NULL)
  2292. continue;
  2293. G_debug(3, " new node: %d", node);
  2294. Vect_get_node_coor(Map, abs(node), &x, &y, &z);
  2295. Vect_reset_line(Points);
  2296. Vect_append_point(Points, x, y, z);
  2297. write_feature(Map, node, GV_POINT, (const struct line_pnts **) &Points, 1, -1);
  2298. }
  2299. }
  2300. return line;
  2301. }
  2302. /*!
  2303. \brief Delete line from native and PostGIS topology
  2304. \param Map vector map
  2305. \param line feature id to remove from topo
  2306. \param type feature type
  2307. \param Points feature vertices
  2308. \return 0 on success
  2309. \return -1 on error
  2310. */
  2311. int delete_line_from_topo_pg(struct Map_info *Map, int line, int type,
  2312. const struct line_pnts *Points)
  2313. {
  2314. int N1, N2, node_id;
  2315. char stmt[DB_SQL_MAX];
  2316. struct Format_info_pg *pg_info;
  2317. struct P_node *Node;
  2318. pg_info = &(Map->fInfo.pg);
  2319. Vect_reset_updated(Map);
  2320. if (!(type & GV_LINES))
  2321. return 0;
  2322. Vect_get_line_nodes(Map, line, &N1, &N2);
  2323. if (0 != V2__delete_line_from_topo_nat(Map, line, type, Points, NULL))
  2324. return -1;
  2325. Node = Map->plus.Node[N1];
  2326. if (!Node || Node->n_lines == 0) {
  2327. node_id = pg_info->offset.array[N1-1];
  2328. sprintf(stmt, "DELETE FROM \"%s\".\"node\" WHERE node_id = %d",
  2329. pg_info->toposchema_name, node_id);
  2330. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  2331. G_warning(_("Unable to delete node %d"), node_id);
  2332. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2333. return -1;
  2334. }
  2335. }
  2336. Node = Map->plus.Node[N2];
  2337. if (!Node || Node->n_lines == 0) {
  2338. node_id = pg_info->offset.array[N2-1];
  2339. sprintf(stmt, "DELETE FROM \"%s\".\"node\" WHERE node_id = %d",
  2340. pg_info->toposchema_name, node_id);
  2341. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  2342. G_warning(_("Unable to delete node %d"), node_id);
  2343. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2344. return -1;
  2345. }
  2346. }
  2347. return 0;
  2348. }
  2349. int set_constraint_to_deferrable(struct Format_info_pg *pg_info,
  2350. const char *table, const char *constraint,
  2351. const char *column, const char *ref_table,
  2352. const char *ref_column)
  2353. {
  2354. char stmt[DB_SQL_MAX];
  2355. sprintf(stmt, "ALTER TABLE \"%s\".%s DROP CONSTRAINT %s",
  2356. pg_info->toposchema_name, table, constraint);
  2357. if (-1 == Vect__execute_pg(pg_info->conn, stmt)) {
  2358. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2359. return -1;
  2360. }
  2361. sprintf(stmt, "ALTER TABLE \"%s\".%s ADD CONSTRAINT %s "
  2362. "FOREIGN KEY (%s) REFERENCES \"%s\".%s (%s) "
  2363. "DEFERRABLE INITIALLY DEFERRED",
  2364. pg_info->toposchema_name, table, constraint, column,
  2365. pg_info->toposchema_name, ref_table, ref_column);
  2366. if (-1 == Vect__execute_pg(pg_info->conn, stmt)) {
  2367. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  2368. return -1;
  2369. }
  2370. return 0;
  2371. }
  2372. /*!
  2373. \brief Open database connection with attribute table
  2374. \param pg_info pointer to Format_info_pg struct
  2375. \return pointer to dbDriver on succes
  2376. \return NULL on failure
  2377. */
  2378. dbDriver *open_db(struct Format_info_pg *pg_info) {
  2379. dbDriver *driver;
  2380. dbHandle handle;
  2381. struct field_info *Fi;
  2382. db_init_handle(&handle);
  2383. Fi = pg_info->fi;
  2384. pg_info->dbdriver = driver = db_start_driver(Fi->driver);
  2385. if (!driver) {
  2386. G_warning(_("Unable to start driver <%s>"), Fi->driver);
  2387. return NULL;
  2388. }
  2389. db_set_handle(&handle, Fi->database, NULL);
  2390. if (db_open_database(driver, &handle) != DB_OK) {
  2391. G_warning(_("Unable to open database <%s> by driver <%s>"),
  2392. Fi->database, Fi->driver);
  2393. db_close_database_shutdown_driver(driver);
  2394. pg_info->dbdriver = NULL;
  2395. return NULL;
  2396. }
  2397. return pg_info->dbdriver;
  2398. }
  2399. #endif