Advertisement
shouhei

shpdunp.c

Oct 30th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.55 KB | None | 0 0
  1. /******************************************************************************
  2. * $Id: shpdump.c,v 1.18 2011-07-24 03:05:14 fwarmerdam Exp $
  3. *
  4. * Project: Shapelib
  5. * Purpose: Sample application for dumping contents of a shapefile to
  6. * the terminal in human readable form.
  7. * Author: Frank Warmerdam, warmerdam@pobox.com
  8. *
  9. ******************************************************************************
  10. * Copyright (c) 1999, Frank Warmerdam
  11. *
  12. * This software is available under the following "MIT Style" license,
  13. * or at the option of the licensee under the LGPL (see LICENSE.LGPL). This
  14. * option is discussed in more detail in shapelib.html.
  15. *
  16. * --
  17. *
  18. * Permission is hereby granted, free of charge, to any person obtaining a
  19. * copy of this software and associated documentation files (the "Software"),
  20. * to deal in the Software without restriction, including without limitation
  21. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  22. * and/or sell copies of the Software, and to permit persons to whom the
  23. * Software is furnished to do so, subject to the following conditions:
  24. *
  25. * The above copyright notice and this permission notice shall be included
  26. * in all copies or substantial portions of the Software.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  29. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  31. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  33. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  34. * DEALINGS IN THE SOFTWARE.
  35. ******************************************************************************
  36. *
  37. * $Log: shpdump.c,v $
  38. * Revision 1.18 2011-07-24 03:05:14 fwarmerdam
  39. * use %.15g for formatting coordiantes in shpdump
  40. *
  41. * Revision 1.17 2010-07-01 07:33:04 fwarmerdam
  42. * do not crash in shpdump if null object returned
  43. *
  44. * Revision 1.16 2010-07-01 07:27:13 fwarmerdam
  45. * white space formatting adjustments
  46. *
  47. * Revision 1.15 2006-01-26 15:07:32 fwarmerdam
  48. * add bMeasureIsUsed flag from Craig Bruce: Bug 1249
  49. *
  50. * Revision 1.14 2005/02/11 17:17:46 fwarmerdam
  51. * added panPartStart[0] validation
  52. *
  53. * Revision 1.13 2004/09/26 20:09:35 fwarmerdam
  54. * avoid rcsid warnings
  55. *
  56. * Revision 1.12 2004/01/27 18:05:35 fwarmerdam
  57. * Added the -ho (header only) switch.
  58. *
  59. * Revision 1.11 2004/01/09 16:39:49 fwarmerdam
  60. * include standard include files
  61. *
  62. * Revision 1.10 2002/04/10 16:59:29 warmerda
  63. * added -validate switch
  64. *
  65. * Revision 1.9 2002/01/15 14:36:07 warmerda
  66. * updated email address
  67. *
  68. * Revision 1.8 2000/07/07 13:39:45 warmerda
  69. * removed unused variables, and added system include files
  70. *
  71. * Revision 1.7 1999/11/05 14:12:04 warmerda
  72. * updated license terms
  73. *
  74. * Revision 1.6 1998/12/03 15:48:48 warmerda
  75. * Added report of shapefile type, and total number of shapes.
  76. *
  77. * Revision 1.5 1998/11/09 20:57:36 warmerda
  78. * use SHPObject.
  79. *
  80. * Revision 1.4 1995/10/21 03:14:49 warmerda
  81. * Changed to use binary file access.
  82. *
  83. * Revision 1.3 1995/08/23 02:25:25 warmerda
  84. * Added support for bounds.
  85. *
  86. * Revision 1.2 1995/08/04 03:18:11 warmerda
  87. * Added header.
  88. *
  89. */
  90.  
  91. #include <string.h>
  92. #include <stdlib.h>
  93. #include "shapefil.h"
  94.  
  95. SHP_CVSID("$Id: shpdump.c,v 1.18 2011-07-24 03:05:14 fwarmerdam Exp $")
  96.  
  97. int main( int argc, char ** argv )
  98.  
  99. {
  100. FILE *fpo;
  101. SHPHandle hSHP;
  102. int nShapeType, nEntities, i, iPart, bValidate = 0,nInvalidCount=0;
  103. int bHeaderOnly = 0;
  104. const char *pszPlus;
  105. double adfMinBound[4], adfMaxBound[4];
  106.  
  107. if((fpo=fopen(argv[2],"w"))==NULL)
  108. {
  109. printf("The file can't be opened. The program is exit.\n");
  110.  
  111. return 0;
  112. }
  113.  
  114. if( argc > 1 && strcmp(argv[1],"-validate") == 0 )
  115. {
  116. bValidate = 1;
  117. argv++;
  118. argc--;
  119. }
  120.  
  121. if( argc > 1 && strcmp(argv[1],"-ho") == 0 )
  122. {
  123. bHeaderOnly = 1;
  124. argv++;
  125. argc--;
  126. }
  127.  
  128. /* -------------------------------------------------------------------- */
  129. /* Display a usage message. */
  130. /* -------------------------------------------------------------------- */
  131. if( argc != 3 )
  132. {
  133. printf( "shpdump [-validate] [-ho] shp_file\n" );
  134. exit( 1 );
  135. }
  136.  
  137. /* -------------------------------------------------------------------- */
  138. /* Open the passed shapefile. */
  139. /* -------------------------------------------------------------------- */
  140. hSHP = SHPOpen( argv[1], "rb" );
  141.  
  142. if( hSHP == NULL )
  143. {
  144. printf( "Unable to open:%s\n", argv[1] );
  145. exit( 1 );
  146. }
  147.  
  148. /* -------------------------------------------------------------------- */
  149. /* Print out the file bounds. */
  150. /* -------------------------------------------------------------------- */
  151. SHPGetInfo( hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound );
  152.  
  153. printf( "Shapefile Type: %s # of Shapes: %d\n\n",
  154. SHPTypeName( nShapeType ), nEntities );
  155.  
  156. printf( "File Bounds: (%.15g,%.15g,%.15g,%.15g)\n"
  157. " to (%.15g,%.15g,%.15g,%.15g)\n",
  158.  
  159. adfMinBound[0],
  160. adfMinBound[1],
  161. adfMinBound[2],
  162. adfMinBound[3],
  163. adfMaxBound[0],
  164. adfMaxBound[1],
  165. adfMaxBound[2],
  166. adfMaxBound[3] );
  167. fprintf(fpo, "%.15g,%.15g,%.15g,%.15g,%.15g,%.15g,%.15g,%.15g\n",
  168. adfMinBound[0],
  169. adfMinBound[1],
  170. adfMinBound[2],
  171. adfMinBound[3],
  172. adfMaxBound[0],
  173. adfMaxBound[1],
  174. adfMaxBound[2],
  175. adfMaxBound[3] );
  176. /* -------------------------------------------------------------------- */
  177. /* Skim over the list of shapes, printing all the vertices. */
  178. /* -------------------------------------------------------------------- */
  179. for( i = 0; i < nEntities && !bHeaderOnly; i++ )
  180. {
  181. int j;
  182. SHPObject *psShape;
  183.  
  184. psShape = SHPReadObject( hSHP, i );
  185.  
  186. if( psShape == NULL )
  187. {
  188. fprintf( stderr,
  189. "Unable to read shape %d, terminating object reading.\n",
  190. i );
  191. break;
  192. }
  193.  
  194. if( psShape->bMeasureIsUsed )
  195. printf( "\nShape:%d (%s) nVertices=%d, nParts=%d\n"
  196. " Bounds:(%.15g,%.15g, %.15g, %.15g)\n"
  197. " to (%.15g,%.15g, %.15g, %.15g)\n",
  198. i, SHPTypeName(psShape->nSHPType),
  199. psShape->nVertices, psShape->nParts,
  200. psShape->dfXMin, psShape->dfYMin,
  201. psShape->dfZMin, psShape->dfMMin,
  202. psShape->dfXMax, psShape->dfYMax,
  203. psShape->dfZMax, psShape->dfMMax );
  204. else
  205. printf( "\nShape:%d (%s) nVertices=%d, nParts=%d\n"
  206. " Bounds:(%.15g,%.15g, %.15g)\n"
  207. " to (%.15g,%.15g, %.15g)\n",
  208. i, SHPTypeName(psShape->nSHPType),
  209. psShape->nVertices, psShape->nParts,
  210. psShape->dfXMin, psShape->dfYMin,
  211. psShape->dfZMin,
  212. psShape->dfXMax, psShape->dfYMax,
  213. psShape->dfZMax );
  214.  
  215. if( psShape->nParts > 0 && psShape->panPartStart[0] != 0 )
  216. {
  217. fprintf( stderr, "panPartStart[0] = %d, not zero as expected.\n",
  218. psShape->panPartStart[0] );
  219. }
  220.  
  221. for( j = 0, iPart = 1; j < psShape->nVertices; j++ )
  222. {
  223. const char *pszPartType = "";
  224.  
  225. if( j == 0 && psShape->nParts > 0 )
  226. pszPartType = SHPPartTypeName( psShape->panPartType[0] );
  227.  
  228. if( iPart < psShape->nParts
  229. && psShape->panPartStart[iPart] == j )
  230. {
  231. pszPartType = SHPPartTypeName( psShape->panPartType[iPart] );
  232. iPart++;
  233. pszPlus = "+";
  234. }
  235. else
  236. pszPlus = " ";
  237.  
  238. if( psShape->bMeasureIsUsed )
  239. printf(" %s (%.15g,%.15g, %.15g, %.15g) %s \n",
  240. pszPlus,
  241. psShape->padfX[j],
  242. psShape->padfY[j],
  243. psShape->padfZ[j],
  244. psShape->padfM[j],
  245. pszPartType );
  246. else
  247. printf(" %s (%.15g,%.15g, %.15g) %s \n",
  248. pszPlus,
  249. psShape->padfX[j],
  250. psShape->padfY[j],
  251. psShape->padfZ[j],
  252. pszPartType );
  253. }
  254.  
  255. if( bValidate )
  256. {
  257. int nAltered = SHPRewindObject( hSHP, psShape );
  258.  
  259. if( nAltered > 0 )
  260. {
  261. printf( " %d rings wound in the wrong direction.\n",
  262. nAltered );
  263. nInvalidCount++;
  264. }
  265. }
  266.  
  267. SHPDestroyObject( psShape );
  268. }
  269.  
  270. SHPClose( hSHP );
  271.  
  272. if( bValidate )
  273. {
  274. printf( "%d object has invalid ring orderings.\n", nInvalidCount );
  275. }
  276.  
  277. #ifdef USE_DBMALLOC
  278. malloc_dump(2);
  279. #endif
  280.  
  281. fclose(fpo);
  282. exit( 0 );
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement