shpopen.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  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. return( NULL );
  730. }
  731. sprintf( pszFullname, "%s.shx", pszBasename );
  732. fpSHX = psHooks->FOpen(pszFullname, "wb" );
  733. if( fpSHX == NULL )
  734. {
  735. psHooks->Error( "Failed to create file .shx file." );
  736. return( NULL );
  737. }
  738. free( pszFullname );
  739. free( pszBasename );
  740. /* -------------------------------------------------------------------- */
  741. /* Prepare header block for .shp file. */
  742. /* -------------------------------------------------------------------- */
  743. for( i = 0; i < 100; i++ )
  744. abyHeader[i] = 0;
  745. abyHeader[2] = 0x27; /* magic cookie */
  746. abyHeader[3] = 0x0a;
  747. i32 = 50; /* file size */
  748. ByteCopy( &i32, abyHeader+24, 4 );
  749. if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
  750. i32 = 1000; /* version */
  751. ByteCopy( &i32, abyHeader+28, 4 );
  752. if( bBigEndian ) SwapWord( 4, abyHeader+28 );
  753. i32 = nShapeType; /* shape type */
  754. ByteCopy( &i32, abyHeader+32, 4 );
  755. if( bBigEndian ) SwapWord( 4, abyHeader+32 );
  756. dValue = 0.0; /* set bounds */
  757. ByteCopy( &dValue, abyHeader+36, 8 );
  758. ByteCopy( &dValue, abyHeader+44, 8 );
  759. ByteCopy( &dValue, abyHeader+52, 8 );
  760. ByteCopy( &dValue, abyHeader+60, 8 );
  761. /* -------------------------------------------------------------------- */
  762. /* Write .shp file header. */
  763. /* -------------------------------------------------------------------- */
  764. if( psHooks->FWrite( abyHeader, 100, 1, fpSHP ) != 1 )
  765. {
  766. psHooks->Error( "Failed to write .shp header." );
  767. return NULL;
  768. }
  769. /* -------------------------------------------------------------------- */
  770. /* Prepare, and write .shx file header. */
  771. /* -------------------------------------------------------------------- */
  772. i32 = 50; /* file size */
  773. ByteCopy( &i32, abyHeader+24, 4 );
  774. if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
  775. if( psHooks->FWrite( abyHeader, 100, 1, fpSHX ) != 1 )
  776. {
  777. psHooks->Error( "Failed to write .shx header." );
  778. return NULL;
  779. }
  780. /* -------------------------------------------------------------------- */
  781. /* Close the files, and then open them as regular existing files. */
  782. /* -------------------------------------------------------------------- */
  783. psHooks->FClose( fpSHP );
  784. psHooks->FClose( fpSHX );
  785. return( SHPOpenLL( pszLayer, "r+b", psHooks ) );
  786. }
  787. /************************************************************************/
  788. /* _SHPSetBounds() */
  789. /* */
  790. /* Compute a bounds rectangle for a shape, and set it into the */
  791. /* indicated location in the record. */
  792. /************************************************************************/
  793. static void _SHPSetBounds( uchar * pabyRec, SHPObject * psShape )
  794. {
  795. ByteCopy( &(psShape->dfXMin), pabyRec + 0, 8 );
  796. ByteCopy( &(psShape->dfYMin), pabyRec + 8, 8 );
  797. ByteCopy( &(psShape->dfXMax), pabyRec + 16, 8 );
  798. ByteCopy( &(psShape->dfYMax), pabyRec + 24, 8 );
  799. if( bBigEndian )
  800. {
  801. SwapWord( 8, pabyRec + 0 );
  802. SwapWord( 8, pabyRec + 8 );
  803. SwapWord( 8, pabyRec + 16 );
  804. SwapWord( 8, pabyRec + 24 );
  805. }
  806. }
  807. /************************************************************************/
  808. /* SHPComputeExtents() */
  809. /* */
  810. /* Recompute the extents of a shape. Automatically done by */
  811. /* SHPCreateObject(). */
  812. /************************************************************************/
  813. void SHPAPI_CALL
  814. SHPComputeExtents( SHPObject * psObject )
  815. {
  816. int i;
  817. /* -------------------------------------------------------------------- */
  818. /* Build extents for this object. */
  819. /* -------------------------------------------------------------------- */
  820. if( psObject->nVertices > 0 )
  821. {
  822. psObject->dfXMin = psObject->dfXMax = psObject->padfX[0];
  823. psObject->dfYMin = psObject->dfYMax = psObject->padfY[0];
  824. psObject->dfZMin = psObject->dfZMax = psObject->padfZ[0];
  825. psObject->dfMMin = psObject->dfMMax = psObject->padfM[0];
  826. }
  827. for( i = 0; i < psObject->nVertices; i++ )
  828. {
  829. psObject->dfXMin = MIN(psObject->dfXMin, psObject->padfX[i]);
  830. psObject->dfYMin = MIN(psObject->dfYMin, psObject->padfY[i]);
  831. psObject->dfZMin = MIN(psObject->dfZMin, psObject->padfZ[i]);
  832. psObject->dfMMin = MIN(psObject->dfMMin, psObject->padfM[i]);
  833. psObject->dfXMax = MAX(psObject->dfXMax, psObject->padfX[i]);
  834. psObject->dfYMax = MAX(psObject->dfYMax, psObject->padfY[i]);
  835. psObject->dfZMax = MAX(psObject->dfZMax, psObject->padfZ[i]);
  836. psObject->dfMMax = MAX(psObject->dfMMax, psObject->padfM[i]);
  837. }
  838. }
  839. /************************************************************************/
  840. /* SHPCreateObject() */
  841. /* */
  842. /* Create a shape object. It should be freed with */
  843. /* SHPDestroyObject(). */
  844. /************************************************************************/
  845. SHPObject SHPAPI_CALL1(*)
  846. SHPCreateObject( int nSHPType, int nShapeId, int nParts,
  847. const int * panPartStart, const int * panPartType,
  848. int nVertices, const double *padfX, const double *padfY,
  849. const double * padfZ, const double * padfM )
  850. {
  851. SHPObject *psObject;
  852. int i, bHasM, bHasZ;
  853. psObject = (SHPObject *) calloc(1,sizeof(SHPObject));
  854. psObject->nSHPType = nSHPType;
  855. psObject->nShapeId = nShapeId;
  856. psObject->bMeasureIsUsed = FALSE;
  857. /* -------------------------------------------------------------------- */
  858. /* Establish whether this shape type has M, and Z values. */
  859. /* -------------------------------------------------------------------- */
  860. if( nSHPType == SHPT_ARCM
  861. || nSHPType == SHPT_POINTM
  862. || nSHPType == SHPT_POLYGONM
  863. || nSHPType == SHPT_MULTIPOINTM )
  864. {
  865. bHasM = TRUE;
  866. bHasZ = FALSE;
  867. }
  868. else if( nSHPType == SHPT_ARCZ
  869. || nSHPType == SHPT_POINTZ
  870. || nSHPType == SHPT_POLYGONZ
  871. || nSHPType == SHPT_MULTIPOINTZ
  872. || nSHPType == SHPT_MULTIPATCH )
  873. {
  874. bHasM = TRUE;
  875. bHasZ = TRUE;
  876. }
  877. else
  878. {
  879. bHasM = FALSE;
  880. bHasZ = FALSE;
  881. }
  882. /* -------------------------------------------------------------------- */
  883. /* Capture parts. Note that part type is optional, and */
  884. /* defaults to ring. */
  885. /* -------------------------------------------------------------------- */
  886. if( nSHPType == SHPT_ARC || nSHPType == SHPT_POLYGON
  887. || nSHPType == SHPT_ARCM || nSHPType == SHPT_POLYGONM
  888. || nSHPType == SHPT_ARCZ || nSHPType == SHPT_POLYGONZ
  889. || nSHPType == SHPT_MULTIPATCH )
  890. {
  891. psObject->nParts = MAX(1,nParts);
  892. psObject->panPartStart = (int *)
  893. malloc(sizeof(int) * psObject->nParts);
  894. psObject->panPartType = (int *)
  895. malloc(sizeof(int) * psObject->nParts);
  896. psObject->panPartStart[0] = 0;
  897. psObject->panPartType[0] = SHPP_RING;
  898. for( i = 0; i < nParts; i++ )
  899. {
  900. psObject->panPartStart[i] = panPartStart[i];
  901. if( panPartType != NULL )
  902. psObject->panPartType[i] = panPartType[i];
  903. else
  904. psObject->panPartType[i] = SHPP_RING;
  905. }
  906. if( psObject->panPartStart[0] != 0 )
  907. psObject->panPartStart[0] = 0;
  908. }
  909. /* -------------------------------------------------------------------- */
  910. /* Capture vertices. Note that Z and M are optional, but X and */
  911. /* Y are not. */
  912. /* -------------------------------------------------------------------- */
  913. if( nVertices > 0 )
  914. {
  915. psObject->padfX = (double *) calloc(sizeof(double),nVertices);
  916. psObject->padfY = (double *) calloc(sizeof(double),nVertices);
  917. psObject->padfZ = (double *) calloc(sizeof(double),nVertices);
  918. psObject->padfM = (double *) calloc(sizeof(double),nVertices);
  919. assert( padfX != NULL );
  920. assert( padfY != NULL );
  921. for( i = 0; i < nVertices; i++ )
  922. {
  923. psObject->padfX[i] = padfX[i];
  924. psObject->padfY[i] = padfY[i];
  925. if( padfZ != NULL && bHasZ )
  926. psObject->padfZ[i] = padfZ[i];
  927. if( padfM != NULL && bHasM )
  928. psObject->padfM[i] = padfM[i];
  929. }
  930. if( padfM != NULL && bHasM )
  931. psObject->bMeasureIsUsed = TRUE;
  932. }
  933. /* -------------------------------------------------------------------- */
  934. /* Compute the extents. */
  935. /* -------------------------------------------------------------------- */
  936. psObject->nVertices = nVertices;
  937. SHPComputeExtents( psObject );
  938. return( psObject );
  939. }
  940. /************************************************************************/
  941. /* SHPCreateSimpleObject() */
  942. /* */
  943. /* Create a simple (common) shape object. Destroy with */
  944. /* SHPDestroyObject(). */
  945. /************************************************************************/
  946. SHPObject SHPAPI_CALL1(*)
  947. SHPCreateSimpleObject( int nSHPType, int nVertices,
  948. const double * padfX, const double * padfY,
  949. const double * padfZ )
  950. {
  951. return( SHPCreateObject( nSHPType, -1, 0, NULL, NULL,
  952. nVertices, padfX, padfY, padfZ, NULL ) );
  953. }
  954. /************************************************************************/
  955. /* SHPWriteObject() */
  956. /* */
  957. /* Write out the vertices of a new structure. Note that it is */
  958. /* only possible to write vertices at the end of the file. */
  959. /************************************************************************/
  960. int SHPAPI_CALL
  961. SHPWriteObject(SHPHandle psSHP, int nShapeId, SHPObject * psObject )
  962. {
  963. int nRecordOffset, i, nRecordSize=0;
  964. uchar *pabyRec;
  965. int32 i32;
  966. psSHP->bUpdated = TRUE;
  967. /* -------------------------------------------------------------------- */
  968. /* Ensure that shape object matches the type of the file it is */
  969. /* being written to. */
  970. /* -------------------------------------------------------------------- */
  971. assert( psObject->nSHPType == psSHP->nShapeType
  972. || psObject->nSHPType == SHPT_NULL );
  973. /* -------------------------------------------------------------------- */
  974. /* Ensure that -1 is used for appends. Either blow an */
  975. /* assertion, or if they are disabled, set the shapeid to -1 */
  976. /* for appends. */
  977. /* -------------------------------------------------------------------- */
  978. assert( nShapeId == -1
  979. || (nShapeId >= 0 && nShapeId < psSHP->nRecords) );
  980. if( nShapeId != -1 && nShapeId >= psSHP->nRecords )
  981. nShapeId = -1;
  982. /* -------------------------------------------------------------------- */
  983. /* Add the new entity to the in memory index. */
  984. /* -------------------------------------------------------------------- */
  985. if( nShapeId == -1 && psSHP->nRecords+1 > psSHP->nMaxRecords )
  986. {
  987. psSHP->nMaxRecords =(int) ( psSHP->nMaxRecords * 1.3 + 100);
  988. psSHP->panRecOffset = (int *)
  989. SfRealloc(psSHP->panRecOffset,sizeof(int) * psSHP->nMaxRecords );
  990. psSHP->panRecSize = (int *)
  991. SfRealloc(psSHP->panRecSize,sizeof(int) * psSHP->nMaxRecords );
  992. }
  993. /* -------------------------------------------------------------------- */
  994. /* Initialize record. */
  995. /* -------------------------------------------------------------------- */
  996. pabyRec = (uchar *) malloc(psObject->nVertices * 4 * sizeof(double)
  997. + psObject->nParts * 8 + 128);
  998. /* -------------------------------------------------------------------- */
  999. /* Extract vertices for a Polygon or Arc. */
  1000. /* -------------------------------------------------------------------- */
  1001. if( psObject->nSHPType == SHPT_POLYGON
  1002. || psObject->nSHPType == SHPT_POLYGONZ
  1003. || psObject->nSHPType == SHPT_POLYGONM
  1004. || psObject->nSHPType == SHPT_ARC
  1005. || psObject->nSHPType == SHPT_ARCZ
  1006. || psObject->nSHPType == SHPT_ARCM
  1007. || psObject->nSHPType == SHPT_MULTIPATCH )
  1008. {
  1009. int32 nPoints, nParts;
  1010. int i;
  1011. nPoints = psObject->nVertices;
  1012. nParts = psObject->nParts;
  1013. _SHPSetBounds( pabyRec + 12, psObject );
  1014. if( bBigEndian ) SwapWord( 4, &nPoints );
  1015. if( bBigEndian ) SwapWord( 4, &nParts );
  1016. ByteCopy( &nPoints, pabyRec + 40 + 8, 4 );
  1017. ByteCopy( &nParts, pabyRec + 36 + 8, 4 );
  1018. nRecordSize = 52;
  1019. /*
  1020. * Write part start positions.
  1021. */
  1022. ByteCopy( psObject->panPartStart, pabyRec + 44 + 8,
  1023. 4 * psObject->nParts );
  1024. for( i = 0; i < psObject->nParts; i++ )
  1025. {
  1026. if( bBigEndian ) SwapWord( 4, pabyRec + 44 + 8 + 4*i );
  1027. nRecordSize += 4;
  1028. }
  1029. /*
  1030. * Write multipatch part types if needed.
  1031. */
  1032. if( psObject->nSHPType == SHPT_MULTIPATCH )
  1033. {
  1034. memcpy( pabyRec + nRecordSize, psObject->panPartType,
  1035. 4*psObject->nParts );
  1036. for( i = 0; i < psObject->nParts; i++ )
  1037. {
  1038. if( bBigEndian ) SwapWord( 4, pabyRec + nRecordSize );
  1039. nRecordSize += 4;
  1040. }
  1041. }
  1042. /*
  1043. * Write the (x,y) vertex values.
  1044. */
  1045. for( i = 0; i < psObject->nVertices; i++ )
  1046. {
  1047. ByteCopy( psObject->padfX + i, pabyRec + nRecordSize, 8 );
  1048. ByteCopy( psObject->padfY + i, pabyRec + nRecordSize + 8, 8 );
  1049. if( bBigEndian )
  1050. SwapWord( 8, pabyRec + nRecordSize );
  1051. if( bBigEndian )
  1052. SwapWord( 8, pabyRec + nRecordSize + 8 );
  1053. nRecordSize += 2 * 8;
  1054. }
  1055. /*
  1056. * Write the Z coordinates (if any).
  1057. */
  1058. if( psObject->nSHPType == SHPT_POLYGONZ
  1059. || psObject->nSHPType == SHPT_ARCZ
  1060. || psObject->nSHPType == SHPT_MULTIPATCH )
  1061. {
  1062. ByteCopy( &(psObject->dfZMin), pabyRec + nRecordSize, 8 );
  1063. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1064. nRecordSize += 8;
  1065. ByteCopy( &(psObject->dfZMax), pabyRec + nRecordSize, 8 );
  1066. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1067. nRecordSize += 8;
  1068. for( i = 0; i < psObject->nVertices; i++ )
  1069. {
  1070. ByteCopy( psObject->padfZ + i, pabyRec + nRecordSize, 8 );
  1071. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1072. nRecordSize += 8;
  1073. }
  1074. }
  1075. /*
  1076. * Write the M values, if any.
  1077. */
  1078. if( psObject->bMeasureIsUsed
  1079. && (psObject->nSHPType == SHPT_POLYGONM
  1080. || psObject->nSHPType == SHPT_ARCM
  1081. #ifndef DISABLE_MULTIPATCH_MEASURE
  1082. || psObject->nSHPType == SHPT_MULTIPATCH
  1083. #endif
  1084. || psObject->nSHPType == SHPT_POLYGONZ
  1085. || psObject->nSHPType == SHPT_ARCZ) )
  1086. {
  1087. ByteCopy( &(psObject->dfMMin), pabyRec + nRecordSize, 8 );
  1088. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1089. nRecordSize += 8;
  1090. ByteCopy( &(psObject->dfMMax), pabyRec + nRecordSize, 8 );
  1091. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1092. nRecordSize += 8;
  1093. for( i = 0; i < psObject->nVertices; i++ )
  1094. {
  1095. ByteCopy( psObject->padfM + i, pabyRec + nRecordSize, 8 );
  1096. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1097. nRecordSize += 8;
  1098. }
  1099. }
  1100. }
  1101. /* -------------------------------------------------------------------- */
  1102. /* Extract vertices for a MultiPoint. */
  1103. /* -------------------------------------------------------------------- */
  1104. else if( psObject->nSHPType == SHPT_MULTIPOINT
  1105. || psObject->nSHPType == SHPT_MULTIPOINTZ
  1106. || psObject->nSHPType == SHPT_MULTIPOINTM )
  1107. {
  1108. int32 nPoints;
  1109. int i;
  1110. nPoints = psObject->nVertices;
  1111. _SHPSetBounds( pabyRec + 12, psObject );
  1112. if( bBigEndian ) SwapWord( 4, &nPoints );
  1113. ByteCopy( &nPoints, pabyRec + 44, 4 );
  1114. for( i = 0; i < psObject->nVertices; i++ )
  1115. {
  1116. ByteCopy( psObject->padfX + i, pabyRec + 48 + i*16, 8 );
  1117. ByteCopy( psObject->padfY + i, pabyRec + 48 + i*16 + 8, 8 );
  1118. if( bBigEndian ) SwapWord( 8, pabyRec + 48 + i*16 );
  1119. if( bBigEndian ) SwapWord( 8, pabyRec + 48 + i*16 + 8 );
  1120. }
  1121. nRecordSize = 48 + 16 * psObject->nVertices;
  1122. if( psObject->nSHPType == SHPT_MULTIPOINTZ )
  1123. {
  1124. ByteCopy( &(psObject->dfZMin), pabyRec + nRecordSize, 8 );
  1125. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1126. nRecordSize += 8;
  1127. ByteCopy( &(psObject->dfZMax), pabyRec + nRecordSize, 8 );
  1128. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1129. nRecordSize += 8;
  1130. for( i = 0; i < psObject->nVertices; i++ )
  1131. {
  1132. ByteCopy( psObject->padfZ + i, pabyRec + nRecordSize, 8 );
  1133. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1134. nRecordSize += 8;
  1135. }
  1136. }
  1137. if( psObject->bMeasureIsUsed
  1138. && (psObject->nSHPType == SHPT_MULTIPOINTZ
  1139. || psObject->nSHPType == SHPT_MULTIPOINTM) )
  1140. {
  1141. ByteCopy( &(psObject->dfMMin), pabyRec + nRecordSize, 8 );
  1142. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1143. nRecordSize += 8;
  1144. ByteCopy( &(psObject->dfMMax), pabyRec + nRecordSize, 8 );
  1145. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1146. nRecordSize += 8;
  1147. for( i = 0; i < psObject->nVertices; i++ )
  1148. {
  1149. ByteCopy( psObject->padfM + i, pabyRec + nRecordSize, 8 );
  1150. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1151. nRecordSize += 8;
  1152. }
  1153. }
  1154. }
  1155. /* -------------------------------------------------------------------- */
  1156. /* Write point. */
  1157. /* -------------------------------------------------------------------- */
  1158. else if( psObject->nSHPType == SHPT_POINT
  1159. || psObject->nSHPType == SHPT_POINTZ
  1160. || psObject->nSHPType == SHPT_POINTM )
  1161. {
  1162. ByteCopy( psObject->padfX, pabyRec + 12, 8 );
  1163. ByteCopy( psObject->padfY, pabyRec + 20, 8 );
  1164. if( bBigEndian ) SwapWord( 8, pabyRec + 12 );
  1165. if( bBigEndian ) SwapWord( 8, pabyRec + 20 );
  1166. nRecordSize = 28;
  1167. if( psObject->nSHPType == SHPT_POINTZ )
  1168. {
  1169. ByteCopy( psObject->padfZ, pabyRec + nRecordSize, 8 );
  1170. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1171. nRecordSize += 8;
  1172. }
  1173. if( psObject->bMeasureIsUsed
  1174. && (psObject->nSHPType == SHPT_POINTZ
  1175. || psObject->nSHPType == SHPT_POINTM) )
  1176. {
  1177. ByteCopy( psObject->padfM, pabyRec + nRecordSize, 8 );
  1178. if( bBigEndian ) SwapWord( 8, pabyRec + nRecordSize );
  1179. nRecordSize += 8;
  1180. }
  1181. }
  1182. /* -------------------------------------------------------------------- */
  1183. /* Not much to do for null geometries. */
  1184. /* -------------------------------------------------------------------- */
  1185. else if( psObject->nSHPType == SHPT_NULL )
  1186. {
  1187. nRecordSize = 12;
  1188. }
  1189. else
  1190. {
  1191. /* unknown type */
  1192. assert( FALSE );
  1193. }
  1194. /* -------------------------------------------------------------------- */
  1195. /* Establish where we are going to put this record. If we are */
  1196. /* rewriting and existing record, and it will fit, then put it */
  1197. /* back where the original came from. Otherwise write at the end. */
  1198. /* -------------------------------------------------------------------- */
  1199. if( nShapeId == -1 || psSHP->panRecSize[nShapeId] < nRecordSize-8 )
  1200. {
  1201. if( nShapeId == -1 )
  1202. nShapeId = psSHP->nRecords++;
  1203. psSHP->panRecOffset[nShapeId] = nRecordOffset = psSHP->nFileSize;
  1204. psSHP->panRecSize[nShapeId] = nRecordSize-8;
  1205. psSHP->nFileSize += nRecordSize;
  1206. }
  1207. else
  1208. {
  1209. nRecordOffset = psSHP->panRecOffset[nShapeId];
  1210. psSHP->panRecSize[nShapeId] = nRecordSize-8;
  1211. }
  1212. /* -------------------------------------------------------------------- */
  1213. /* Set the shape type, record number, and record size. */
  1214. /* -------------------------------------------------------------------- */
  1215. i32 = nShapeId+1; /* record # */
  1216. if( !bBigEndian ) SwapWord( 4, &i32 );
  1217. ByteCopy( &i32, pabyRec, 4 );
  1218. i32 = (nRecordSize-8)/2; /* record size */
  1219. if( !bBigEndian ) SwapWord( 4, &i32 );
  1220. ByteCopy( &i32, pabyRec + 4, 4 );
  1221. i32 = psObject->nSHPType; /* shape type */
  1222. if( bBigEndian ) SwapWord( 4, &i32 );
  1223. ByteCopy( &i32, pabyRec + 8, 4 );
  1224. /* -------------------------------------------------------------------- */
  1225. /* Write out record. */
  1226. /* -------------------------------------------------------------------- */
  1227. if( psSHP->sHooks.FSeek( psSHP->fpSHP, nRecordOffset, 0 ) != 0
  1228. || psSHP->sHooks.FWrite( pabyRec, nRecordSize, 1, psSHP->fpSHP ) < 1 )
  1229. {
  1230. psSHP->sHooks.Error( "Error in psSHP->sHooks.FSeek() or fwrite() writing object to .shp file." );
  1231. free( pabyRec );
  1232. return -1;
  1233. }
  1234. free( pabyRec );
  1235. /* -------------------------------------------------------------------- */
  1236. /* Expand file wide bounds based on this shape. */
  1237. /* -------------------------------------------------------------------- */
  1238. if( psSHP->adBoundsMin[0] == 0.0
  1239. && psSHP->adBoundsMax[0] == 0.0
  1240. && psSHP->adBoundsMin[1] == 0.0
  1241. && psSHP->adBoundsMax[1] == 0.0 )
  1242. {
  1243. if( psObject->nSHPType == SHPT_NULL || psObject->nVertices == 0 )
  1244. {
  1245. psSHP->adBoundsMin[0] = psSHP->adBoundsMax[0] = 0.0;
  1246. psSHP->adBoundsMin[1] = psSHP->adBoundsMax[1] = 0.0;
  1247. psSHP->adBoundsMin[2] = psSHP->adBoundsMax[2] = 0.0;
  1248. psSHP->adBoundsMin[3] = psSHP->adBoundsMax[3] = 0.0;
  1249. }
  1250. else
  1251. {
  1252. psSHP->adBoundsMin[0] = psSHP->adBoundsMax[0] = psObject->padfX[0];
  1253. psSHP->adBoundsMin[1] = psSHP->adBoundsMax[1] = psObject->padfY[0];
  1254. psSHP->adBoundsMin[2] = psSHP->adBoundsMax[2] = psObject->padfZ[0];
  1255. psSHP->adBoundsMin[3] = psSHP->adBoundsMax[3] = psObject->padfM[0];
  1256. }
  1257. }
  1258. for( i = 0; i < psObject->nVertices; i++ )
  1259. {
  1260. psSHP->adBoundsMin[0] = MIN(psSHP->adBoundsMin[0],psObject->padfX[i]);
  1261. psSHP->adBoundsMin[1] = MIN(psSHP->adBoundsMin[1],psObject->padfY[i]);
  1262. psSHP->adBoundsMin[2] = MIN(psSHP->adBoundsMin[2],psObject->padfZ[i]);
  1263. psSHP->adBoundsMin[3] = MIN(psSHP->adBoundsMin[3],psObject->padfM[i]);
  1264. psSHP->adBoundsMax[0] = MAX(psSHP->adBoundsMax[0],psObject->padfX[i]);
  1265. psSHP->adBoundsMax[1] = MAX(psSHP->adBoundsMax[1],psObject->padfY[i]);
  1266. psSHP->adBoundsMax[2] = MAX(psSHP->adBoundsMax[2],psObject->padfZ[i]);
  1267. psSHP->adBoundsMax[3] = MAX(psSHP->adBoundsMax[3],psObject->padfM[i]);
  1268. }
  1269. return( nShapeId );
  1270. }
  1271. /************************************************************************/
  1272. /* SHPReadObject() */
  1273. /* */
  1274. /* Read the vertices, parts, and other non-attribute information */
  1275. /* for one shape. */
  1276. /************************************************************************/
  1277. SHPObject SHPAPI_CALL1(*)
  1278. SHPReadObject( SHPHandle psSHP, int hEntity )
  1279. {
  1280. int nEntitySize, nRequiredSize;
  1281. SHPObject *psShape;
  1282. char pszErrorMsg[128];
  1283. /* -------------------------------------------------------------------- */
  1284. /* Validate the record/entity number. */
  1285. /* -------------------------------------------------------------------- */
  1286. if( hEntity < 0 || hEntity >= psSHP->nRecords )
  1287. return( NULL );
  1288. /* -------------------------------------------------------------------- */
  1289. /* Ensure our record buffer is large enough. */
  1290. /* -------------------------------------------------------------------- */
  1291. nEntitySize = psSHP->panRecSize[hEntity]+8;
  1292. if( nEntitySize > psSHP->nBufSize )
  1293. {
  1294. psSHP->pabyRec = (uchar *) SfRealloc(psSHP->pabyRec,nEntitySize);
  1295. if (psSHP->pabyRec == NULL)
  1296. {
  1297. char szError[200];
  1298. /* Reallocate previous successfull size for following features */
  1299. psSHP->pabyRec = malloc(psSHP->nBufSize);
  1300. sprintf( szError,
  1301. "Not enough memory to allocate requested memory (nBufSize=%d). "
  1302. "Probably broken SHP file", psSHP->nBufSize );
  1303. psSHP->sHooks.Error( szError );
  1304. return NULL;
  1305. }
  1306. /* Only set new buffer size after successfull alloc */
  1307. psSHP->nBufSize = nEntitySize;
  1308. }
  1309. /* In case we were not able to reallocate the buffer on a previous step */
  1310. if (psSHP->pabyRec == NULL)
  1311. {
  1312. return NULL;
  1313. }
  1314. /* -------------------------------------------------------------------- */
  1315. /* Read the record. */
  1316. /* -------------------------------------------------------------------- */
  1317. if( psSHP->sHooks.FSeek( psSHP->fpSHP, psSHP->panRecOffset[hEntity], 0 ) != 0
  1318. || psSHP->sHooks.FRead( psSHP->pabyRec, nEntitySize, 1,
  1319. psSHP->fpSHP ) != 1 )
  1320. {
  1321. /*
  1322. * TODO - mloskot: Consider detailed diagnostics of shape file,
  1323. * for example to detect if file is truncated.
  1324. */
  1325. psSHP->sHooks.Error( "Error in fseek() or fread() reading object from .shp file." );
  1326. return NULL;
  1327. }
  1328. /* -------------------------------------------------------------------- */
  1329. /* Allocate and minimally initialize the object. */
  1330. /* -------------------------------------------------------------------- */
  1331. psShape = (SHPObject *) calloc(1,sizeof(SHPObject));
  1332. psShape->nShapeId = hEntity;
  1333. psShape->bMeasureIsUsed = FALSE;
  1334. if ( 8 + 4 > nEntitySize )
  1335. {
  1336. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nEntitySize = %d",
  1337. hEntity, nEntitySize);
  1338. psSHP->sHooks.Error( pszErrorMsg );
  1339. SHPDestroyObject(psShape);
  1340. return NULL;
  1341. }
  1342. memcpy( &psShape->nSHPType, psSHP->pabyRec + 8, 4 );
  1343. if( bBigEndian ) SwapWord( 4, &(psShape->nSHPType) );
  1344. /* ==================================================================== */
  1345. /* Extract vertices for a Polygon or Arc. */
  1346. /* ==================================================================== */
  1347. if( psShape->nSHPType == SHPT_POLYGON || psShape->nSHPType == SHPT_ARC
  1348. || psShape->nSHPType == SHPT_POLYGONZ
  1349. || psShape->nSHPType == SHPT_POLYGONM
  1350. || psShape->nSHPType == SHPT_ARCZ
  1351. || psShape->nSHPType == SHPT_ARCM
  1352. || psShape->nSHPType == SHPT_MULTIPATCH )
  1353. {
  1354. int32 nPoints, nParts;
  1355. int i, nOffset;
  1356. if ( 40 + 8 + 4 > nEntitySize )
  1357. {
  1358. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nEntitySize = %d",
  1359. hEntity, nEntitySize);
  1360. psSHP->sHooks.Error( pszErrorMsg );
  1361. SHPDestroyObject(psShape);
  1362. return NULL;
  1363. }
  1364. /* -------------------------------------------------------------------- */
  1365. /* Get the X/Y bounds. */
  1366. /* -------------------------------------------------------------------- */
  1367. memcpy( &(psShape->dfXMin), psSHP->pabyRec + 8 + 4, 8 );
  1368. memcpy( &(psShape->dfYMin), psSHP->pabyRec + 8 + 12, 8 );
  1369. memcpy( &(psShape->dfXMax), psSHP->pabyRec + 8 + 20, 8 );
  1370. memcpy( &(psShape->dfYMax), psSHP->pabyRec + 8 + 28, 8 );
  1371. if( bBigEndian ) SwapWord( 8, &(psShape->dfXMin) );
  1372. if( bBigEndian ) SwapWord( 8, &(psShape->dfYMin) );
  1373. if( bBigEndian ) SwapWord( 8, &(psShape->dfXMax) );
  1374. if( bBigEndian ) SwapWord( 8, &(psShape->dfYMax) );
  1375. /* -------------------------------------------------------------------- */
  1376. /* Extract part/point count, and build vertex and part arrays */
  1377. /* to proper size. */
  1378. /* -------------------------------------------------------------------- */
  1379. memcpy( &nPoints, psSHP->pabyRec + 40 + 8, 4 );
  1380. memcpy( &nParts, psSHP->pabyRec + 36 + 8, 4 );
  1381. if( bBigEndian ) SwapWord( 4, &nPoints );
  1382. if( bBigEndian ) SwapWord( 4, &nParts );
  1383. if (nPoints < 0 || nParts < 0 ||
  1384. nPoints > 50 * 1000 * 1000 || nParts > 10 * 1000 * 1000)
  1385. {
  1386. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d, nPoints=%d, nParts=%d.",
  1387. hEntity, nPoints, nParts);
  1388. psSHP->sHooks.Error( pszErrorMsg );
  1389. SHPDestroyObject(psShape);
  1390. return NULL;
  1391. }
  1392. /* With the previous checks on nPoints and nParts, */
  1393. /* we should not overflow here and after */
  1394. /* since 50 M * (16 + 8 + 8) = 1 600 MB */
  1395. nRequiredSize = 44 + 8 + 4 * nParts + 16 * nPoints;
  1396. if ( psShape->nSHPType == SHPT_POLYGONZ
  1397. || psShape->nSHPType == SHPT_ARCZ
  1398. || psShape->nSHPType == SHPT_MULTIPATCH )
  1399. {
  1400. nRequiredSize += 16 + 8 * nPoints;
  1401. }
  1402. if( psShape->nSHPType == SHPT_MULTIPATCH )
  1403. {
  1404. nRequiredSize += 4 * nParts;
  1405. }
  1406. if (nRequiredSize > nEntitySize)
  1407. {
  1408. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d, nPoints=%d, nParts=%d, nEntitySize=%d.",
  1409. hEntity, nPoints, nParts, nEntitySize);
  1410. psSHP->sHooks.Error( pszErrorMsg );
  1411. SHPDestroyObject(psShape);
  1412. return NULL;
  1413. }
  1414. psShape->nVertices = nPoints;
  1415. psShape->padfX = (double *) calloc(nPoints,sizeof(double));
  1416. psShape->padfY = (double *) calloc(nPoints,sizeof(double));
  1417. psShape->padfZ = (double *) calloc(nPoints,sizeof(double));
  1418. psShape->padfM = (double *) calloc(nPoints,sizeof(double));
  1419. psShape->nParts = nParts;
  1420. psShape->panPartStart = (int *) calloc(nParts,sizeof(int));
  1421. psShape->panPartType = (int *) calloc(nParts,sizeof(int));
  1422. if (psShape->padfX == NULL ||
  1423. psShape->padfY == NULL ||
  1424. psShape->padfZ == NULL ||
  1425. psShape->padfM == NULL ||
  1426. psShape->panPartStart == NULL ||
  1427. psShape->panPartType == NULL)
  1428. {
  1429. snprintf(pszErrorMsg, 128,
  1430. "Not enough memory to allocate requested memory (nPoints=%d, nParts=%d) for shape %d. "
  1431. "Probably broken SHP file", hEntity, nPoints, nParts );
  1432. psSHP->sHooks.Error( pszErrorMsg );
  1433. SHPDestroyObject(psShape);
  1434. return NULL;
  1435. }
  1436. for( i = 0; i < nParts; i++ )
  1437. psShape->panPartType[i] = SHPP_RING;
  1438. /* -------------------------------------------------------------------- */
  1439. /* Copy out the part array from the record. */
  1440. /* -------------------------------------------------------------------- */
  1441. memcpy( psShape->panPartStart, psSHP->pabyRec + 44 + 8, 4 * nParts );
  1442. for( i = 0; i < nParts; i++ )
  1443. {
  1444. if( bBigEndian ) SwapWord( 4, psShape->panPartStart+i );
  1445. /* We check that the offset is inside the vertex array */
  1446. if (psShape->panPartStart[i] < 0 ||
  1447. psShape->panPartStart[i] >= psShape->nVertices)
  1448. {
  1449. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : panPartStart[%d] = %d, nVertices = %d",
  1450. hEntity, i, psShape->panPartStart[i], psShape->nVertices);
  1451. psSHP->sHooks.Error( pszErrorMsg );
  1452. SHPDestroyObject(psShape);
  1453. return NULL;
  1454. }
  1455. if (i > 0 && psShape->panPartStart[i] <= psShape->panPartStart[i-1])
  1456. {
  1457. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : panPartStart[%d] = %d, panPartStart[%d] = %d",
  1458. hEntity, i, psShape->panPartStart[i], i - 1, psShape->panPartStart[i - 1]);
  1459. psSHP->sHooks.Error( pszErrorMsg );
  1460. SHPDestroyObject(psShape);
  1461. return NULL;
  1462. }
  1463. }
  1464. nOffset = 44 + 8 + 4*nParts;
  1465. /* -------------------------------------------------------------------- */
  1466. /* If this is a multipatch, we will also have parts types. */
  1467. /* -------------------------------------------------------------------- */
  1468. if( psShape->nSHPType == SHPT_MULTIPATCH )
  1469. {
  1470. memcpy( psShape->panPartType, psSHP->pabyRec + nOffset, 4*nParts );
  1471. for( i = 0; i < nParts; i++ )
  1472. {
  1473. if( bBigEndian ) SwapWord( 4, psShape->panPartType+i );
  1474. }
  1475. nOffset += 4*nParts;
  1476. }
  1477. /* -------------------------------------------------------------------- */
  1478. /* Copy out the vertices from the record. */
  1479. /* -------------------------------------------------------------------- */
  1480. for( i = 0; i < nPoints; i++ )
  1481. {
  1482. memcpy(psShape->padfX + i,
  1483. psSHP->pabyRec + nOffset + i * 16,
  1484. 8 );
  1485. memcpy(psShape->padfY + i,
  1486. psSHP->pabyRec + nOffset + i * 16 + 8,
  1487. 8 );
  1488. if( bBigEndian ) SwapWord( 8, psShape->padfX + i );
  1489. if( bBigEndian ) SwapWord( 8, psShape->padfY + i );
  1490. }
  1491. nOffset += 16*nPoints;
  1492. /* -------------------------------------------------------------------- */
  1493. /* If we have a Z coordinate, collect that now. */
  1494. /* -------------------------------------------------------------------- */
  1495. if( psShape->nSHPType == SHPT_POLYGONZ
  1496. || psShape->nSHPType == SHPT_ARCZ
  1497. || psShape->nSHPType == SHPT_MULTIPATCH )
  1498. {
  1499. memcpy( &(psShape->dfZMin), psSHP->pabyRec + nOffset, 8 );
  1500. memcpy( &(psShape->dfZMax), psSHP->pabyRec + nOffset + 8, 8 );
  1501. if( bBigEndian ) SwapWord( 8, &(psShape->dfZMin) );
  1502. if( bBigEndian ) SwapWord( 8, &(psShape->dfZMax) );
  1503. for( i = 0; i < nPoints; i++ )
  1504. {
  1505. memcpy( psShape->padfZ + i,
  1506. psSHP->pabyRec + nOffset + 16 + i*8, 8 );
  1507. if( bBigEndian ) SwapWord( 8, psShape->padfZ + i );
  1508. }
  1509. nOffset += 16 + 8*nPoints;
  1510. }
  1511. /* -------------------------------------------------------------------- */
  1512. /* If we have a M measure value, then read it now. We assume */
  1513. /* that the measure can be present for any shape if the size is */
  1514. /* big enough, but really it will only occur for the Z shapes */
  1515. /* (options), and the M shapes. */
  1516. /* -------------------------------------------------------------------- */
  1517. if( nEntitySize >= nOffset + 16 + 8*nPoints )
  1518. {
  1519. memcpy( &(psShape->dfMMin), psSHP->pabyRec + nOffset, 8 );
  1520. memcpy( &(psShape->dfMMax), psSHP->pabyRec + nOffset + 8, 8 );
  1521. if( bBigEndian ) SwapWord( 8, &(psShape->dfMMin) );
  1522. if( bBigEndian ) SwapWord( 8, &(psShape->dfMMax) );
  1523. for( i = 0; i < nPoints; i++ )
  1524. {
  1525. memcpy( psShape->padfM + i,
  1526. psSHP->pabyRec + nOffset + 16 + i*8, 8 );
  1527. if( bBigEndian ) SwapWord( 8, psShape->padfM + i );
  1528. }
  1529. psShape->bMeasureIsUsed = TRUE;
  1530. }
  1531. }
  1532. /* ==================================================================== */
  1533. /* Extract vertices for a MultiPoint. */
  1534. /* ==================================================================== */
  1535. else if( psShape->nSHPType == SHPT_MULTIPOINT
  1536. || psShape->nSHPType == SHPT_MULTIPOINTM
  1537. || psShape->nSHPType == SHPT_MULTIPOINTZ )
  1538. {
  1539. int32 nPoints;
  1540. int i, nOffset;
  1541. if ( 44 + 4 > nEntitySize )
  1542. {
  1543. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nEntitySize = %d",
  1544. hEntity, nEntitySize);
  1545. psSHP->sHooks.Error( pszErrorMsg );
  1546. SHPDestroyObject(psShape);
  1547. return NULL;
  1548. }
  1549. memcpy( &nPoints, psSHP->pabyRec + 44, 4 );
  1550. if( bBigEndian ) SwapWord( 4, &nPoints );
  1551. if (nPoints < 0 || nPoints > 50 * 1000 * 1000)
  1552. {
  1553. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nPoints = %d",
  1554. hEntity, nPoints);
  1555. psSHP->sHooks.Error( pszErrorMsg );
  1556. SHPDestroyObject(psShape);
  1557. return NULL;
  1558. }
  1559. nRequiredSize = 48 + nPoints * 16;
  1560. if( psShape->nSHPType == SHPT_MULTIPOINTZ )
  1561. {
  1562. nRequiredSize += 16 + nPoints * 8;
  1563. }
  1564. if (nRequiredSize > nEntitySize)
  1565. {
  1566. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nPoints = %d, nEntitySize = %d",
  1567. hEntity, nPoints, nEntitySize);
  1568. psSHP->sHooks.Error( pszErrorMsg );
  1569. SHPDestroyObject(psShape);
  1570. return NULL;
  1571. }
  1572. psShape->nVertices = nPoints;
  1573. psShape->padfX = (double *) calloc(nPoints,sizeof(double));
  1574. psShape->padfY = (double *) calloc(nPoints,sizeof(double));
  1575. psShape->padfZ = (double *) calloc(nPoints,sizeof(double));
  1576. psShape->padfM = (double *) calloc(nPoints,sizeof(double));
  1577. if (psShape->padfX == NULL ||
  1578. psShape->padfY == NULL ||
  1579. psShape->padfZ == NULL ||
  1580. psShape->padfM == NULL)
  1581. {
  1582. snprintf(pszErrorMsg, 128,
  1583. "Not enough memory to allocate requested memory (nPoints=%d) for shape %d. "
  1584. "Probably broken SHP file", hEntity, nPoints );
  1585. psSHP->sHooks.Error( pszErrorMsg );
  1586. SHPDestroyObject(psShape);
  1587. return NULL;
  1588. }
  1589. for( i = 0; i < nPoints; i++ )
  1590. {
  1591. memcpy(psShape->padfX+i, psSHP->pabyRec + 48 + 16 * i, 8 );
  1592. memcpy(psShape->padfY+i, psSHP->pabyRec + 48 + 16 * i + 8, 8 );
  1593. if( bBigEndian ) SwapWord( 8, psShape->padfX + i );
  1594. if( bBigEndian ) SwapWord( 8, psShape->padfY + i );
  1595. }
  1596. nOffset = 48 + 16*nPoints;
  1597. /* -------------------------------------------------------------------- */
  1598. /* Get the X/Y bounds. */
  1599. /* -------------------------------------------------------------------- */
  1600. memcpy( &(psShape->dfXMin), psSHP->pabyRec + 8 + 4, 8 );
  1601. memcpy( &(psShape->dfYMin), psSHP->pabyRec + 8 + 12, 8 );
  1602. memcpy( &(psShape->dfXMax), psSHP->pabyRec + 8 + 20, 8 );
  1603. memcpy( &(psShape->dfYMax), psSHP->pabyRec + 8 + 28, 8 );
  1604. if( bBigEndian ) SwapWord( 8, &(psShape->dfXMin) );
  1605. if( bBigEndian ) SwapWord( 8, &(psShape->dfYMin) );
  1606. if( bBigEndian ) SwapWord( 8, &(psShape->dfXMax) );
  1607. if( bBigEndian ) SwapWord( 8, &(psShape->dfYMax) );
  1608. /* -------------------------------------------------------------------- */
  1609. /* If we have a Z coordinate, collect that now. */
  1610. /* -------------------------------------------------------------------- */
  1611. if( psShape->nSHPType == SHPT_MULTIPOINTZ )
  1612. {
  1613. memcpy( &(psShape->dfZMin), psSHP->pabyRec + nOffset, 8 );
  1614. memcpy( &(psShape->dfZMax), psSHP->pabyRec + nOffset + 8, 8 );
  1615. if( bBigEndian ) SwapWord( 8, &(psShape->dfZMin) );
  1616. if( bBigEndian ) SwapWord( 8, &(psShape->dfZMax) );
  1617. for( i = 0; i < nPoints; i++ )
  1618. {
  1619. memcpy( psShape->padfZ + i,
  1620. psSHP->pabyRec + nOffset + 16 + i*8, 8 );
  1621. if( bBigEndian ) SwapWord( 8, psShape->padfZ + i );
  1622. }
  1623. nOffset += 16 + 8*nPoints;
  1624. }
  1625. /* -------------------------------------------------------------------- */
  1626. /* If we have a M measure value, then read it now. We assume */
  1627. /* that the measure can be present for any shape if the size is */
  1628. /* big enough, but really it will only occur for the Z shapes */
  1629. /* (options), and the M shapes. */
  1630. /* -------------------------------------------------------------------- */
  1631. if( nEntitySize >= nOffset + 16 + 8*nPoints )
  1632. {
  1633. memcpy( &(psShape->dfMMin), psSHP->pabyRec + nOffset, 8 );
  1634. memcpy( &(psShape->dfMMax), psSHP->pabyRec + nOffset + 8, 8 );
  1635. if( bBigEndian ) SwapWord( 8, &(psShape->dfMMin) );
  1636. if( bBigEndian ) SwapWord( 8, &(psShape->dfMMax) );
  1637. for( i = 0; i < nPoints; i++ )
  1638. {
  1639. memcpy( psShape->padfM + i,
  1640. psSHP->pabyRec + nOffset + 16 + i*8, 8 );
  1641. if( bBigEndian ) SwapWord( 8, psShape->padfM + i );
  1642. }
  1643. psShape->bMeasureIsUsed = TRUE;
  1644. }
  1645. }
  1646. /* ==================================================================== */
  1647. /* Extract vertices for a point. */
  1648. /* ==================================================================== */
  1649. else if( psShape->nSHPType == SHPT_POINT
  1650. || psShape->nSHPType == SHPT_POINTM
  1651. || psShape->nSHPType == SHPT_POINTZ )
  1652. {
  1653. int nOffset;
  1654. psShape->nVertices = 1;
  1655. psShape->padfX = (double *) calloc(1,sizeof(double));
  1656. psShape->padfY = (double *) calloc(1,sizeof(double));
  1657. psShape->padfZ = (double *) calloc(1,sizeof(double));
  1658. psShape->padfM = (double *) calloc(1,sizeof(double));
  1659. if (20 + 8 + (( psShape->nSHPType == SHPT_POINTZ ) ? 8 : 0)> nEntitySize)
  1660. {
  1661. snprintf(pszErrorMsg, 128, "Corrupted .shp file : shape %d : nEntitySize = %d",
  1662. hEntity, nEntitySize);
  1663. psSHP->sHooks.Error( pszErrorMsg );
  1664. SHPDestroyObject(psShape);
  1665. return NULL;
  1666. }
  1667. memcpy( psShape->padfX, psSHP->pabyRec + 12, 8 );
  1668. memcpy( psShape->padfY, psSHP->pabyRec + 20, 8 );
  1669. if( bBigEndian ) SwapWord( 8, psShape->padfX );
  1670. if( bBigEndian ) SwapWord( 8, psShape->padfY );
  1671. nOffset = 20 + 8;
  1672. /* -------------------------------------------------------------------- */
  1673. /* If we have a Z coordinate, collect that now. */
  1674. /* -------------------------------------------------------------------- */
  1675. if( psShape->nSHPType == SHPT_POINTZ )
  1676. {
  1677. memcpy( psShape->padfZ, psSHP->pabyRec + nOffset, 8 );
  1678. if( bBigEndian ) SwapWord( 8, psShape->padfZ );
  1679. nOffset += 8;
  1680. }
  1681. /* -------------------------------------------------------------------- */
  1682. /* If we have a M measure value, then read it now. We assume */
  1683. /* that the measure can be present for any shape if the size is */
  1684. /* big enough, but really it will only occur for the Z shapes */
  1685. /* (options), and the M shapes. */
  1686. /* -------------------------------------------------------------------- */
  1687. if( nEntitySize >= nOffset + 8 )
  1688. {
  1689. memcpy( psShape->padfM, psSHP->pabyRec + nOffset, 8 );
  1690. if( bBigEndian ) SwapWord( 8, psShape->padfM );
  1691. psShape->bMeasureIsUsed = TRUE;
  1692. }
  1693. /* -------------------------------------------------------------------- */
  1694. /* Since no extents are supplied in the record, we will apply */
  1695. /* them from the single vertex. */
  1696. /* -------------------------------------------------------------------- */
  1697. psShape->dfXMin = psShape->dfXMax = psShape->padfX[0];
  1698. psShape->dfYMin = psShape->dfYMax = psShape->padfY[0];
  1699. psShape->dfZMin = psShape->dfZMax = psShape->padfZ[0];
  1700. psShape->dfMMin = psShape->dfMMax = psShape->padfM[0];
  1701. }
  1702. return( psShape );
  1703. }
  1704. /************************************************************************/
  1705. /* SHPTypeName() */
  1706. /************************************************************************/
  1707. const char SHPAPI_CALL1(*)
  1708. SHPTypeName( int nSHPType )
  1709. {
  1710. switch( nSHPType )
  1711. {
  1712. case SHPT_NULL:
  1713. return "NullShape";
  1714. case SHPT_POINT:
  1715. return "Point";
  1716. case SHPT_ARC:
  1717. return "Arc";
  1718. case SHPT_POLYGON:
  1719. return "Polygon";
  1720. case SHPT_MULTIPOINT:
  1721. return "MultiPoint";
  1722. case SHPT_POINTZ:
  1723. return "PointZ";
  1724. case SHPT_ARCZ:
  1725. return "ArcZ";
  1726. case SHPT_POLYGONZ:
  1727. return "PolygonZ";
  1728. case SHPT_MULTIPOINTZ:
  1729. return "MultiPointZ";
  1730. case SHPT_POINTM:
  1731. return "PointM";
  1732. case SHPT_ARCM:
  1733. return "ArcM";
  1734. case SHPT_POLYGONM:
  1735. return "PolygonM";
  1736. case SHPT_MULTIPOINTM:
  1737. return "MultiPointM";
  1738. case SHPT_MULTIPATCH:
  1739. return "MultiPatch";
  1740. default:
  1741. return "UnknownShapeType";
  1742. }
  1743. }
  1744. /************************************************************************/
  1745. /* SHPPartTypeName() */
  1746. /************************************************************************/
  1747. const char SHPAPI_CALL1(*)
  1748. SHPPartTypeName( int nPartType )
  1749. {
  1750. switch( nPartType )
  1751. {
  1752. case SHPP_TRISTRIP:
  1753. return "TriangleStrip";
  1754. case SHPP_TRIFAN:
  1755. return "TriangleFan";
  1756. case SHPP_OUTERRING:
  1757. return "OuterRing";
  1758. case SHPP_INNERRING:
  1759. return "InnerRing";
  1760. case SHPP_FIRSTRING:
  1761. return "FirstRing";
  1762. case SHPP_RING:
  1763. return "Ring";
  1764. default:
  1765. return "UnknownPartType";
  1766. }
  1767. }
  1768. /************************************************************************/
  1769. /* SHPDestroyObject() */
  1770. /************************************************************************/
  1771. void SHPAPI_CALL
  1772. SHPDestroyObject( SHPObject * psShape )
  1773. {
  1774. if( psShape == NULL )
  1775. return;
  1776. if( psShape->padfX != NULL )
  1777. free( psShape->padfX );
  1778. if( psShape->padfY != NULL )
  1779. free( psShape->padfY );
  1780. if( psShape->padfZ != NULL )
  1781. free( psShape->padfZ );
  1782. if( psShape->padfM != NULL )
  1783. free( psShape->padfM );
  1784. if( psShape->panPartStart != NULL )
  1785. free( psShape->panPartStart );
  1786. if( psShape->panPartType != NULL )
  1787. free( psShape->panPartType );
  1788. free( psShape );
  1789. }
  1790. /************************************************************************/
  1791. /* SHPRewindObject() */
  1792. /* */
  1793. /* Reset the winding of polygon objects to adhere to the */
  1794. /* specification. */
  1795. /************************************************************************/
  1796. int SHPAPI_CALL
  1797. SHPRewindObject( SHPHandle hSHP, SHPObject * psObject )
  1798. {
  1799. int iOpRing, bAltered = 0;
  1800. /* -------------------------------------------------------------------- */
  1801. /* Do nothing if this is not a polygon object. */
  1802. /* -------------------------------------------------------------------- */
  1803. if( psObject->nSHPType != SHPT_POLYGON
  1804. && psObject->nSHPType != SHPT_POLYGONZ
  1805. && psObject->nSHPType != SHPT_POLYGONM )
  1806. return 0;
  1807. if( psObject->nVertices == 0 || psObject->nParts == 0 )
  1808. return 0;
  1809. /* -------------------------------------------------------------------- */
  1810. /* Process each of the rings. */
  1811. /* -------------------------------------------------------------------- */
  1812. for( iOpRing = 0; iOpRing < psObject->nParts; iOpRing++ )
  1813. {
  1814. int bInner, iVert, nVertCount, nVertStart, iCheckRing;
  1815. double dfSum, dfTestX, dfTestY;
  1816. /* -------------------------------------------------------------------- */
  1817. /* Determine if this ring is an inner ring or an outer ring */
  1818. /* relative to all the other rings. For now we assume the */
  1819. /* first ring is outer and all others are inner, but eventually */
  1820. /* we need to fix this to handle multiple island polygons and */
  1821. /* unordered sets of rings. */
  1822. /* */
  1823. /* -------------------------------------------------------------------- */
  1824. /* Use point in the middle of segment to avoid testing
  1825. * common points of rings.
  1826. */
  1827. dfTestX = ( psObject->padfX[psObject->panPartStart[iOpRing]]
  1828. + psObject->padfX[psObject->panPartStart[iOpRing] + 1] ) / 2;
  1829. dfTestY = ( psObject->padfY[psObject->panPartStart[iOpRing]]
  1830. + psObject->padfY[psObject->panPartStart[iOpRing] + 1] ) / 2;
  1831. bInner = FALSE;
  1832. for( iCheckRing = 0; iCheckRing < psObject->nParts; iCheckRing++ )
  1833. {
  1834. int iEdge;
  1835. if( iCheckRing == iOpRing )
  1836. continue;
  1837. nVertStart = psObject->panPartStart[iCheckRing];
  1838. if( iCheckRing == psObject->nParts-1 )
  1839. nVertCount = psObject->nVertices
  1840. - psObject->panPartStart[iCheckRing];
  1841. else
  1842. nVertCount = psObject->panPartStart[iCheckRing+1]
  1843. - psObject->panPartStart[iCheckRing];
  1844. for( iEdge = 0; iEdge < nVertCount; iEdge++ )
  1845. {
  1846. int iNext;
  1847. if( iEdge < nVertCount-1 )
  1848. iNext = iEdge+1;
  1849. else
  1850. iNext = 0;
  1851. /* Rule #1:
  1852. * Test whether the edge 'straddles' the horizontal ray from the test point (dfTestY,dfTestY)
  1853. * The rule #1 also excludes edges collinear with the ray.
  1854. */
  1855. if ( ( psObject->padfY[iEdge+nVertStart] < dfTestY
  1856. && dfTestY <= psObject->padfY[iNext+nVertStart] )
  1857. || ( psObject->padfY[iNext+nVertStart] < dfTestY
  1858. && dfTestY <= psObject->padfY[iEdge+nVertStart] ) )
  1859. {
  1860. /* Rule #2:
  1861. * Test if edge-ray intersection is on the right from the test point (dfTestY,dfTestY)
  1862. */
  1863. double const intersect =
  1864. ( psObject->padfX[iEdge+nVertStart]
  1865. + ( dfTestY - psObject->padfY[iEdge+nVertStart] )
  1866. / ( psObject->padfY[iNext+nVertStart] - psObject->padfY[iEdge+nVertStart] )
  1867. * ( psObject->padfX[iNext+nVertStart] - psObject->padfX[iEdge+nVertStart] ) );
  1868. if (intersect < dfTestX)
  1869. {
  1870. bInner = !bInner;
  1871. }
  1872. }
  1873. }
  1874. } /* for iCheckRing */
  1875. /* -------------------------------------------------------------------- */
  1876. /* Determine the current order of this ring so we will know if */
  1877. /* it has to be reversed. */
  1878. /* -------------------------------------------------------------------- */
  1879. nVertStart = psObject->panPartStart[iOpRing];
  1880. if( iOpRing == psObject->nParts-1 )
  1881. nVertCount = psObject->nVertices - psObject->panPartStart[iOpRing];
  1882. else
  1883. nVertCount = psObject->panPartStart[iOpRing+1]
  1884. - psObject->panPartStart[iOpRing];
  1885. dfSum = 0.0;
  1886. for( iVert = nVertStart; iVert < nVertStart+nVertCount-1; iVert++ )
  1887. {
  1888. dfSum += psObject->padfX[iVert] * psObject->padfY[iVert+1]
  1889. - psObject->padfY[iVert] * psObject->padfX[iVert+1];
  1890. }
  1891. dfSum += psObject->padfX[iVert] * psObject->padfY[nVertStart]
  1892. - psObject->padfY[iVert] * psObject->padfX[nVertStart];
  1893. /* -------------------------------------------------------------------- */
  1894. /* Reverse if necessary. */
  1895. /* -------------------------------------------------------------------- */
  1896. if( (dfSum < 0.0 && bInner) || (dfSum > 0.0 && !bInner) )
  1897. {
  1898. int i;
  1899. bAltered++;
  1900. for( i = 0; i < nVertCount/2; i++ )
  1901. {
  1902. double dfSaved;
  1903. /* Swap X */
  1904. dfSaved = psObject->padfX[nVertStart+i];
  1905. psObject->padfX[nVertStart+i] =
  1906. psObject->padfX[nVertStart+nVertCount-i-1];
  1907. psObject->padfX[nVertStart+nVertCount-i-1] = dfSaved;
  1908. /* Swap Y */
  1909. dfSaved = psObject->padfY[nVertStart+i];
  1910. psObject->padfY[nVertStart+i] =
  1911. psObject->padfY[nVertStart+nVertCount-i-1];
  1912. psObject->padfY[nVertStart+nVertCount-i-1] = dfSaved;
  1913. /* Swap Z */
  1914. if( psObject->padfZ )
  1915. {
  1916. dfSaved = psObject->padfZ[nVertStart+i];
  1917. psObject->padfZ[nVertStart+i] =
  1918. psObject->padfZ[nVertStart+nVertCount-i-1];
  1919. psObject->padfZ[nVertStart+nVertCount-i-1] = dfSaved;
  1920. }
  1921. /* Swap M */
  1922. if( psObject->padfM )
  1923. {
  1924. dfSaved = psObject->padfM[nVertStart+i];
  1925. psObject->padfM[nVertStart+i] =
  1926. psObject->padfM[nVertStart+nVertCount-i-1];
  1927. psObject->padfM[nVertStart+nVertCount-i-1] = dfSaved;
  1928. }
  1929. }
  1930. }
  1931. }
  1932. return bAltered;
  1933. }