shpopen.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  1. /******************************************************************************
  2. * $Id$
  3. *
  4. * Project: Shapelib
  5. * Purpose: Implementation of core Shapefile read/write functions.
  6. * Author: Frank Warmerdam, warmerdam@pobox.com
  7. *
  8. ******************************************************************************
  9. * Copyright (c) 1999, 2001, Frank Warmerdam
  10. *
  11. * This software is available under the following "MIT Style" license,
  12. * or at the option of the licensee under the LGPL (see LICENSE.LGPL). This
  13. * option is discussed in more detail in shapelib.html.
  14. *
  15. * --
  16. *
  17. * Permission is hereby granted, free of charge, to any person obtaining a
  18. * copy of this software and associated documentation files (the "Software"),
  19. * to deal in the Software without restriction, including without limitation
  20. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  21. * and/or sell copies of the Software, and to permit persons to whom the
  22. * Software is furnished to do so, subject to the following conditions:
  23. *
  24. * The above copyright notice and this permission notice shall be included
  25. * in all copies or substantial portions of the Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  28. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  32. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  33. * DEALINGS IN THE SOFTWARE.
  34. ******************************************************************************
  35. *
  36. * $Log: shpopen.c,v $
  37. * Revision 1.59 2008/03/14 05:25:31 fwarmerdam
  38. * Correct crash on buggy geometries (gdal #2218)
  39. *
  40. * Revision 1.58 2008/01/08 23:28:26 bram
  41. * on line 2095, use a float instead of a double to avoid a compiler warning
  42. *
  43. * Revision 1.57 2007/12/06 07:00:25 fwarmerdam
  44. * dbfopen now using SAHooks for fileio
  45. *
  46. * Revision 1.56 2007/12/04 20:37:56 fwarmerdam
  47. * preliminary implementation of hooks api for io and errors
  48. *
  49. * Revision 1.55 2007/11/21 22:39:56 fwarmerdam
  50. * close shx file in readonly mode (GDAL #1956)
  51. *
  52. * Revision 1.54 2007/11/15 00:12:47 mloskot
  53. * Backported recent changes from GDAL (Ticket #1415) to Shapelib.
  54. *
  55. * Revision 1.53 2007/11/14 22:31:08 fwarmerdam
  56. * checks after mallocs to detect for corrupted/voluntary broken shapefiles.
  57. * http://trac.osgeo.org/gdal/ticket/1991
  58. *
  59. * Revision 1.52 2007/06/21 15:58:33 fwarmerdam
  60. * fix for SHPRewindObject when rings touch at one vertex (gdal #976)
  61. *
  62. * Revision 1.51 2006/09/04 15:24:01 fwarmerdam
  63. * Fixed up log message for 1.49.
  64. *
  65. * Revision 1.50 2006/09/04 15:21:39 fwarmerdam
  66. * fix of last fix
  67. *
  68. * Revision 1.49 2006/09/04 15:21:00 fwarmerdam
  69. * MLoskot: Added stronger test of Shapefile reading failures, e.g. truncated
  70. * files. The problem was discovered by Tim Sutton and reported here
  71. * https://svn.qgis.org/trac/ticket/200
  72. *
  73. * Revision 1.48 2006/01/26 15:07:32 fwarmerdam
  74. * add bMeasureIsUsed flag from Craig Bruce: Bug 1249
  75. *
  76. * Revision 1.47 2006/01/04 20:07:23 fwarmerdam
  77. * In SHPWriteObject() make sure that the record length is updated
  78. * when rewriting an existing record.
  79. *
  80. * Revision 1.46 2005/02/11 17:17:46 fwarmerdam
  81. * added panPartStart[0] validation
  82. *
  83. * Revision 1.45 2004/09/26 20:09:48 fwarmerdam
  84. * const correctness changes
  85. *
  86. * Revision 1.44 2003/12/29 00:18:39 fwarmerdam
  87. * added error checking for failed IO and optional CPL error reporting
  88. *
  89. * Revision 1.43 2003/12/01 16:20:08 warmerda
  90. * be careful of zero vertex shapes
  91. *
  92. * Revision 1.42 2003/12/01 14:58:27 warmerda
  93. * added degenerate object check in SHPRewindObject()
  94. *
  95. * Revision 1.41 2003/07/08 15:22:43 warmerda
  96. * avoid warning
  97. *
  98. * Revision 1.40 2003/04/21 18:30:37 warmerda
  99. * added header write/update public methods
  100. *
  101. * Revision 1.39 2002/08/26 06:46:56 warmerda
  102. * avoid c++ comments
  103. *
  104. * Revision 1.38 2002/05/07 16:43:39 warmerda
  105. * Removed debugging printf.
  106. *
  107. * Revision 1.37 2002/04/10 17:35:22 warmerda
  108. * fixed bug in ring reversal code
  109. *
  110. * Revision 1.36 2002/04/10 16:59:54 warmerda
  111. * added SHPRewindObject
  112. *
  113. * Revision 1.35 2001/12/07 15:10:44 warmerda
  114. * fix if .shx fails to open
  115. *
  116. * Revision 1.34 2001/11/01 16:29:55 warmerda
  117. * move pabyRec into SHPInfo for thread safety
  118. *
  119. * Revision 1.33 2001/07/03 12:18:15 warmerda
  120. * Improved cleanup if SHX not found, provied by Riccardo Cohen.
  121. *
  122. * Revision 1.32 2001/06/22 01:58:07 warmerda
  123. * be more careful about establishing initial bounds in face of NULL shapes
  124. *
  125. * Revision 1.31 2001/05/31 19:35:29 warmerda
  126. * added support for writing null shapes
  127. *
  128. * Revision 1.30 2001/05/28 12:46:29 warmerda
  129. * Add some checking on reasonableness of record count when opening.
  130. *
  131. * Revision 1.29 2001/05/23 13:36:52 warmerda
  132. * added use of SHPAPI_CALL
  133. *
  134. * Revision 1.28 2001/02/06 22:25:06 warmerda
  135. * fixed memory leaks when SHPOpen() fails
  136. *
  137. * Revision 1.27 2000/07/18 15:21:33 warmerda
  138. * added better enforcement of -1 for append in SHPWriteObject
  139. *
  140. * Revision 1.26 2000/02/16 16:03:51 warmerda
  141. * added null shape support
  142. *
  143. * Revision 1.25 1999/12/15 13:47:07 warmerda
  144. * Fixed record size settings in .shp file (was 4 words too long)
  145. * Added stdlib.h.
  146. *
  147. * Revision 1.24 1999/11/05 14:12:04 warmerda
  148. * updated license terms
  149. *
  150. * Revision 1.23 1999/07/27 00:53:46 warmerda
  151. * added support for rewriting shapes
  152. *
  153. * Revision 1.22 1999/06/11 19:19:11 warmerda
  154. * Cleanup pabyRec static buffer on SHPClose().
  155. *
  156. * Revision 1.21 1999/06/02 14:57:56 kshih
  157. * Remove unused variables
  158. *
  159. * Revision 1.20 1999/04/19 21:04:17 warmerda
  160. * Fixed syntax error.
  161. *
  162. * Revision 1.19 1999/04/19 21:01:57 warmerda
  163. * Force access string to binary in SHPOpen().
  164. *
  165. * Revision 1.18 1999/04/01 18:48:07 warmerda
  166. * Try upper case extensions if lower case doesn't work.
  167. *
  168. * Revision 1.17 1998/12/31 15:29:39 warmerda
  169. * Disable writing measure values to multipatch objects if
  170. * DISABLE_MULTIPATCH_MEASURE is defined.
  171. *
  172. * Revision 1.16 1998/12/16 05:14:33 warmerda
  173. * Added support to write MULTIPATCH. Fixed reading Z coordinate of
  174. * MULTIPATCH. Fixed record size written for all feature types.
  175. *
  176. * Revision 1.15 1998/12/03 16:35:29 warmerda
  177. * r+b is proper binary access string, not rb+.
  178. *
  179. * Revision 1.14 1998/12/03 15:47:56 warmerda
  180. * Fixed setting of nVertices in SHPCreateObject().
  181. *
  182. * Revision 1.13 1998/12/03 15:33:54 warmerda
  183. * Made SHPCalculateExtents() separately callable.
  184. *
  185. * Revision 1.12 1998/11/11 20:01:50 warmerda
  186. * Fixed bug writing ArcM/Z, and PolygonM/Z for big endian machines.
  187. *
  188. * Revision 1.11 1998/11/09 20:56:44 warmerda
  189. * Fixed up handling of file wide bounds.
  190. *
  191. * Revision 1.10 1998/11/09 20:18:51 warmerda
  192. * Converted to support 3D shapefiles, and use of SHPObject.
  193. *
  194. * Revision 1.9 1998/02/24 15:09:05 warmerda
  195. * Fixed memory leak.
  196. *
  197. * Revision 1.8 1997/12/04 15:40:29 warmerda
  198. * Fixed byte swapping of record number, and record length fields in the
  199. * .shp file.
  200. *
  201. * Revision 1.7 1995/10/21 03:15:58 warmerda
  202. * Added support for binary file access, the magic cookie 9997
  203. * and tried to improve the int32 selection logic for 16bit systems.
  204. *
  205. * Revision 1.6 1995/09/04 04:19:41 warmerda
  206. * Added fix for file bounds.
  207. *
  208. * Revision 1.5 1995/08/25 15:16:44 warmerda
  209. * Fixed a couple of problems with big endian systems ... one with bounds
  210. * and the other with multipart polygons.
  211. *
  212. * Revision 1.4 1995/08/24 18:10:17 warmerda
  213. * Switch to use SfRealloc() to avoid problems with pre-ANSI realloc()
  214. * functions (such as on the Sun).
  215. *
  216. * Revision 1.3 1995/08/23 02:23:15 warmerda
  217. * Added support for reading bounds, and fixed up problems in setting the
  218. * file wide bounds.
  219. *
  220. * Revision 1.2 1995/08/04 03:16:57 warmerda
  221. * Added header.
  222. *
  223. */
  224. #include <grass/shapefil.h>
  225. #include <math.h>
  226. #include <limits.h>
  227. #include <assert.h>
  228. #include <stdlib.h>
  229. #include <string.h>
  230. #include <stdio.h>
  231. SHP_CVSID("$Id$")
  232. typedef unsigned char uchar;
  233. #if UINT_MAX == 65535
  234. typedef long int32;
  235. #else
  236. typedef int int32;
  237. #endif
  238. #ifndef FALSE
  239. # define FALSE 0
  240. # define TRUE 1
  241. #endif
  242. #define ByteCopy( a, b, c ) memcpy( b, a, c )
  243. #ifndef MAX
  244. # define MIN(a,b) ((a<b) ? a : b)
  245. # define MAX(a,b) ((a>b) ? a : b)
  246. #endif
  247. static int bBigEndian;
  248. /************************************************************************/
  249. /* SwapWord() */
  250. /* */
  251. /* Swap a 2, 4 or 8 byte word. */
  252. /************************************************************************/
  253. static void SwapWord( int length, void * wordP )
  254. {
  255. int i;
  256. uchar temp;
  257. for( i=0; i < length/2; i++ )
  258. {
  259. temp = ((uchar *) wordP)[i];
  260. ((uchar *)wordP)[i] = ((uchar *) wordP)[length-i-1];
  261. ((uchar *) wordP)[length-i-1] = temp;
  262. }
  263. }
  264. /************************************************************************/
  265. /* SfRealloc() */
  266. /* */
  267. /* A realloc cover function that will access a NULL pointer as */
  268. /* a valid input. */
  269. /************************************************************************/
  270. static void * SfRealloc( void * pMem, int nNewSize )
  271. {
  272. if( pMem == NULL )
  273. return( (void *) malloc(nNewSize) );
  274. else
  275. return( (void *) realloc(pMem,nNewSize) );
  276. }
  277. /************************************************************************/
  278. /* SHPWriteHeader() */
  279. /* */
  280. /* Write out a header for the .shp and .shx files as well as the */
  281. /* contents of the index (.shx) file. */
  282. /************************************************************************/
  283. void SHPWriteHeader( SHPHandle psSHP )
  284. {
  285. uchar abyHeader[100];
  286. int i;
  287. int32 i32;
  288. double dValue;
  289. int32 *panSHX;
  290. if (psSHP->fpSHX == NULL)
  291. {
  292. psSHP->sHooks.Error( "SHPWriteHeader failed : SHX file is closed");
  293. return;
  294. }
  295. /* -------------------------------------------------------------------- */
  296. /* Prepare header block for .shp file. */
  297. /* -------------------------------------------------------------------- */
  298. for( i = 0; i < 100; i++ )
  299. abyHeader[i] = 0;
  300. abyHeader[2] = 0x27; /* magic cookie */
  301. abyHeader[3] = 0x0a;
  302. i32 = psSHP->nFileSize/2; /* file size */
  303. ByteCopy( &i32, abyHeader+24, 4 );
  304. if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
  305. i32 = 1000; /* version */
  306. ByteCopy( &i32, abyHeader+28, 4 );
  307. if( bBigEndian ) SwapWord( 4, abyHeader+28 );
  308. i32 = psSHP->nShapeType; /* shape type */
  309. ByteCopy( &i32, abyHeader+32, 4 );
  310. if( bBigEndian ) SwapWord( 4, abyHeader+32 );
  311. dValue = psSHP->adBoundsMin[0]; /* set bounds */
  312. ByteCopy( &dValue, abyHeader+36, 8 );
  313. if( bBigEndian ) SwapWord( 8, abyHeader+36 );
  314. dValue = psSHP->adBoundsMin[1];
  315. ByteCopy( &dValue, abyHeader+44, 8 );
  316. if( bBigEndian ) SwapWord( 8, abyHeader+44 );
  317. dValue = psSHP->adBoundsMax[0];
  318. ByteCopy( &dValue, abyHeader+52, 8 );
  319. if( bBigEndian ) SwapWord( 8, abyHeader+52 );
  320. dValue = psSHP->adBoundsMax[1];
  321. ByteCopy( &dValue, abyHeader+60, 8 );
  322. if( bBigEndian ) SwapWord( 8, abyHeader+60 );
  323. dValue = psSHP->adBoundsMin[2]; /* z */
  324. ByteCopy( &dValue, abyHeader+68, 8 );
  325. if( bBigEndian ) SwapWord( 8, abyHeader+68 );
  326. dValue = psSHP->adBoundsMax[2];
  327. ByteCopy( &dValue, abyHeader+76, 8 );
  328. if( bBigEndian ) SwapWord( 8, abyHeader+76 );
  329. dValue = psSHP->adBoundsMin[3]; /* m */
  330. ByteCopy( &dValue, abyHeader+84, 8 );
  331. if( bBigEndian ) SwapWord( 8, abyHeader+84 );
  332. dValue = psSHP->adBoundsMax[3];
  333. ByteCopy( &dValue, abyHeader+92, 8 );
  334. if( bBigEndian ) SwapWord( 8, abyHeader+92 );
  335. /* -------------------------------------------------------------------- */
  336. /* Write .shp file header. */
  337. /* -------------------------------------------------------------------- */
  338. if( psSHP->sHooks.FSeek( psSHP->fpSHP, 0, 0 ) != 0
  339. || psSHP->sHooks.FWrite( abyHeader, 100, 1, psSHP->fpSHP ) != 1 )
  340. {
  341. psSHP->sHooks.Error( "Failure writing .shp header" );
  342. return;
  343. }
  344. /* -------------------------------------------------------------------- */
  345. /* Prepare, and write .shx file header. */
  346. /* -------------------------------------------------------------------- */
  347. i32 = (psSHP->nRecords * 2 * sizeof(int32) + 100)/2; /* file size */
  348. ByteCopy( &i32, abyHeader+24, 4 );
  349. if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
  350. if( psSHP->sHooks.FSeek( psSHP->fpSHX, 0, 0 ) != 0
  351. || psSHP->sHooks.FWrite( abyHeader, 100, 1, psSHP->fpSHX ) != 1 )
  352. {
  353. psSHP->sHooks.Error( "Failure writing .shx header" );
  354. return;
  355. }
  356. /* -------------------------------------------------------------------- */
  357. /* Write out the .shx contents. */
  358. /* -------------------------------------------------------------------- */
  359. panSHX = (int32 *) malloc(sizeof(int32) * 2 * psSHP->nRecords);
  360. for( i = 0; i < psSHP->nRecords; i++ )
  361. {
  362. panSHX[i*2 ] = psSHP->panRecOffset[i]/2;
  363. panSHX[i*2+1] = psSHP->panRecSize[i]/2;
  364. if( !bBigEndian ) SwapWord( 4, panSHX+i*2 );
  365. if( !bBigEndian ) SwapWord( 4, panSHX+i*2+1 );
  366. }
  367. if( (int)psSHP->sHooks.FWrite( panSHX, sizeof(int32)*2, psSHP->nRecords, psSHP->fpSHX )
  368. != psSHP->nRecords )
  369. {
  370. psSHP->sHooks.Error( "Failure writing .shx contents" );
  371. }
  372. free( panSHX );
  373. /* -------------------------------------------------------------------- */
  374. /* Flush to disk. */
  375. /* -------------------------------------------------------------------- */
  376. psSHP->sHooks.FFlush( psSHP->fpSHP );
  377. psSHP->sHooks.FFlush( psSHP->fpSHX );
  378. }
  379. /************************************************************************/
  380. /* SHPOpen() */
  381. /************************************************************************/
  382. SHPHandle SHPAPI_CALL
  383. SHPOpen( const char * pszLayer, const char * pszAccess )
  384. {
  385. SAHooks sHooks;
  386. SASetupDefaultHooks( &sHooks );
  387. return SHPOpenLL( pszLayer, pszAccess, &sHooks );
  388. }
  389. /************************************************************************/
  390. /* SHPOpen() */
  391. /* */
  392. /* Open the .shp and .shx files based on the basename of the */
  393. /* files or either file name. */
  394. /************************************************************************/
  395. SHPHandle SHPAPI_CALL
  396. SHPOpenLL( const char * pszLayer, const char * pszAccess, SAHooks *psHooks )
  397. {
  398. char *pszFullname, *pszBasename;
  399. SHPHandle psSHP;
  400. uchar *pabyBuf;
  401. int i;
  402. double dValue;
  403. /* -------------------------------------------------------------------- */
  404. /* Ensure the access string is one of the legal ones. We */
  405. /* ensure the result string indicates binary to avoid common */
  406. /* problems on Windows. */
  407. /* -------------------------------------------------------------------- */
  408. if( strcmp(pszAccess,"rb+") == 0 || strcmp(pszAccess,"r+b") == 0
  409. || strcmp(pszAccess,"r+") == 0 )
  410. pszAccess = "r+b";
  411. else
  412. pszAccess = "rb";
  413. /* -------------------------------------------------------------------- */
  414. /* Establish the byte order on this machine. */
  415. /* -------------------------------------------------------------------- */
  416. i = 1;
  417. if( *((uchar *) &i) == 1 )
  418. bBigEndian = FALSE;
  419. else
  420. bBigEndian = TRUE;
  421. /* -------------------------------------------------------------------- */
  422. /* Initialize the info structure. */
  423. /* -------------------------------------------------------------------- */
  424. psSHP = (SHPHandle) calloc(sizeof(SHPInfo),1);
  425. psSHP->bUpdated = FALSE;
  426. memcpy( &(psSHP->sHooks), psHooks, sizeof(SAHooks) );
  427. /* -------------------------------------------------------------------- */
  428. /* Compute the base (layer) name. If there is any extension */
  429. /* on the passed in filename we will strip it off. */
  430. /* -------------------------------------------------------------------- */
  431. pszBasename = (char *) malloc(strlen(pszLayer)+5);
  432. strcpy( pszBasename, pszLayer );
  433. for( i = strlen(pszBasename)-1;
  434. i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
  435. && pszBasename[i] != '\\';
  436. i-- ) {}
  437. if( pszBasename[i] == '.' )
  438. pszBasename[i] = '\0';
  439. /* -------------------------------------------------------------------- */
  440. /* Open the .shp and .shx files. Note that files pulled from */
  441. /* a PC to Unix with upper case filenames won't work! */
  442. /* -------------------------------------------------------------------- */
  443. pszFullname = (char *) malloc(strlen(pszBasename) + 5);
  444. sprintf( pszFullname, "%s.shp", pszBasename ) ;
  445. psSHP->fpSHP = psSHP->sHooks.FOpen(pszFullname, pszAccess );
  446. if( psSHP->fpSHP == NULL )
  447. {
  448. sprintf( pszFullname, "%s.SHP", pszBasename );
  449. psSHP->fpSHP = psSHP->sHooks.FOpen(pszFullname, pszAccess );
  450. }
  451. if( psSHP->fpSHP == NULL )
  452. {
  453. #ifdef USE_CPL
  454. CPLError( CE_Failure, CPLE_OpenFailed,
  455. "Unable to open %s.shp or %s.SHP.",
  456. pszBasename, pszBasename );
  457. #endif
  458. free( psSHP );
  459. free( pszBasename );
  460. free( pszFullname );
  461. return( NULL );
  462. }
  463. sprintf( pszFullname, "%s.shx", pszBasename );
  464. psSHP->fpSHX = psSHP->sHooks.FOpen(pszFullname, pszAccess );
  465. if( psSHP->fpSHX == NULL )
  466. {
  467. sprintf( pszFullname, "%s.SHX", pszBasename );
  468. psSHP->fpSHX = psSHP->sHooks.FOpen(pszFullname, pszAccess );
  469. }
  470. if( psSHP->fpSHX == NULL )
  471. {
  472. #ifdef USE_CPL
  473. CPLError( CE_Failure, CPLE_OpenFailed,
  474. "Unable to open %s.shx or %s.SHX.",
  475. pszBasename, pszBasename );
  476. #endif
  477. psSHP->sHooks.FClose( psSHP->fpSHP );
  478. free( psSHP );
  479. free( pszBasename );
  480. free( pszFullname );
  481. return( NULL );
  482. }
  483. free( pszFullname );
  484. free( pszBasename );
  485. /* -------------------------------------------------------------------- */
  486. /* Read the file size from the SHP file. */
  487. /* -------------------------------------------------------------------- */
  488. pabyBuf = (uchar *) malloc(100);
  489. psSHP->sHooks.FRead( pabyBuf, 100, 1, psSHP->fpSHP );
  490. psSHP->nFileSize = (pabyBuf[24] * 256 * 256 * 256
  491. + pabyBuf[25] * 256 * 256
  492. + pabyBuf[26] * 256
  493. + pabyBuf[27]) * 2;
  494. /* -------------------------------------------------------------------- */
  495. /* Read SHX file Header info */
  496. /* -------------------------------------------------------------------- */
  497. if( psSHP->sHooks.FRead( pabyBuf, 100, 1, psSHP->fpSHX ) != 1
  498. || pabyBuf[0] != 0
  499. || pabyBuf[1] != 0
  500. || pabyBuf[2] != 0x27
  501. || (pabyBuf[3] != 0x0a && pabyBuf[3] != 0x0d) )
  502. {
  503. psSHP->sHooks.Error( ".shx file is unreadable, or corrupt." );
  504. psSHP->sHooks.FClose( psSHP->fpSHP );
  505. psSHP->sHooks.FClose( psSHP->fpSHX );
  506. free( psSHP );
  507. return( NULL );
  508. }
  509. psSHP->nRecords = pabyBuf[27] + pabyBuf[26] * 256
  510. + pabyBuf[25] * 256 * 256 + pabyBuf[24] * 256 * 256 * 256;
  511. psSHP->nRecords = (psSHP->nRecords*2 - 100) / 8;
  512. psSHP->nShapeType = pabyBuf[32];
  513. if( psSHP->nRecords < 0 || psSHP->nRecords > 256000000 )
  514. {
  515. char szError[200];
  516. sprintf( szError,
  517. "Record count in .shp header is %d, which seems\n"
  518. "unreasonable. Assuming header is corrupt.",
  519. psSHP->nRecords );
  520. psSHP->sHooks.Error( szError );
  521. psSHP->sHooks.FClose( psSHP->fpSHP );
  522. psSHP->sHooks.FClose( psSHP->fpSHX );
  523. free( psSHP );
  524. free(pabyBuf);
  525. return( NULL );
  526. }
  527. /* -------------------------------------------------------------------- */
  528. /* Read the bounds. */
  529. /* -------------------------------------------------------------------- */
  530. if( bBigEndian ) SwapWord( 8, pabyBuf+36 );
  531. memcpy( &dValue, pabyBuf+36, 8 );
  532. psSHP->adBoundsMin[0] = dValue;
  533. if( bBigEndian ) SwapWord( 8, pabyBuf+44 );
  534. memcpy( &dValue, pabyBuf+44, 8 );
  535. psSHP->adBoundsMin[1] = dValue;
  536. if( bBigEndian ) SwapWord( 8, pabyBuf+52 );
  537. memcpy( &dValue, pabyBuf+52, 8 );
  538. psSHP->adBoundsMax[0] = dValue;
  539. if( bBigEndian ) SwapWord( 8, pabyBuf+60 );
  540. memcpy( &dValue, pabyBuf+60, 8 );
  541. psSHP->adBoundsMax[1] = dValue;
  542. if( bBigEndian ) SwapWord( 8, pabyBuf+68 ); /* z */
  543. memcpy( &dValue, pabyBuf+68, 8 );
  544. psSHP->adBoundsMin[2] = dValue;
  545. if( bBigEndian ) SwapWord( 8, pabyBuf+76 );
  546. memcpy( &dValue, pabyBuf+76, 8 );
  547. psSHP->adBoundsMax[2] = dValue;
  548. if( bBigEndian ) SwapWord( 8, pabyBuf+84 ); /* z */
  549. memcpy( &dValue, pabyBuf+84, 8 );
  550. psSHP->adBoundsMin[3] = dValue;
  551. if( bBigEndian ) SwapWord( 8, pabyBuf+92 );
  552. memcpy( &dValue, pabyBuf+92, 8 );
  553. psSHP->adBoundsMax[3] = dValue;
  554. free( pabyBuf );
  555. /* -------------------------------------------------------------------- */
  556. /* Read the .shx file to get the offsets to each record in */
  557. /* the .shp file. */
  558. /* -------------------------------------------------------------------- */
  559. psSHP->nMaxRecords = psSHP->nRecords;
  560. psSHP->panRecOffset =
  561. (int *) malloc(sizeof(int) * MAX(1,psSHP->nMaxRecords) );
  562. psSHP->panRecSize =
  563. (int *) malloc(sizeof(int) * MAX(1,psSHP->nMaxRecords) );
  564. pabyBuf = (uchar *) malloc(8 * MAX(1,psSHP->nRecords) );
  565. if (psSHP->panRecOffset == NULL ||
  566. psSHP->panRecSize == NULL ||
  567. pabyBuf == NULL)
  568. {
  569. char szError[200];
  570. sprintf(szError,
  571. "Not enough memory to allocate requested memory (nRecords=%d).\n"
  572. "Probably broken SHP file",
  573. psSHP->nRecords );
  574. psSHP->sHooks.Error( szError );
  575. psSHP->sHooks.FClose( psSHP->fpSHP );
  576. psSHP->sHooks.FClose( psSHP->fpSHX );
  577. if (psSHP->panRecOffset) free( psSHP->panRecOffset );
  578. if (psSHP->panRecSize) free( psSHP->panRecSize );
  579. if (pabyBuf) free( pabyBuf );
  580. free( psSHP );
  581. return( NULL );
  582. }
  583. if( (int) psSHP->sHooks.FRead( pabyBuf, 8, psSHP->nRecords, psSHP->fpSHX )
  584. != psSHP->nRecords )
  585. {
  586. char szError[200];
  587. sprintf( szError,
  588. "Failed to read all values for %d records in .shx file.",
  589. psSHP->nRecords );
  590. psSHP->sHooks.Error( szError );
  591. /* SHX is short or unreadable for some reason. */
  592. psSHP->sHooks.FClose( psSHP->fpSHP );
  593. psSHP->sHooks.FClose( psSHP->fpSHX );
  594. free( psSHP->panRecOffset );
  595. free( psSHP->panRecSize );
  596. free( pabyBuf );
  597. free( psSHP );
  598. return( NULL );
  599. }
  600. /* In read-only mode, we can close the SHX now */
  601. if (strcmp(pszAccess, "rb") == 0)
  602. {
  603. psSHP->sHooks.FClose( psSHP->fpSHX );
  604. psSHP->fpSHX = NULL;
  605. }
  606. for( i = 0; i < psSHP->nRecords; i++ )
  607. {
  608. int32 nOffset, nLength;
  609. memcpy( &nOffset, pabyBuf + i * 8, 4 );
  610. if( !bBigEndian ) SwapWord( 4, &nOffset );
  611. memcpy( &nLength, pabyBuf + i * 8 + 4, 4 );
  612. if( !bBigEndian ) SwapWord( 4, &nLength );
  613. psSHP->panRecOffset[i] = nOffset*2;
  614. psSHP->panRecSize[i] = nLength*2;
  615. }
  616. free( pabyBuf );
  617. return( psSHP );
  618. }
  619. /************************************************************************/
  620. /* SHPClose() */
  621. /* */
  622. /* Close the .shp and .shx files. */
  623. /************************************************************************/
  624. void SHPAPI_CALL
  625. SHPClose(SHPHandle psSHP )
  626. {
  627. if( psSHP == NULL )
  628. return;
  629. /* -------------------------------------------------------------------- */
  630. /* Update the header if we have modified anything. */
  631. /* -------------------------------------------------------------------- */
  632. if( psSHP->bUpdated )
  633. SHPWriteHeader( psSHP );
  634. /* -------------------------------------------------------------------- */
  635. /* Free all resources, and close files. */
  636. /* -------------------------------------------------------------------- */
  637. free( psSHP->panRecOffset );
  638. free( psSHP->panRecSize );
  639. if ( psSHP->fpSHX != NULL)
  640. psSHP->sHooks.FClose( psSHP->fpSHX );
  641. psSHP->sHooks.FClose( psSHP->fpSHP );
  642. if( psSHP->pabyRec != NULL )
  643. {
  644. free( psSHP->pabyRec );
  645. }
  646. free( psSHP );
  647. }
  648. /************************************************************************/
  649. /* SHPGetInfo() */
  650. /* */
  651. /* Fetch general information about the shape file. */
  652. /************************************************************************/
  653. void SHPAPI_CALL
  654. SHPGetInfo(SHPHandle psSHP, int * pnEntities, int * pnShapeType,
  655. double * padfMinBound, double * padfMaxBound )
  656. {
  657. int i;
  658. if( psSHP == NULL )
  659. return;
  660. if( pnEntities != NULL )
  661. *pnEntities = psSHP->nRecords;
  662. if( pnShapeType != NULL )
  663. *pnShapeType = psSHP->nShapeType;
  664. for( i = 0; i < 4; i++ )
  665. {
  666. if( padfMinBound != NULL )
  667. padfMinBound[i] = psSHP->adBoundsMin[i];
  668. if( padfMaxBound != NULL )
  669. padfMaxBound[i] = psSHP->adBoundsMax[i];
  670. }
  671. }
  672. /************************************************************************/
  673. /* SHPCreate() */
  674. /* */
  675. /* Create a new shape file and return a handle to the open */
  676. /* shape file with read/write access. */
  677. /************************************************************************/
  678. SHPHandle SHPAPI_CALL
  679. SHPCreate( const char * pszLayer, int nShapeType )
  680. {
  681. SAHooks sHooks;
  682. SASetupDefaultHooks( &sHooks );
  683. return SHPCreateLL( pszLayer, nShapeType, &sHooks );
  684. }
  685. /************************************************************************/
  686. /* SHPCreate() */
  687. /* */
  688. /* Create a new shape file and return a handle to the open */
  689. /* shape file with read/write access. */
  690. /************************************************************************/
  691. SHPHandle SHPAPI_CALL
  692. SHPCreateLL( const char * pszLayer, int nShapeType, SAHooks *psHooks )
  693. {
  694. char *pszBasename, *pszFullname;
  695. int i;
  696. SAFile fpSHP, fpSHX;
  697. uchar abyHeader[100];
  698. int32 i32;
  699. double dValue;
  700. /* -------------------------------------------------------------------- */
  701. /* Establish the byte order on this system. */
  702. /* -------------------------------------------------------------------- */
  703. i = 1;
  704. if( *((uchar *) &i) == 1 )
  705. bBigEndian = FALSE;
  706. else
  707. bBigEndian = TRUE;
  708. /* -------------------------------------------------------------------- */
  709. /* Compute the base (layer) name. If there is any extension */
  710. /* on the passed in filename we will strip it off. */
  711. /* -------------------------------------------------------------------- */
  712. pszBasename = (char *) malloc(strlen(pszLayer)+5);
  713. strcpy( pszBasename, pszLayer );
  714. for( i = strlen(pszBasename)-1;
  715. i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
  716. && pszBasename[i] != '\\';
  717. i-- ) {}
  718. if( pszBasename[i] == '.' )
  719. pszBasename[i] = '\0';
  720. /* -------------------------------------------------------------------- */
  721. /* Open the two files so we can write their headers. */
  722. /* -------------------------------------------------------------------- */
  723. pszFullname = (char *) malloc(strlen(pszBasename) + 5);
  724. sprintf( pszFullname, "%s.shp", pszBasename );
  725. fpSHP = psHooks->FOpen(pszFullname, "wb" );
  726. if( fpSHP == NULL )
  727. {
  728. psHooks->Error( "Failed to create file .shp file." );
  729. free( pszFullname );
  730. free( pszBasename );
  731. return( NULL );
  732. }
  733. sprintf( pszFullname, "%s.shx", pszBasename );
  734. fpSHX = psHooks->FOpen(pszFullname, "wb" );
  735. if( fpSHX == NULL )
  736. {
  737. psHooks->Error( "Failed to create file .shx file." );
  738. free( pszFullname );
  739. free( pszBasename );
  740. return( NULL );
  741. }
  742. free( pszFullname );
  743. free( pszBasename );
  744. /* -------------------------------------------------------------------- */
  745. /* Prepare header block for .shp file. */
  746. /* -------------------------------------------------------------------- */
  747. for( i = 0; i < 100; i++ )
  748. abyHeader[i] = 0;
  749. abyHeader[2] = 0x27; /* magic cookie */
  750. abyHeader[3] = 0x0a;
  751. i32 = 50; /* file size */
  752. ByteCopy( &i32, abyHeader+24, 4 );
  753. if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
  754. i32 = 1000; /* version */
  755. ByteCopy( &i32, abyHeader+28, 4 );
  756. if( bBigEndian ) SwapWord( 4, abyHeader+28 );
  757. i32 = nShapeType; /* shape type */
  758. ByteCopy( &i32, abyHeader+32, 4 );
  759. if( bBigEndian ) SwapWord( 4, abyHeader+32 );
  760. dValue = 0.0; /* set bounds */
  761. ByteCopy( &dValue, abyHeader+36, 8 );
  762. ByteCopy( &dValue, abyHeader+44, 8 );
  763. ByteCopy( &dValue, abyHeader+52, 8 );
  764. ByteCopy( &dValue, abyHeader+60, 8 );
  765. /* -------------------------------------------------------------------- */
  766. /* Write .shp file header. */
  767. /* -------------------------------------------------------------------- */
  768. if( psHooks->FWrite( abyHeader, 100, 1, fpSHP ) != 1 )
  769. {
  770. psHooks->Error( "Failed to write .shp header." );
  771. return NULL;
  772. }
  773. /* -------------------------------------------------------------------- */
  774. /* Prepare, and write .shx file header. */
  775. /* -------------------------------------------------------------------- */
  776. i32 = 50; /* file size */
  777. ByteCopy( &i32, abyHeader+24, 4 );
  778. if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
  779. if( psHooks->FWrite( abyHeader, 100, 1, fpSHX ) != 1 )
  780. {
  781. psHooks->Error( "Failed to write .shx header." );
  782. return NULL;
  783. }
  784. /* -------------------------------------------------------------------- */
  785. /* Close the files, and then open them as regular existing files. */
  786. /* -------------------------------------------------------------------- */
  787. psHooks->FClose( fpSHP );
  788. psHooks->FClose( fpSHX );
  789. return( SHPOpenLL( pszLayer, "r+b", psHooks ) );
  790. }
  791. /************************************************************************/
  792. /* _SHPSetBounds() */
  793. /* */
  794. /* Compute a bounds rectangle for a shape, and set it into the */
  795. /* indicated location in the record. */
  796. /************************************************************************/
  797. static void _SHPSetBounds( uchar * pabyRec, SHPObject * psShape )
  798. {
  799. ByteCopy( &(psShape->dfXMin), pabyRec + 0, 8 );
  800. ByteCopy( &(psShape->dfYMin), pabyRec + 8, 8 );
  801. ByteCopy( &(psShape->dfXMax), pabyRec + 16, 8 );
  802. ByteCopy( &(psShape->dfYMax), pabyRec + 24, 8 );
  803. if( bBigEndian )
  804. {
  805. SwapWord( 8, pabyRec + 0 );
  806. SwapWord( 8, pabyRec + 8 );
  807. SwapWord( 8, pabyRec + 16 );
  808. SwapWord( 8, pabyRec + 24 );
  809. }
  810. }
  811. /************************************************************************/
  812. /* SHPComputeExtents() */
  813. /* */
  814. /* Recompute the extents of a shape. Automatically done by */
  815. /* SHPCreateObject(). */
  816. /************************************************************************/
  817. void SHPAPI_CALL
  818. SHPComputeExtents( SHPObject * psObject )
  819. {
  820. int i;
  821. /* -------------------------------------------------------------------- */
  822. /* Build extents for this object. */
  823. /* -------------------------------------------------------------------- */
  824. if( psObject->nVertices > 0 )
  825. {
  826. psObject->dfXMin = psObject->dfXMax = psObject->padfX[0];
  827. psObject->dfYMin = psObject->dfYMax = psObject->padfY[0];
  828. psObject->dfZMin = psObject->dfZMax = psObject->padfZ[0];
  829. psObject->dfMMin = psObject->dfMMax = psObject->padfM[0];
  830. }
  831. for( i = 0; i < psObject->nVertices; i++ )
  832. {
  833. psObject->dfXMin = MIN(psObject->dfXMin, psObject->padfX[i]);
  834. psObject->dfYMin = MIN(psObject->dfYMin, psObject->padfY[i]);
  835. psObject->dfZMin = MIN(psObject->dfZMin, psObject->padfZ[i]);
  836. psObject->dfMMin = MIN(psObject->dfMMin, psObject->padfM[i]);
  837. psObject->dfXMax = MAX(psObject->dfXMax, psObject->padfX[i]);
  838. psObject->dfYMax = MAX(psObject->dfYMax, psObject->padfY[i]);
  839. psObject->dfZMax = MAX(psObject->dfZMax, psObject->padfZ[i]);
  840. psObject->dfMMax = MAX(psObject->dfMMax, psObject->padfM[i]);
  841. }
  842. }
  843. /************************************************************************/
  844. /* SHPCreateObject() */
  845. /* */
  846. /* Create a shape object. It should be freed with */
  847. /* SHPDestroyObject(). */
  848. /************************************************************************/
  849. SHPObject SHPAPI_CALL1(*)
  850. SHPCreateObject( int nSHPType, int nShapeId, int nParts,
  851. const int * panPartStart, const int * panPartType,
  852. int nVertices, const double *padfX, const double *padfY,
  853. const double * padfZ, const double * padfM )
  854. {
  855. SHPObject *psObject;
  856. int i, bHasM, bHasZ;
  857. psObject = (SHPObject *) calloc(1,sizeof(SHPObject));
  858. psObject->nSHPType = nSHPType;
  859. psObject->nShapeId = nShapeId;
  860. psObject->bMeasureIsUsed = FALSE;
  861. /* -------------------------------------------------------------------- */
  862. /* Establish whether this shape type has M, and Z values. */
  863. /* -------------------------------------------------------------------- */
  864. if( nSHPType == SHPT_ARCM
  865. || nSHPType == SHPT_POINTM
  866. || nSHPType == SHPT_POLYGONM
  867. || nSHPType == SHPT_MULTIPOINTM )
  868. {
  869. bHasM = TRUE;
  870. bHasZ = FALSE;
  871. }
  872. else if( nSHPType == SHPT_ARCZ
  873. || nSHPType == SHPT_POINTZ
  874. || nSHPType == SHPT_POLYGONZ
  875. || nSHPType == SHPT_MULTIPOINTZ
  876. || nSHPType == SHPT_MULTIPATCH )
  877. {
  878. bHasM = TRUE;
  879. bHasZ = TRUE;
  880. }
  881. else
  882. {
  883. bHasM = FALSE;
  884. bHasZ = FALSE;
  885. }
  886. /* -------------------------------------------------------------------- */
  887. /* Capture parts. Note that part type is optional, and */
  888. /* defaults to ring. */
  889. /* -------------------------------------------------------------------- */
  890. if( nSHPType == SHPT_ARC || nSHPType == SHPT_POLYGON
  891. || nSHPType == SHPT_ARCM || nSHPType == SHPT_POLYGONM
  892. || nSHPType == SHPT_ARCZ || nSHPType == SHPT_POLYGONZ
  893. || nSHPType == SHPT_MULTIPATCH )
  894. {
  895. psObject->nParts = MAX(1,nParts);
  896. psObject->panPartStart = (int *)
  897. malloc(sizeof(int) * psObject->nParts);
  898. psObject->panPartType = (int *)
  899. malloc(sizeof(int) * psObject->nParts);
  900. psObject->panPartStart[0] = 0;
  901. psObject->panPartType[0] = SHPP_RING;
  902. for( i = 0; i < nParts; i++ )
  903. {
  904. psObject->panPartStart[i] = panPartStart[i];
  905. if( panPartType != NULL )
  906. psObject->panPartType[i] = panPartType[i];
  907. else
  908. psObject->panPartType[i] = SHPP_RING;
  909. }
  910. if( psObject->panPartStart[0] != 0 )
  911. psObject->panPartStart[0] = 0;
  912. }
  913. /* -------------------------------------------------------------------- */
  914. /* Capture vertices. Note that Z and M are optional, but X and */
  915. /* Y are not. */
  916. /* -------------------------------------------------------------------- */
  917. if( nVertices > 0 )
  918. {
  919. psObject->padfX = (double *) calloc(sizeof(double),nVertices);
  920. psObject->padfY = (double *) calloc(sizeof(double),nVertices);
  921. psObject->padfZ = (double *) calloc(sizeof(double),nVertices);
  922. psObject->padfM = (double *) calloc(sizeof(double),nVertices);
  923. assert( padfX != NULL );
  924. assert( padfY != NULL );
  925. for( i = 0; i < nVertices; i++ )
  926. {
  927. psObject->padfX[i] = padfX[i];
  928. psObject->padfY[i] = padfY[i];
  929. if( padfZ != NULL && bHasZ )
  930. psObject->padfZ[i] = padfZ[i];
  931. if( padfM != NULL && bHasM )
  932. psObject->padfM[i] = padfM[i];
  933. }
  934. if( padfM != NULL && bHasM )
  935. psObject->bMeasureIsUsed = TRUE;
  936. }
  937. /* -------------------------------------------------------------------- */
  938. /* Compute the extents. */
  939. /* -------------------------------------------------------------------- */
  940. psObject->nVertices = nVertices;
  941. SHPComputeExtents( psObject );
  942. return( psObject );
  943. }
  944. /************************************************************************/
  945. /* SHPCreateSimpleObject() */
  946. /* */
  947. /* Create a simple (common) shape object. Destroy with */
  948. /* SHPDestroyObject(). */
  949. /************************************************************************/
  950. SHPObject SHPAPI_CALL1(*)
  951. SHPCreateSimpleObject( int nSHPType, int nVertices,
  952. const double * padfX, const double * padfY,
  953. const double * padfZ )
  954. {
  955. return( SHPCreateObject( nSHPType, -1, 0, NULL, NULL,
  956. nVertices, padfX, padfY, padfZ, NULL ) );
  957. }
  958. /************************************************************************/
  959. /* SHPWriteObject() */
  960. /* */
  961. /* Write out the vertices of a new structure. Note that it is */
  962. /* only possible to write vertices at the end of the file. */
  963. /************************************************************************/
  964. int SHPAPI_CALL
  965. SHPWriteObject(SHPHandle psSHP, int nShapeId, SHPObject * psObject )
  966. {
  967. int nRecordOffset, i, nRecordSize=0;
  968. uchar *pabyRec;
  969. int32 i32;
  970. psSHP->bUpdated = TRUE;
  971. /* -------------------------------------------------------------------- */
  972. /* Ensure that shape object matches the type of the file it is */
  973. /* being written to. */
  974. /* -------------------------------------------------------------------- */
  975. assert( psObject->nSHPType == psSHP->nShapeType
  976. || psObject->nSHPType == SHPT_NULL );
  977. /* -------------------------------------------------------------------- */
  978. /* Ensure that -1 is used for appends. Either blow an */
  979. /* assertion, or if they are disabled, set the shapeid to -1 */
  980. /* for appends. */
  981. /* -------------------------------------------------------------------- */
  982. assert( nShapeId == -1
  983. || (nShapeId >= 0 && nShapeId < psSHP->nRecords) );
  984. if( nShapeId != -1 && nShapeId >= psSHP->nRecords )
  985. nShapeId = -1;
  986. /* -------------------------------------------------------------------- */
  987. /* Add the new entity to the in memory index. */
  988. /* -------------------------------------------------------------------- */
  989. if( nShapeId == -1 && psSHP->nRecords+1 > psSHP->nMaxRecords )
  990. {
  991. psSHP->nMaxRecords =(int) ( psSHP->nMaxRecords * 1.3 + 100);
  992. psSHP->panRecOffset = (int *)
  993. SfRealloc(psSHP->panRecOffset,sizeof(int) * psSHP->nMaxRecords );
  994. psSHP->panRecSize = (int *)
  995. SfRealloc(psSHP->panRecSize,sizeof(int) * psSHP->nMaxRecords );
  996. }
  997. /* -------------------------------------------------------------------- */
  998. /* Initialize record. */
  999. /* -------------------------------------------------------------------- */
  1000. pabyRec = (uchar *) malloc(psObject->nVertices * 4 * sizeof(double)
  1001. + psObject->nParts * 8 + 128);
  1002. /* -------------------------------------------------------------------- */
  1003. /* Extract vertices for a Polygon or Arc. */
  1004. /* -------------------------------------------------------------------- */
  1005. if( psObject->nSHPType == SHPT_POLYGON
  1006. || psObject->nSHPType == SHPT_POLYGONZ
  1007. || psObject->nSHPType == SHPT_POLYGONM
  1008. || psObject->nSHPType == SHPT_ARC
  1009. || psObject->nSHPType == SHPT_ARCZ
  1010. || psObject->nSHPType == SHPT_ARCM
  1011. || psObject->nSHPType == SHPT_MULTIPATCH )
  1012. {
  1013. int32 nPoints, nParts;
  1014. int i;
  1015. nPoints = psObject->nVertices;
  1016. nParts = psObject->nParts;
  1017. _SHPSetBounds( pabyRec + 12, psObject );
  1018. if( bBigEndian ) SwapWord( 4, &nPoints );
  1019. if( bBigEndian ) SwapWord( 4, &nParts );
  1020. ByteCopy( &nPoints, pabyRec + 40 + 8, 4 );
  1021. ByteCopy( &nParts, pabyRec + 36 + 8, 4 );
  1022. nRecordSize = 52;
  1023. /*
  1024. * Write part start positions.
  1025. */
  1026. ByteCopy( psObject->panPartStart, pabyRec + 44 + 8,
  1027. 4 * psObject->nParts );
  1028. for( i = 0; i < psObject->nParts; i++ )
  1029. {
  1030. if( bBigEndian ) SwapWord( 4, pabyRec + 44 + 8 + 4*i );
  1031. nRecordSize += 4;
  1032. }
  1033. /*
  1034. * Write multipatch part types if needed.
  1035. */
  1036. if( psObject->nSHPType == SHPT_MULTIPATCH )
  1037. {
  1038. memcpy( pabyRec + nRecordSize, psObject->panPartType,
  1039. 4*psObject->nParts );
  1040. for( i = 0; i < psObject->nParts; i++ )
  1041. {
  1042. if( bBigEndian ) SwapWord( 4, pabyRec + nRecordSize );
  1043. nRecordSize += 4;
  1044. }
  1045. }
  1046. /*
  1047. * Write the (x,y) vertex values.
  1048. */
  1049. for( i = 0; i < psObject->nVertices; i++ )
  1050. {
  1051. ByteCopy( psObject->padfX + i, pabyRec + nRecordSize, 8 );
  1052. ByteCopy( psObject->padfY + i, pabyRec + nRecordSize + 8, 8 );
  1053. if( bBigEndian )
  1054. SwapWord( 8, pabyRec + nRecordSize );
  1055. if( bBigEndian )
  1056. SwapWord( 8, pabyRec + nRecordSize + 8 );
  1057. nRecordSize += 2 * 8;
  1058. }
  1059. /*
  1060. * Write the Z coordinates (if any).
  1061. */
  1062. if( psObject->nSHPType == SHPT_POLYGONZ
  1063. || psObject->nSHPType == SHPT_ARCZ
  1064. || psObject->nSHPType == SHPT_MULTIPATCH )
  1065. {
  1066. ByteCopy( &(psObject->dfZMin), pabyRec + nRecordSize, 8 );
  1067. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1068. nRecordSize += 8;
  1069. ByteCopy( &(psObject->dfZMax), pabyRec + nRecordSize, 8 );
  1070. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1071. nRecordSize += 8;
  1072. for( i = 0; i < psObject->nVertices; i++ )
  1073. {
  1074. ByteCopy( psObject->padfZ + i, pabyRec + nRecordSize, 8 );
  1075. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1076. nRecordSize += 8;
  1077. }
  1078. }
  1079. /*
  1080. * Write the M values, if any.
  1081. */
  1082. if( psObject->bMeasureIsUsed
  1083. && (psObject->nSHPType == SHPT_POLYGONM
  1084. || psObject->nSHPType == SHPT_ARCM
  1085. #ifndef DISABLE_MULTIPATCH_MEASURE
  1086. || psObject->nSHPType == SHPT_MULTIPATCH
  1087. #endif
  1088. || psObject->nSHPType == SHPT_POLYGONZ
  1089. || psObject->nSHPType == SHPT_ARCZ) )
  1090. {
  1091. ByteCopy( &(psObject->dfMMin), pabyRec + nRecordSize, 8 );
  1092. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1093. nRecordSize += 8;
  1094. ByteCopy( &(psObject->dfMMax), pabyRec + nRecordSize, 8 );
  1095. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1096. nRecordSize += 8;
  1097. for( i = 0; i < psObject->nVertices; i++ )
  1098. {
  1099. ByteCopy( psObject->padfM + i, pabyRec + nRecordSize, 8 );
  1100. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1101. nRecordSize += 8;
  1102. }
  1103. }
  1104. }
  1105. /* -------------------------------------------------------------------- */
  1106. /* Extract vertices for a MultiPoint. */
  1107. /* -------------------------------------------------------------------- */
  1108. else if( psObject->nSHPType == SHPT_MULTIPOINT
  1109. || psObject->nSHPType == SHPT_MULTIPOINTZ
  1110. || psObject->nSHPType == SHPT_MULTIPOINTM )
  1111. {
  1112. int32 nPoints;
  1113. int i;
  1114. nPoints = psObject->nVertices;
  1115. _SHPSetBounds( pabyRec + 12, psObject );
  1116. if( bBigEndian ) SwapWord( 4, &nPoints );
  1117. ByteCopy( &nPoints, pabyRec + 44, 4 );
  1118. for( i = 0; i < psObject->nVertices; i++ )
  1119. {
  1120. ByteCopy( psObject->padfX + i, pabyRec + 48 + i*16, 8 );
  1121. ByteCopy( psObject->padfY + i, pabyRec + 48 + i*16 + 8, 8 );
  1122. if( bBigEndian ) SwapWord( 8, pabyRec + 48 + i*16 );
  1123. if( bBigEndian ) SwapWord( 8, pabyRec + 48 + i*16 + 8 );
  1124. }
  1125. nRecordSize = 48 + 16 * psObject->nVertices;
  1126. if( psObject->nSHPType == SHPT_MULTIPOINTZ )
  1127. {
  1128. ByteCopy( &(psObject->dfZMin), pabyRec + nRecordSize, 8 );
  1129. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1130. nRecordSize += 8;
  1131. ByteCopy( &(psObject->dfZMax), pabyRec + nRecordSize, 8 );
  1132. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1133. nRecordSize += 8;
  1134. for( i = 0; i < psObject->nVertices; i++ )
  1135. {
  1136. ByteCopy( psObject->padfZ + i, pabyRec + nRecordSize, 8 );
  1137. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1138. nRecordSize += 8;
  1139. }
  1140. }
  1141. if( psObject->bMeasureIsUsed
  1142. && (psObject->nSHPType == SHPT_MULTIPOINTZ
  1143. || psObject->nSHPType == SHPT_MULTIPOINTM) )
  1144. {
  1145. ByteCopy( &(psObject->dfMMin), pabyRec + nRecordSize, 8 );
  1146. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1147. nRecordSize += 8;
  1148. ByteCopy( &(psObject->dfMMax), pabyRec + nRecordSize, 8 );
  1149. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1150. nRecordSize += 8;
  1151. for( i = 0; i < psObject->nVertices; i++ )
  1152. {
  1153. ByteCopy( psObject->padfM + i, pabyRec + nRecordSize, 8 );
  1154. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1155. nRecordSize += 8;
  1156. }
  1157. }
  1158. }
  1159. /* -------------------------------------------------------------------- */
  1160. /* Write point. */
  1161. /* -------------------------------------------------------------------- */
  1162. else if( psObject->nSHPType == SHPT_POINT
  1163. || psObject->nSHPType == SHPT_POINTZ
  1164. || psObject->nSHPType == SHPT_POINTM )
  1165. {
  1166. ByteCopy( psObject->padfX, pabyRec + 12, 8 );
  1167. ByteCopy( psObject->padfY, pabyRec + 20, 8 );
  1168. if( bBigEndian ) SwapWord( 8, pabyRec + 12 );
  1169. if( bBigEndian ) SwapWord( 8, pabyRec + 20 );
  1170. nRecordSize = 28;
  1171. if( psObject->nSHPType == SHPT_POINTZ )
  1172. {
  1173. ByteCopy( psObject->padfZ, pabyRec + nRecordSize, 8 );
  1174. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1175. nRecordSize += 8;
  1176. }
  1177. if( psObject->bMeasureIsUsed
  1178. && (psObject->nSHPType == SHPT_POINTZ
  1179. || psObject->nSHPType == SHPT_POINTM) )
  1180. {
  1181. ByteCopy( psObject->padfM, pabyRec + nRecordSize, 8 );
  1182. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1183. nRecordSize += 8;
  1184. }
  1185. }
  1186. /* -------------------------------------------------------------------- */
  1187. /* Not much to do for null geometries. */
  1188. /* -------------------------------------------------------------------- */
  1189. else if( psObject->nSHPType == SHPT_NULL )
  1190. {
  1191. nRecordSize = 12;
  1192. }
  1193. else
  1194. {
  1195. /* unknown type */
  1196. assert( FALSE );
  1197. }
  1198. /* -------------------------------------------------------------------- */
  1199. /* Establish where we are going to put this record. If we are */
  1200. /* rewriting and existing record, and it will fit, then put it */
  1201. /* back where the original came from. Otherwise write at the end. */
  1202. /* -------------------------------------------------------------------- */
  1203. if( nShapeId == -1 || psSHP->panRecSize[nShapeId] < nRecordSize-8 )
  1204. {
  1205. if( nShapeId == -1 )
  1206. nShapeId = psSHP->nRecords++;
  1207. psSHP->panRecOffset[nShapeId] = nRecordOffset = psSHP->nFileSize;
  1208. psSHP->panRecSize[nShapeId] = nRecordSize-8;
  1209. psSHP->nFileSize += nRecordSize;
  1210. }
  1211. else
  1212. {
  1213. nRecordOffset = psSHP->panRecOffset[nShapeId];
  1214. psSHP->panRecSize[nShapeId] = nRecordSize-8;
  1215. }
  1216. /* -------------------------------------------------------------------- */
  1217. /* Set the shape type, record number, and record size. */
  1218. /* -------------------------------------------------------------------- */
  1219. i32 = nShapeId+1; /* record # */
  1220. if( !bBigEndian ) SwapWord( 4, &i32 );
  1221. ByteCopy( &i32, pabyRec, 4 );
  1222. i32 = (nRecordSize-8)/2; /* record size */
  1223. if( !bBigEndian ) SwapWord( 4, &i32 );
  1224. ByteCopy( &i32, pabyRec + 4, 4 );
  1225. i32 = psObject->nSHPType; /* shape type */
  1226. if( bBigEndian ) SwapWord( 4, &i32 );
  1227. ByteCopy( &i32, pabyRec + 8, 4 );
  1228. /* -------------------------------------------------------------------- */
  1229. /* Write out record. */
  1230. /* -------------------------------------------------------------------- */
  1231. if( psSHP->sHooks.FSeek( psSHP->fpSHP, nRecordOffset, 0 ) != 0
  1232. || psSHP->sHooks.FWrite( pabyRec, nRecordSize, 1, psSHP->fpSHP ) < 1 )
  1233. {
  1234. psSHP->sHooks.Error( "Error in psSHP->sHooks.FSeek() or fwrite() writing object to .shp file." );
  1235. free( pabyRec );
  1236. return -1;
  1237. }
  1238. free( pabyRec );
  1239. /* -------------------------------------------------------------------- */
  1240. /* Expand file wide bounds based on this shape. */
  1241. /* -------------------------------------------------------------------- */
  1242. if( psSHP->adBoundsMin[0] == 0.0
  1243. && psSHP->adBoundsMax[0] == 0.0
  1244. && psSHP->adBoundsMin[1] == 0.0
  1245. && psSHP->adBoundsMax[1] == 0.0 )
  1246. {
  1247. if( psObject->nSHPType == SHPT_NULL || psObject->nVertices == 0 )
  1248. {
  1249. psSHP->adBoundsMin[0] = psSHP->adBoundsMax[0] = 0.0;
  1250. psSHP->adBoundsMin[1] = psSHP->adBoundsMax[1] = 0.0;
  1251. psSHP->adBoundsMin[2] = psSHP->adBoundsMax[2] = 0.0;
  1252. psSHP->adBoundsMin[3] = psSHP->adBoundsMax[3] = 0.0;
  1253. }
  1254. else
  1255. {
  1256. psSHP->adBoundsMin[0] = psSHP->adBoundsMax[0] = psObject->padfX[0];
  1257. psSHP->adBoundsMin[1] = psSHP->adBoundsMax[1] = psObject->padfY[0];
  1258. psSHP->adBoundsMin[2] = psSHP->adBoundsMax[2] = psObject->padfZ[0];
  1259. psSHP->adBoundsMin[3] = psSHP->adBoundsMax[3] = psObject->padfM[0];
  1260. }
  1261. }
  1262. for( i = 0; i < psObject->nVertices; i++ )
  1263. {
  1264. psSHP->adBoundsMin[0] = MIN(psSHP->adBoundsMin[0],psObject->padfX[i]);
  1265. psSHP->adBoundsMin[1] = MIN(psSHP->adBoundsMin[1],psObject->padfY[i]);
  1266. psSHP->adBoundsMin[2] = MIN(psSHP->adBoundsMin[2],psObject->padfZ[i]);
  1267. psSHP->adBoundsMin[3] = MIN(psSHP->adBoundsMin[3],psObject->padfM[i]);
  1268. psSHP->adBoundsMax[0] = MAX(psSHP->adBoundsMax[0],psObject->padfX[i]);
  1269. psSHP->adBoundsMax[1] = MAX(psSHP->adBoundsMax[1],psObject->padfY[i]);
  1270. psSHP->adBoundsMax[2] = MAX(psSHP->adBoundsMax[2],psObject->padfZ[i]);
  1271. psSHP->adBoundsMax[3] = MAX(psSHP->adBoundsMax[3],psObject->padfM[i]);
  1272. }
  1273. return( nShapeId );
  1274. }
  1275. /************************************************************************/
  1276. /* SHPReadObject() */
  1277. /* */
  1278. /* Read the vertices, parts, and other non-attribute information */
  1279. /* for one shape. */
  1280. /************************************************************************/
  1281. SHPObject SHPAPI_CALL1(*)
  1282. SHPReadObject( SHPHandle psSHP, int hEntity )
  1283. {
  1284. int nEntitySize, nRequiredSize;
  1285. SHPObject *psShape;
  1286. char pszErrorMsg[128];
  1287. /* -------------------------------------------------------------------- */
  1288. /* Validate the record/entity number. */
  1289. /* -------------------------------------------------------------------- */
  1290. if( hEntity < 0 || hEntity >= psSHP->nRecords )
  1291. return( NULL );
  1292. /* -------------------------------------------------------------------- */
  1293. /* Ensure our record buffer is large enough. */
  1294. /* -------------------------------------------------------------------- */
  1295. nEntitySize = psSHP->panRecSize[hEntity]+8;
  1296. if( nEntitySize > psSHP->nBufSize )
  1297. {
  1298. psSHP->pabyRec = (uchar *) SfRealloc(psSHP->pabyRec,nEntitySize);
  1299. if (psSHP->pabyRec == NULL)
  1300. {
  1301. char szError[200];
  1302. /* Reallocate previous successfull size for following features */
  1303. psSHP->pabyRec = malloc(psSHP->nBufSize);
  1304. sprintf( szError,
  1305. "Not enough memory to allocate requested memory (nBufSize=%d). "
  1306. "Probably broken SHP file", psSHP->nBufSize );
  1307. psSHP->sHooks.Error( szError );
  1308. return NULL;
  1309. }
  1310. /* Only set new buffer size after successfull alloc */
  1311. psSHP->nBufSize = nEntitySize;
  1312. }
  1313. /* In case we were not able to reallocate the buffer on a previous step */
  1314. if (psSHP->pabyRec == NULL)
  1315. {
  1316. return NULL;
  1317. }
  1318. /* -------------------------------------------------------------------- */
  1319. /* Read the record. */
  1320. /* -------------------------------------------------------------------- */
  1321. if( psSHP->sHooks.FSeek( psSHP->fpSHP, psSHP->panRecOffset[hEntity], 0 ) != 0
  1322. || psSHP->sHooks.FRead( psSHP->pabyRec, nEntitySize, 1,
  1323. psSHP->fpSHP ) != 1 )
  1324. {
  1325. /*
  1326. * TODO - mloskot: Consider detailed diagnostics of shape file,
  1327. * for example to detect if file is truncated.
  1328. */
  1329. psSHP->sHooks.Error( "Error in fseek() or fread() reading object from .shp file." );
  1330. return NULL;
  1331. }
  1332. /* -------------------------------------------------------------------- */
  1333. /* Allocate and minimally initialize the object. */
  1334. /* -------------------------------------------------------------------- */
  1335. psShape = (SHPObject *) calloc(1,sizeof(SHPObject));
  1336. psShape->nShapeId = hEntity;
  1337. psShape->bMeasureIsUsed = FALSE;
  1338. if ( 8 + 4 > nEntitySize )
  1339. {
  1340. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nEntitySize = %d",
  1341. hEntity, nEntitySize);
  1342. psSHP->sHooks.Error( pszErrorMsg );
  1343. SHPDestroyObject(psShape);
  1344. return NULL;
  1345. }
  1346. memcpy( &psShape->nSHPType, psSHP->pabyRec + 8, 4 );
  1347. if( bBigEndian ) SwapWord( 4, &(psShape->nSHPType) );
  1348. /* ==================================================================== */
  1349. /* Extract vertices for a Polygon or Arc. */
  1350. /* ==================================================================== */
  1351. if( psShape->nSHPType == SHPT_POLYGON || psShape->nSHPType == SHPT_ARC
  1352. || psShape->nSHPType == SHPT_POLYGONZ
  1353. || psShape->nSHPType == SHPT_POLYGONM
  1354. || psShape->nSHPType == SHPT_ARCZ
  1355. || psShape->nSHPType == SHPT_ARCM
  1356. || psShape->nSHPType == SHPT_MULTIPATCH )
  1357. {
  1358. int32 nPoints, nParts;
  1359. int i, nOffset;
  1360. if ( 40 + 8 + 4 > nEntitySize )
  1361. {
  1362. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nEntitySize = %d",
  1363. hEntity, nEntitySize);
  1364. psSHP->sHooks.Error( pszErrorMsg );
  1365. SHPDestroyObject(psShape);
  1366. return NULL;
  1367. }
  1368. /* -------------------------------------------------------------------- */
  1369. /* Get the X/Y bounds. */
  1370. /* -------------------------------------------------------------------- */
  1371. memcpy( &(psShape->dfXMin), psSHP->pabyRec + 8 + 4, 8 );
  1372. memcpy( &(psShape->dfYMin), psSHP->pabyRec + 8 + 12, 8 );
  1373. memcpy( &(psShape->dfXMax), psSHP->pabyRec + 8 + 20, 8 );
  1374. memcpy( &(psShape->dfYMax), psSHP->pabyRec + 8 + 28, 8 );
  1375. if( bBigEndian ) SwapWord( 8, &(psShape->dfXMin) );
  1376. if( bBigEndian ) SwapWord( 8, &(psShape->dfYMin) );
  1377. if( bBigEndian ) SwapWord( 8, &(psShape->dfXMax) );
  1378. if( bBigEndian ) SwapWord( 8, &(psShape->dfYMax) );
  1379. /* -------------------------------------------------------------------- */
  1380. /* Extract part/point count, and build vertex and part arrays */
  1381. /* to proper size. */
  1382. /* -------------------------------------------------------------------- */
  1383. memcpy( &nPoints, psSHP->pabyRec + 40 + 8, 4 );
  1384. memcpy( &nParts, psSHP->pabyRec + 36 + 8, 4 );
  1385. if( bBigEndian ) SwapWord( 4, &nPoints );
  1386. if( bBigEndian ) SwapWord( 4, &nParts );
  1387. if (nPoints < 0 || nParts < 0 ||
  1388. nPoints > 50 * 1000 * 1000 || nParts > 10 * 1000 * 1000)
  1389. {
  1390. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d, nPoints=%d, nParts=%d.",
  1391. hEntity, nPoints, nParts);
  1392. psSHP->sHooks.Error( pszErrorMsg );
  1393. SHPDestroyObject(psShape);
  1394. return NULL;
  1395. }
  1396. /* With the previous checks on nPoints and nParts, */
  1397. /* we should not overflow here and after */
  1398. /* since 50 M * (16 + 8 + 8) = 1 600 MB */
  1399. nRequiredSize = 44 + 8 + 4 * nParts + 16 * nPoints;
  1400. if ( psShape->nSHPType == SHPT_POLYGONZ
  1401. || psShape->nSHPType == SHPT_ARCZ
  1402. || psShape->nSHPType == SHPT_MULTIPATCH )
  1403. {
  1404. nRequiredSize += 16 + 8 * nPoints;
  1405. }
  1406. if( psShape->nSHPType == SHPT_MULTIPATCH )
  1407. {
  1408. nRequiredSize += 4 * nParts;
  1409. }
  1410. if (nRequiredSize > nEntitySize)
  1411. {
  1412. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d, nPoints=%d, nParts=%d, nEntitySize=%d.",
  1413. hEntity, nPoints, nParts, nEntitySize);
  1414. psSHP->sHooks.Error( pszErrorMsg );
  1415. SHPDestroyObject(psShape);
  1416. return NULL;
  1417. }
  1418. psShape->nVertices = nPoints;
  1419. psShape->padfX = (double *) calloc(nPoints,sizeof(double));
  1420. psShape->padfY = (double *) calloc(nPoints,sizeof(double));
  1421. psShape->padfZ = (double *) calloc(nPoints,sizeof(double));
  1422. psShape->padfM = (double *) calloc(nPoints,sizeof(double));
  1423. psShape->nParts = nParts;
  1424. psShape->panPartStart = (int *) calloc(nParts,sizeof(int));
  1425. psShape->panPartType = (int *) calloc(nParts,sizeof(int));
  1426. if (psShape->padfX == NULL ||
  1427. psShape->padfY == NULL ||
  1428. psShape->padfZ == NULL ||
  1429. psShape->padfM == NULL ||
  1430. psShape->panPartStart == NULL ||
  1431. psShape->panPartType == NULL)
  1432. {
  1433. snprintf(pszErrorMsg, 128,
  1434. "Not enough memory to allocate requested memory (nPoints=%d, nParts=%d) for shape %d. "
  1435. "Probably broken SHP file", hEntity, nPoints, nParts );
  1436. psSHP->sHooks.Error( pszErrorMsg );
  1437. SHPDestroyObject(psShape);
  1438. return NULL;
  1439. }
  1440. for( i = 0; i < nParts; i++ )
  1441. psShape->panPartType[i] = SHPP_RING;
  1442. /* -------------------------------------------------------------------- */
  1443. /* Copy out the part array from the record. */
  1444. /* -------------------------------------------------------------------- */
  1445. memcpy( psShape->panPartStart, psSHP->pabyRec + 44 + 8, 4 * nParts );
  1446. for( i = 0; i < nParts; i++ )
  1447. {
  1448. if( bBigEndian ) SwapWord( 4, psShape->panPartStart+i );
  1449. /* We check that the offset is inside the vertex array */
  1450. if (psShape->panPartStart[i] < 0 ||
  1451. psShape->panPartStart[i] >= psShape->nVertices)
  1452. {
  1453. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : panPartStart[%d] = %d, nVertices = %d",
  1454. hEntity, i, psShape->panPartStart[i], psShape->nVertices);
  1455. psSHP->sHooks.Error( pszErrorMsg );
  1456. SHPDestroyObject(psShape);
  1457. return NULL;
  1458. }
  1459. if (i > 0 && psShape->panPartStart[i] <= psShape->panPartStart[i-1])
  1460. {
  1461. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : panPartStart[%d] = %d, panPartStart[%d] = %d",
  1462. hEntity, i, psShape->panPartStart[i], i - 1, psShape->panPartStart[i - 1]);
  1463. psSHP->sHooks.Error( pszErrorMsg );
  1464. SHPDestroyObject(psShape);
  1465. return NULL;
  1466. }
  1467. }
  1468. nOffset = 44 + 8 + 4*nParts;
  1469. /* -------------------------------------------------------------------- */
  1470. /* If this is a multipatch, we will also have parts types. */
  1471. /* -------------------------------------------------------------------- */
  1472. if( psShape->nSHPType == SHPT_MULTIPATCH )
  1473. {
  1474. memcpy( psShape->panPartType, psSHP->pabyRec + nOffset, 4*nParts );
  1475. for( i = 0; i < nParts; i++ )
  1476. {
  1477. if( bBigEndian ) SwapWord( 4, psShape->panPartType+i );
  1478. }
  1479. nOffset += 4*nParts;
  1480. }
  1481. /* -------------------------------------------------------------------- */
  1482. /* Copy out the vertices from the record. */
  1483. /* -------------------------------------------------------------------- */
  1484. for( i = 0; i < nPoints; i++ )
  1485. {
  1486. memcpy(psShape->padfX + i,
  1487. psSHP->pabyRec + nOffset + i * 16,
  1488. 8 );
  1489. memcpy(psShape->padfY + i,
  1490. psSHP->pabyRec + nOffset + i * 16 + 8,
  1491. 8 );
  1492. if( bBigEndian ) SwapWord( 8, psShape->padfX + i );
  1493. if( bBigEndian ) SwapWord( 8, psShape->padfY + i );
  1494. }
  1495. nOffset += 16*nPoints;
  1496. /* -------------------------------------------------------------------- */
  1497. /* If we have a Z coordinate, collect that now. */
  1498. /* -------------------------------------------------------------------- */
  1499. if( psShape->nSHPType == SHPT_POLYGONZ
  1500. || psShape->nSHPType == SHPT_ARCZ
  1501. || psShape->nSHPType == SHPT_MULTIPATCH )
  1502. {
  1503. memcpy( &(psShape->dfZMin), psSHP->pabyRec + nOffset, 8 );
  1504. memcpy( &(psShape->dfZMax), psSHP->pabyRec + nOffset + 8, 8 );
  1505. if( bBigEndian ) SwapWord( 8, &(psShape->dfZMin) );
  1506. if( bBigEndian ) SwapWord( 8, &(psShape->dfZMax) );
  1507. for( i = 0; i < nPoints; i++ )
  1508. {
  1509. memcpy( psShape->padfZ + i,
  1510. psSHP->pabyRec + nOffset + 16 + i*8, 8 );
  1511. if( bBigEndian ) SwapWord( 8, psShape->padfZ + i );
  1512. }
  1513. nOffset += 16 + 8*nPoints;
  1514. }
  1515. /* -------------------------------------------------------------------- */
  1516. /* If we have a M measure value, then read it now. We assume */
  1517. /* that the measure can be present for any shape if the size is */
  1518. /* big enough, but really it will only occur for the Z shapes */
  1519. /* (options), and the M shapes. */
  1520. /* -------------------------------------------------------------------- */
  1521. if( nEntitySize >= nOffset + 16 + 8*nPoints )
  1522. {
  1523. memcpy( &(psShape->dfMMin), psSHP->pabyRec + nOffset, 8 );
  1524. memcpy( &(psShape->dfMMax), psSHP->pabyRec + nOffset + 8, 8 );
  1525. if( bBigEndian ) SwapWord( 8, &(psShape->dfMMin) );
  1526. if( bBigEndian ) SwapWord( 8, &(psShape->dfMMax) );
  1527. for( i = 0; i < nPoints; i++ )
  1528. {
  1529. memcpy( psShape->padfM + i,
  1530. psSHP->pabyRec + nOffset + 16 + i*8, 8 );
  1531. if( bBigEndian ) SwapWord( 8, psShape->padfM + i );
  1532. }
  1533. psShape->bMeasureIsUsed = TRUE;
  1534. }
  1535. }
  1536. /* ==================================================================== */
  1537. /* Extract vertices for a MultiPoint. */
  1538. /* ==================================================================== */
  1539. else if( psShape->nSHPType == SHPT_MULTIPOINT
  1540. || psShape->nSHPType == SHPT_MULTIPOINTM
  1541. || psShape->nSHPType == SHPT_MULTIPOINTZ )
  1542. {
  1543. int32 nPoints;
  1544. int i, nOffset;
  1545. if ( 44 + 4 > nEntitySize )
  1546. {
  1547. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nEntitySize = %d",
  1548. hEntity, nEntitySize);
  1549. psSHP->sHooks.Error( pszErrorMsg );
  1550. SHPDestroyObject(psShape);
  1551. return NULL;
  1552. }
  1553. memcpy( &nPoints, psSHP->pabyRec + 44, 4 );
  1554. if( bBigEndian ) SwapWord( 4, &nPoints );
  1555. if (nPoints < 0 || nPoints > 50 * 1000 * 1000)
  1556. {
  1557. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nPoints = %d",
  1558. hEntity, nPoints);
  1559. psSHP->sHooks.Error( pszErrorMsg );
  1560. SHPDestroyObject(psShape);
  1561. return NULL;
  1562. }
  1563. nRequiredSize = 48 + nPoints * 16;
  1564. if( psShape->nSHPType == SHPT_MULTIPOINTZ )
  1565. {
  1566. nRequiredSize += 16 + nPoints * 8;
  1567. }
  1568. if (nRequiredSize > nEntitySize)
  1569. {
  1570. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nPoints = %d, nEntitySize = %d",
  1571. hEntity, nPoints, nEntitySize);
  1572. psSHP->sHooks.Error( pszErrorMsg );
  1573. SHPDestroyObject(psShape);
  1574. return NULL;
  1575. }
  1576. psShape->nVertices = nPoints;
  1577. psShape->padfX = (double *) calloc(nPoints,sizeof(double));
  1578. psShape->padfY = (double *) calloc(nPoints,sizeof(double));
  1579. psShape->padfZ = (double *) calloc(nPoints,sizeof(double));
  1580. psShape->padfM = (double *) calloc(nPoints,sizeof(double));
  1581. if (psShape->padfX == NULL ||
  1582. psShape->padfY == NULL ||
  1583. psShape->padfZ == NULL ||
  1584. psShape->padfM == NULL)
  1585. {
  1586. snprintf(pszErrorMsg, 128,
  1587. "Not enough memory to allocate requested memory (nPoints=%d) for shape %d. "
  1588. "Probably broken SHP file", hEntity, nPoints );
  1589. psSHP->sHooks.Error( pszErrorMsg );
  1590. SHPDestroyObject(psShape);
  1591. return NULL;
  1592. }
  1593. for( i = 0; i < nPoints; i++ )
  1594. {
  1595. memcpy(psShape->padfX+i, psSHP->pabyRec + 48 + 16 * i, 8 );
  1596. memcpy(psShape->padfY+i, psSHP->pabyRec + 48 + 16 * i + 8, 8 );
  1597. if( bBigEndian ) SwapWord( 8, psShape->padfX + i );
  1598. if( bBigEndian ) SwapWord( 8, psShape->padfY + i );
  1599. }
  1600. nOffset = 48 + 16*nPoints;
  1601. /* -------------------------------------------------------------------- */
  1602. /* Get the X/Y bounds. */
  1603. /* -------------------------------------------------------------------- */
  1604. memcpy( &(psShape->dfXMin), psSHP->pabyRec + 8 + 4, 8 );
  1605. memcpy( &(psShape->dfYMin), psSHP->pabyRec + 8 + 12, 8 );
  1606. memcpy( &(psShape->dfXMax), psSHP->pabyRec + 8 + 20, 8 );
  1607. memcpy( &(psShape->dfYMax), psSHP->pabyRec + 8 + 28, 8 );
  1608. if( bBigEndian ) SwapWord( 8, &(psShape->dfXMin) );
  1609. if( bBigEndian ) SwapWord( 8, &(psShape->dfYMin) );
  1610. if( bBigEndian ) SwapWord( 8, &(psShape->dfXMax) );
  1611. if( bBigEndian ) SwapWord( 8, &(psShape->dfYMax) );
  1612. /* -------------------------------------------------------------------- */
  1613. /* If we have a Z coordinate, collect that now. */
  1614. /* -------------------------------------------------------------------- */
  1615. if( psShape->nSHPType == SHPT_MULTIPOINTZ )
  1616. {
  1617. memcpy( &(psShape->dfZMin), psSHP->pabyRec + nOffset, 8 );
  1618. memcpy( &(psShape->dfZMax), psSHP->pabyRec + nOffset + 8, 8 );
  1619. if( bBigEndian ) SwapWord( 8, &(psShape->dfZMin) );
  1620. if( bBigEndian ) SwapWord( 8, &(psShape->dfZMax) );
  1621. for( i = 0; i < nPoints; i++ )
  1622. {
  1623. memcpy( psShape->padfZ + i,
  1624. psSHP->pabyRec + nOffset + 16 + i*8, 8 );
  1625. if( bBigEndian ) SwapWord( 8, psShape->padfZ + i );
  1626. }
  1627. nOffset += 16 + 8*nPoints;
  1628. }
  1629. /* -------------------------------------------------------------------- */
  1630. /* If we have a M measure value, then read it now. We assume */
  1631. /* that the measure can be present for any shape if the size is */
  1632. /* big enough, but really it will only occur for the Z shapes */
  1633. /* (options), and the M shapes. */
  1634. /* -------------------------------------------------------------------- */
  1635. if( nEntitySize >= nOffset + 16 + 8*nPoints )
  1636. {
  1637. memcpy( &(psShape->dfMMin), psSHP->pabyRec + nOffset, 8 );
  1638. memcpy( &(psShape->dfMMax), psSHP->pabyRec + nOffset + 8, 8 );
  1639. if( bBigEndian ) SwapWord( 8, &(psShape->dfMMin) );
  1640. if( bBigEndian ) SwapWord( 8, &(psShape->dfMMax) );
  1641. for( i = 0; i < nPoints; i++ )
  1642. {
  1643. memcpy( psShape->padfM + i,
  1644. psSHP->pabyRec + nOffset + 16 + i*8, 8 );
  1645. if( bBigEndian ) SwapWord( 8, psShape->padfM + i );
  1646. }
  1647. psShape->bMeasureIsUsed = TRUE;
  1648. }
  1649. }
  1650. /* ==================================================================== */
  1651. /* Extract vertices for a point. */
  1652. /* ==================================================================== */
  1653. else if( psShape->nSHPType == SHPT_POINT
  1654. || psShape->nSHPType == SHPT_POINTM
  1655. || psShape->nSHPType == SHPT_POINTZ )
  1656. {
  1657. int nOffset;
  1658. psShape->nVertices = 1;
  1659. psShape->padfX = (double *) calloc(1,sizeof(double));
  1660. psShape->padfY = (double *) calloc(1,sizeof(double));
  1661. psShape->padfZ = (double *) calloc(1,sizeof(double));
  1662. psShape->padfM = (double *) calloc(1,sizeof(double));
  1663. if (20 + 8 + (( psShape->nSHPType == SHPT_POINTZ ) ? 8 : 0)> nEntitySize)
  1664. {
  1665. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nEntitySize = %d",
  1666. hEntity, nEntitySize);
  1667. psSHP->sHooks.Error( pszErrorMsg );
  1668. SHPDestroyObject(psShape);
  1669. return NULL;
  1670. }
  1671. memcpy( psShape->padfX, psSHP->pabyRec + 12, 8 );
  1672. memcpy( psShape->padfY, psSHP->pabyRec + 20, 8 );
  1673. if( bBigEndian ) SwapWord( 8, psShape->padfX );
  1674. if( bBigEndian ) SwapWord( 8, psShape->padfY );
  1675. nOffset = 20 + 8;
  1676. /* -------------------------------------------------------------------- */
  1677. /* If we have a Z coordinate, collect that now. */
  1678. /* -------------------------------------------------------------------- */
  1679. if( psShape->nSHPType == SHPT_POINTZ )
  1680. {
  1681. memcpy( psShape->padfZ, psSHP->pabyRec + nOffset, 8 );
  1682. if( bBigEndian ) SwapWord( 8, psShape->padfZ );
  1683. nOffset += 8;
  1684. }
  1685. /* -------------------------------------------------------------------- */
  1686. /* If we have a M measure value, then read it now. We assume */
  1687. /* that the measure can be present for any shape if the size is */
  1688. /* big enough, but really it will only occur for the Z shapes */
  1689. /* (options), and the M shapes. */
  1690. /* -------------------------------------------------------------------- */
  1691. if( nEntitySize >= nOffset + 8 )
  1692. {
  1693. memcpy( psShape->padfM, psSHP->pabyRec + nOffset, 8 );
  1694. if( bBigEndian ) SwapWord( 8, psShape->padfM );
  1695. psShape->bMeasureIsUsed = TRUE;
  1696. }
  1697. /* -------------------------------------------------------------------- */
  1698. /* Since no extents are supplied in the record, we will apply */
  1699. /* them from the single vertex. */
  1700. /* -------------------------------------------------------------------- */
  1701. psShape->dfXMin = psShape->dfXMax = psShape->padfX[0];
  1702. psShape->dfYMin = psShape->dfYMax = psShape->padfY[0];
  1703. psShape->dfZMin = psShape->dfZMax = psShape->padfZ[0];
  1704. psShape->dfMMin = psShape->dfMMax = psShape->padfM[0];
  1705. }
  1706. return( psShape );
  1707. }
  1708. /************************************************************************/
  1709. /* SHPTypeName() */
  1710. /************************************************************************/
  1711. const char SHPAPI_CALL1(*)
  1712. SHPTypeName( int nSHPType )
  1713. {
  1714. switch( nSHPType )
  1715. {
  1716. case SHPT_NULL:
  1717. return "NullShape";
  1718. case SHPT_POINT:
  1719. return "Point";
  1720. case SHPT_ARC:
  1721. return "Arc";
  1722. case SHPT_POLYGON:
  1723. return "Polygon";
  1724. case SHPT_MULTIPOINT:
  1725. return "MultiPoint";
  1726. case SHPT_POINTZ:
  1727. return "PointZ";
  1728. case SHPT_ARCZ:
  1729. return "ArcZ";
  1730. case SHPT_POLYGONZ:
  1731. return "PolygonZ";
  1732. case SHPT_MULTIPOINTZ:
  1733. return "MultiPointZ";
  1734. case SHPT_POINTM:
  1735. return "PointM";
  1736. case SHPT_ARCM:
  1737. return "ArcM";
  1738. case SHPT_POLYGONM:
  1739. return "PolygonM";
  1740. case SHPT_MULTIPOINTM:
  1741. return "MultiPointM";
  1742. case SHPT_MULTIPATCH:
  1743. return "MultiPatch";
  1744. default:
  1745. return "UnknownShapeType";
  1746. }
  1747. }
  1748. /************************************************************************/
  1749. /* SHPPartTypeName() */
  1750. /************************************************************************/
  1751. const char SHPAPI_CALL1(*)
  1752. SHPPartTypeName( int nPartType )
  1753. {
  1754. switch( nPartType )
  1755. {
  1756. case SHPP_TRISTRIP:
  1757. return "TriangleStrip";
  1758. case SHPP_TRIFAN:
  1759. return "TriangleFan";
  1760. case SHPP_OUTERRING:
  1761. return "OuterRing";
  1762. case SHPP_INNERRING:
  1763. return "InnerRing";
  1764. case SHPP_FIRSTRING:
  1765. return "FirstRing";
  1766. case SHPP_RING:
  1767. return "Ring";
  1768. default:
  1769. return "UnknownPartType";
  1770. }
  1771. }
  1772. /************************************************************************/
  1773. /* SHPDestroyObject() */
  1774. /************************************************************************/
  1775. void SHPAPI_CALL
  1776. SHPDestroyObject( SHPObject * psShape )
  1777. {
  1778. if( psShape == NULL )
  1779. return;
  1780. if( psShape->padfX != NULL )
  1781. free( psShape->padfX );
  1782. if( psShape->padfY != NULL )
  1783. free( psShape->padfY );
  1784. if( psShape->padfZ != NULL )
  1785. free( psShape->padfZ );
  1786. if( psShape->padfM != NULL )
  1787. free( psShape->padfM );
  1788. if( psShape->panPartStart != NULL )
  1789. free( psShape->panPartStart );
  1790. if( psShape->panPartType != NULL )
  1791. free( psShape->panPartType );
  1792. free( psShape );
  1793. }
  1794. /************************************************************************/
  1795. /* SHPRewindObject() */
  1796. /* */
  1797. /* Reset the winding of polygon objects to adhere to the */
  1798. /* specification. */
  1799. /************************************************************************/
  1800. int SHPAPI_CALL
  1801. SHPRewindObject( SHPHandle hSHP, SHPObject * psObject )
  1802. {
  1803. int iOpRing, bAltered = 0;
  1804. /* -------------------------------------------------------------------- */
  1805. /* Do nothing if this is not a polygon object. */
  1806. /* -------------------------------------------------------------------- */
  1807. if( psObject->nSHPType != SHPT_POLYGON
  1808. && psObject->nSHPType != SHPT_POLYGONZ
  1809. && psObject->nSHPType != SHPT_POLYGONM )
  1810. return 0;
  1811. if( psObject->nVertices == 0 || psObject->nParts == 0 )
  1812. return 0;
  1813. /* -------------------------------------------------------------------- */
  1814. /* Process each of the rings. */
  1815. /* -------------------------------------------------------------------- */
  1816. for( iOpRing = 0; iOpRing < psObject->nParts; iOpRing++ )
  1817. {
  1818. int bInner, iVert, nVertCount, nVertStart, iCheckRing;
  1819. double dfSum, dfTestX, dfTestY;
  1820. /* -------------------------------------------------------------------- */
  1821. /* Determine if this ring is an inner ring or an outer ring */
  1822. /* relative to all the other rings. For now we assume the */
  1823. /* first ring is outer and all others are inner, but eventually */
  1824. /* we need to fix this to handle multiple island polygons and */
  1825. /* unordered sets of rings. */
  1826. /* */
  1827. /* -------------------------------------------------------------------- */
  1828. /* Use point in the middle of segment to avoid testing
  1829. * common points of rings.
  1830. */
  1831. dfTestX = ( psObject->padfX[psObject->panPartStart[iOpRing]]
  1832. + psObject->padfX[psObject->panPartStart[iOpRing] + 1] ) / 2;
  1833. dfTestY = ( psObject->padfY[psObject->panPartStart[iOpRing]]
  1834. + psObject->padfY[psObject->panPartStart[iOpRing] + 1] ) / 2;
  1835. bInner = FALSE;
  1836. for( iCheckRing = 0; iCheckRing < psObject->nParts; iCheckRing++ )
  1837. {
  1838. int iEdge;
  1839. if( iCheckRing == iOpRing )
  1840. continue;
  1841. nVertStart = psObject->panPartStart[iCheckRing];
  1842. if( iCheckRing == psObject->nParts-1 )
  1843. nVertCount = psObject->nVertices
  1844. - psObject->panPartStart[iCheckRing];
  1845. else
  1846. nVertCount = psObject->panPartStart[iCheckRing+1]
  1847. - psObject->panPartStart[iCheckRing];
  1848. for( iEdge = 0; iEdge < nVertCount; iEdge++ )
  1849. {
  1850. int iNext;
  1851. if( iEdge < nVertCount-1 )
  1852. iNext = iEdge+1;
  1853. else
  1854. iNext = 0;
  1855. /* Rule #1:
  1856. * Test whether the edge 'straddles' the horizontal ray from the test point (dfTestY,dfTestY)
  1857. * The rule #1 also excludes edges collinear with the ray.
  1858. */
  1859. if ( ( psObject->padfY[iEdge+nVertStart] < dfTestY
  1860. && dfTestY <= psObject->padfY[iNext+nVertStart] )
  1861. || ( psObject->padfY[iNext+nVertStart] < dfTestY
  1862. && dfTestY <= psObject->padfY[iEdge+nVertStart] ) )
  1863. {
  1864. /* Rule #2:
  1865. * Test if edge-ray intersection is on the right from the test point (dfTestY,dfTestY)
  1866. */
  1867. double const intersect =
  1868. ( psObject->padfX[iEdge+nVertStart]
  1869. + ( dfTestY - psObject->padfY[iEdge+nVertStart] )
  1870. / ( psObject->padfY[iNext+nVertStart] - psObject->padfY[iEdge+nVertStart] )
  1871. * ( psObject->padfX[iNext+nVertStart] - psObject->padfX[iEdge+nVertStart] ) );
  1872. if (intersect < dfTestX)
  1873. {
  1874. bInner = !bInner;
  1875. }
  1876. }
  1877. }
  1878. } /* for iCheckRing */
  1879. /* -------------------------------------------------------------------- */
  1880. /* Determine the current order of this ring so we will know if */
  1881. /* it has to be reversed. */
  1882. /* -------------------------------------------------------------------- */
  1883. nVertStart = psObject->panPartStart[iOpRing];
  1884. if( iOpRing == psObject->nParts-1 )
  1885. nVertCount = psObject->nVertices - psObject->panPartStart[iOpRing];
  1886. else
  1887. nVertCount = psObject->panPartStart[iOpRing+1]
  1888. - psObject->panPartStart[iOpRing];
  1889. dfSum = 0.0;
  1890. for( iVert = nVertStart; iVert < nVertStart+nVertCount-1; iVert++ )
  1891. {
  1892. dfSum += psObject->padfX[iVert] * psObject->padfY[iVert+1]
  1893. - psObject->padfY[iVert] * psObject->padfX[iVert+1];
  1894. }
  1895. dfSum += psObject->padfX[iVert] * psObject->padfY[nVertStart]
  1896. - psObject->padfY[iVert] * psObject->padfX[nVertStart];
  1897. /* -------------------------------------------------------------------- */
  1898. /* Reverse if necessary. */
  1899. /* -------------------------------------------------------------------- */
  1900. if( (dfSum < 0.0 && bInner) || (dfSum > 0.0 && !bInner) )
  1901. {
  1902. int i;
  1903. bAltered++;
  1904. for( i = 0; i < nVertCount/2; i++ )
  1905. {
  1906. double dfSaved;
  1907. /* Swap X */
  1908. dfSaved = psObject->padfX[nVertStart+i];
  1909. psObject->padfX[nVertStart+i] =
  1910. psObject->padfX[nVertStart+nVertCount-i-1];
  1911. psObject->padfX[nVertStart+nVertCount-i-1] = dfSaved;
  1912. /* Swap Y */
  1913. dfSaved = psObject->padfY[nVertStart+i];
  1914. psObject->padfY[nVertStart+i] =
  1915. psObject->padfY[nVertStart+nVertCount-i-1];
  1916. psObject->padfY[nVertStart+nVertCount-i-1] = dfSaved;
  1917. /* Swap Z */
  1918. if( psObject->padfZ )
  1919. {
  1920. dfSaved = psObject->padfZ[nVertStart+i];
  1921. psObject->padfZ[nVertStart+i] =
  1922. psObject->padfZ[nVertStart+nVertCount-i-1];
  1923. psObject->padfZ[nVertStart+nVertCount-i-1] = dfSaved;
  1924. }
  1925. /* Swap M */
  1926. if( psObject->padfM )
  1927. {
  1928. dfSaved = psObject->padfM[nVertStart+i];
  1929. psObject->padfM[nVertStart+i] =
  1930. psObject->padfM[nVertStart+nVertCount-i-1];
  1931. psObject->padfM[nVertStart+nVertCount-i-1] = dfSaved;
  1932. }
  1933. }
  1934. }
  1935. }
  1936. return bAltered;
  1937. }