PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/C/shapelib-1.2.10/shputils.c

https://github.com/SumanthMN/census_utils
C | 1050 lines | 712 code | 90 blank | 248 comment | 260 complexity | 7b3a0fa6160cd46921fb209cb14cba68 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /******************************************************************************
  2. * $Id: shputils.c,v 1.7 2003/02/25 17:20:22 warmerda Exp $
  3. *
  4. * Project: Shapelib
  5. * Purpose:
  6. * Altered "shpdump" and "dbfdump" to allow two files to be appended.
  7. * Other Functions:
  8. * Selecting from the DBF before the write occurs.
  9. * Change the UNITS between Feet and Meters and Shift X,Y.
  10. * Clip and Erase boundary. The program only passes thru the
  11. * data once.
  12. *
  13. * Bill Miller North Carolina - Department of Transporation
  14. * Feb. 1997 -- bmiller@dot.state.nc.us
  15. * There was not a lot of time to debug hidden problems;
  16. * And the code is not very well organized or documented.
  17. * The clip/erase function was not well tested.
  18. * Oct. 2000 -- bmiller@dot.state.nc.us
  19. * Fixed the problem when select is using numbers
  20. * larger than short integer. It now reads long integer.
  21. * NOTE: DBF files created using windows NT will read as a string with
  22. * a length of 381 characters. This is a bug in "dbfopen".
  23. *
  24. *
  25. * Author: Bill Miller (bmiller@dot.state.nc.us)
  26. *
  27. ******************************************************************************
  28. * Copyright (c) 1999, Frank Warmerdam
  29. *
  30. * This software is available under the following "MIT Style" license,
  31. * or at the option of the licensee under the LGPL (see LICENSE.LGPL). This
  32. * option is discussed in more detail in shapelib.html.
  33. *
  34. * --
  35. *
  36. * Permission is hereby granted, free of charge, to any person obtaining a
  37. * copy of this software and associated documentation files (the "Software"),
  38. * to deal in the Software without restriction, including without limitation
  39. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  40. * and/or sell copies of the Software, and to permit persons to whom the
  41. * Software is furnished to do so, subject to the following conditions:
  42. *
  43. * The above copyright notice and this permission notice shall be included
  44. * in all copies or substantial portions of the Software.
  45. *
  46. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  47. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  48. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  49. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  50. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  51. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  52. * DEALINGS IN THE SOFTWARE.
  53. ******************************************************************************
  54. *
  55. * $Log: shputils.c,v $
  56. * Revision 1.7 2003/02/25 17:20:22 warmerda
  57. * Set psCShape to NULL after SHPDestroyObject() to avoid multi-frees of
  58. * the same memory ... as submitted by Fred Fox.
  59. *
  60. * Revision 1.6 2001/08/28 13:57:14 warmerda
  61. * fixed DBFAddField return value check
  62. *
  63. * Revision 1.5 2000/11/02 13:52:48 warmerda
  64. * major upgrade from Bill Miller
  65. *
  66. * Revision 1.4 1999/11/05 14:12:05 warmerda
  67. * updated license terms
  68. *
  69. * Revision 1.3 1998/12/03 15:47:39 warmerda
  70. * Did a bunch of rewriting to make it work with the V1.2 API.
  71. *
  72. * Revision 1.2 1998/06/18 01:19:49 warmerda
  73. * Made C++ compilable.
  74. *
  75. * Revision 1.1 1997/05/27 20:40:27 warmerda
  76. * Initial revision
  77. */
  78. static char rcsid[] =
  79. "$Id: shputils.c,v 1.7 2003/02/25 17:20:22 warmerda Exp $";
  80. #include "shapefil.h"
  81. #include "string.h"
  82. #ifndef FALSE
  83. # define FALSE 0
  84. # define TRUE 1
  85. #endif
  86. char infile[80], outfile[80], temp[400];
  87. /* Variables for shape files */
  88. SHPHandle hSHP;
  89. SHPHandle hSHPappend;
  90. int nShapeType, nEntities, iPart;
  91. int nShapeTypeAppend, nEntitiesAppend;
  92. SHPObject *psCShape;
  93. double adfBoundsMin[4], adfBoundsMax[4];
  94. /* Variables for DBF files */
  95. DBFHandle hDBF;
  96. DBFHandle hDBFappend;
  97. DBFFieldType iType;
  98. DBFFieldType jType;
  99. char iszTitle[12];
  100. char jszTitle[12];
  101. int *pt;
  102. char iszFormat[32], iszField[1024];
  103. char jszFormat[32], jszField[1024];
  104. int i, ti, iWidth, iDecimals, iRecord;
  105. int j, tj, jWidth, jDecimals, jRecord;
  106. int found, newdbf;
  107. void openfiles(void);
  108. void setext(char *pt, char *ext);
  109. int strncasecmp2(char *s1, char *s2, int n);
  110. void mergefields(void);
  111. void findselect(void);
  112. void showitems(void);
  113. int selectrec();
  114. int check_theme_bnd();
  115. int clip_boundary();
  116. void error();
  117. /* -------------------------------------------------------------------- */
  118. /* Variables for the DESCRIBE function */
  119. /* -------------------------------------------------------------------- */
  120. int ilist = FALSE, iall = FALSE;
  121. /* -------------------------------------------------------------------- */
  122. /* Variables for the SELECT function */
  123. /* -------------------------------------------------------------------- */
  124. int found = FALSE, newdbf = FALSE;
  125. char selectitem[40], *cpt;
  126. long int selectvalues[150], selcount=0;
  127. int iselect = FALSE, iselectitem = -1;
  128. int iunselect = FALSE;
  129. /* -------------------------------------------------------------------- */
  130. /* Variables for the CLIP and ERASE functions */
  131. /* -------------------------------------------------------------------- */
  132. double cxmin, cymin, cxmax, cymax;
  133. int iclip = FALSE, ierase = FALSE;
  134. int itouch = FALSE, iinside = FALSE, icut = FALSE;
  135. int ibound = FALSE, ipoly = FALSE;
  136. char clipfile[80];
  137. /* -------------------------------------------------------------------- */
  138. /* Variables for the FACTOR function */
  139. /* -------------------------------------------------------------------- */
  140. double infactor,outfactor,factor = 0; /* NO FACTOR */
  141. int iunit = FALSE;
  142. int ifactor = FALSE;
  143. /* -------------------------------------------------------------------- */
  144. /* Variables for the SHIFT function */
  145. /* -------------------------------------------------------------------- */
  146. double xshift = 0, yshift = 0; /* NO SHIFT */
  147. int main( int argc, char ** argv )
  148. {
  149. /* -------------------------------------------------------------------- */
  150. /* Check command line usage. */
  151. /* -------------------------------------------------------------------- */
  152. if( argc < 2 ) error();
  153. strcpy(infile, argv[1]);
  154. if (argc > 2) {
  155. strcpy(outfile,argv[2]);
  156. if (strncasecmp2(outfile, "LIST",0) == 0) { ilist = TRUE; }
  157. if (strncasecmp2(outfile, "ALL",0) == 0) { iall = TRUE; }
  158. }
  159. if (ilist || iall || argc == 2 ) {
  160. setext(infile, "shp");
  161. printf("DESCRIBE: %s\n",infile);
  162. strcpy(outfile,"");
  163. }
  164. /* -------------------------------------------------------------------- */
  165. /* Look for other functions on the command line. (SELECT, UNIT) */
  166. /* -------------------------------------------------------------------- */
  167. for (i = 3; i < argc; i++)
  168. {
  169. if ((strncasecmp2(argv[i], "SEL",3) == 0) ||
  170. (strncasecmp2(argv[i], "UNSEL",5) == 0))
  171. {
  172. if (strncasecmp2(argv[i], "UNSEL",5) == 0) iunselect=TRUE;
  173. i++;
  174. if (i >= argc) error();
  175. strcpy(selectitem,argv[i]);
  176. i++;
  177. if (i >= argc) error();
  178. selcount=0;
  179. strcpy(temp,argv[i]);
  180. cpt=temp;
  181. tj = atoi(cpt);
  182. ti = 0;
  183. while (tj>0) {
  184. selectvalues[selcount] = tj;
  185. while( *cpt >= '0' && *cpt <= '9')
  186. cpt++;
  187. while( *cpt > '\0' && (*cpt < '0' || *cpt > '9') )
  188. cpt++;
  189. tj=atoi(cpt);
  190. selcount++;
  191. }
  192. iselect=TRUE;
  193. } /*** End SEL & UNSEL ***/
  194. else
  195. if ((strncasecmp2(argv[i], "CLIP",4) == 0) ||
  196. (strncasecmp2(argv[i], "ERASE",5) == 0))
  197. {
  198. if (strncasecmp2(argv[i], "ERASE",5) == 0) ierase=TRUE;
  199. i++;
  200. if (i >= argc) error();
  201. strcpy(clipfile,argv[i]);
  202. sscanf(argv[i],"%lf",&cxmin);
  203. i++;
  204. if (i >= argc) error();
  205. if (strncasecmp2(argv[i], "BOUND",5) == 0) {
  206. setext(clipfile, "shp");
  207. hSHP = SHPOpen( clipfile, "rb" );
  208. if( hSHP == NULL )
  209. {
  210. printf( "ERROR: Unable to open the clip shape file:%s\n", clipfile );
  211. exit( 1 );
  212. }
  213. SHPGetInfo( hSHPappend, NULL, NULL,
  214. adfBoundsMin, adfBoundsMax );
  215. cxmin = adfBoundsMin[0];
  216. cymin = adfBoundsMin[1];
  217. cxmax = adfBoundsMax[0];
  218. cymax = adfBoundsMax[1];
  219. printf("Theme Clip Boundary: (%lf,%lf) - (%lf,%lf)\n",
  220. cxmin, cymin, cxmax, cymax);
  221. ibound=TRUE;
  222. } else { /*** xmin,ymin,xmax,ymax ***/
  223. sscanf(argv[i],"%lf",&cymin);
  224. i++;
  225. if (i >= argc) error();
  226. sscanf(argv[i],"%lf",&cxmax);
  227. i++;
  228. if (i >= argc) error();
  229. sscanf(argv[i],"%lf",&cymax);
  230. printf("Clip Box: (%lf,%lf) - (%lf,%lf)\n",cxmin, cymin, cxmax, cymax);
  231. }
  232. i++;
  233. if (i >= argc) error();
  234. if (strncasecmp2(argv[i], "CUT",3) == 0) icut=TRUE;
  235. else if (strncasecmp2(argv[i], "TOUCH",5) == 0) itouch=TRUE;
  236. else if (strncasecmp2(argv[i], "INSIDE",6) == 0) iinside=TRUE;
  237. else error();
  238. iclip=TRUE;
  239. } /*** End CLIP & ERASE ***/
  240. else if (strncasecmp2(argv[i], "FACTOR",0) == 0)
  241. {
  242. i++;
  243. if (i >= argc) error();
  244. infactor=findunit(argv[i]);
  245. if (infactor == 0) error();
  246. iunit=TRUE;
  247. i++;
  248. if (i >= argc) error();
  249. outfactor=findunit(argv[i]);
  250. if (outfactor == 0)
  251. {
  252. sscanf(argv[i],"%lf",&factor);
  253. if (factor == 0) error();
  254. }
  255. if (factor == 0)
  256. {
  257. if (infactor ==0)
  258. { puts("ERROR: Input unit must be defined before output unit"); exit(); }
  259. factor=infactor/outfactor;
  260. }
  261. printf("Output file coordinate values will be factored by %lg\n",factor);
  262. ifactor=(factor != 1); /* True if a valid factor */
  263. } /*** End FACTOR ***/
  264. else if (strncasecmp2(argv[i],"SHIFT",5) == 0)
  265. {
  266. i++;
  267. if (i >= argc) error();
  268. sscanf(argv[i],"%lf",&xshift);
  269. i++;
  270. if (i >= argc) error();
  271. sscanf(argv[i],"%lf",&yshift);
  272. iunit=TRUE;
  273. printf("X Shift: %lg Y Shift: %lg\n",xshift,yshift);
  274. } /*** End SHIFT ***/
  275. else {
  276. printf("ERROR: Unknown function %s\n",argv[i]); error();
  277. }
  278. }
  279. /* -------------------------------------------------------------------- */
  280. /* If there is no data in this file let the user know. */
  281. /* -------------------------------------------------------------------- */
  282. openfiles(); /* Open the infile and the outfile for shape and dbf. */
  283. if( DBFGetFieldCount(hDBF) == 0 )
  284. {
  285. puts( "There are no fields in this table!" );
  286. exit( 1 );
  287. }
  288. /* -------------------------------------------------------------------- */
  289. /* Print out the file bounds. */
  290. /* -------------------------------------------------------------------- */
  291. iRecord = DBFGetRecordCount( hDBF );
  292. SHPGetInfo( hSHP, NULL, NULL, adfBoundsMin, adfBoundsMax );
  293. printf( "Input Bounds: (%lg,%lg) - (%lg,%lg) Entities: %d DBF: %d\n",
  294. adfBoundsMin[0], adfBoundsMin[1],
  295. adfBoundsMax[0], adfBoundsMax[1],
  296. nEntities, iRecord );
  297. if (strcmp(outfile,"") == 0) /* Describe the shapefile; No other functions */
  298. {
  299. ti = DBFGetFieldCount( hDBF );
  300. showitems();
  301. exit(0);
  302. }
  303. if (iclip) check_theme_bnd();
  304. jRecord = DBFGetRecordCount( hDBFappend );
  305. SHPGetInfo( hSHPappend, NULL, NULL, adfBoundsMin, adfBoundsMax );
  306. if (nEntitiesAppend == 0)
  307. puts("New Output File\n");
  308. else
  309. printf( "Append Bounds: (%lg,%lg)-(%lg,%lg) Entities: %d DBF: %d\n",
  310. adfBoundsMin[0], adfBoundsMin[1],
  311. adfBoundsMax[0], adfBoundsMax[1],
  312. nEntitiesAppend, jRecord );
  313. /* -------------------------------------------------------------------- */
  314. /* Find matching fields in the append file or add new items. */
  315. /* -------------------------------------------------------------------- */
  316. mergefields();
  317. /* -------------------------------------------------------------------- */
  318. /* Find selection field if needed. */
  319. /* -------------------------------------------------------------------- */
  320. if (iselect) findselect();
  321. /* -------------------------------------------------------------------- */
  322. /* Read all the records */
  323. /* -------------------------------------------------------------------- */
  324. jRecord = DBFGetRecordCount( hDBFappend );
  325. for( iRecord = 0; iRecord < nEntities; iRecord++) /** DBFGetRecordCount(hDBF) **/
  326. {
  327. /* -------------------------------------------------------------------- */
  328. /* SELECT for values if needed. (Can the record be skipped.) */
  329. /* -------------------------------------------------------------------- */
  330. if (iselect)
  331. if (selectrec() == 0) goto SKIP_RECORD; /** SKIP RECORD **/
  332. /* -------------------------------------------------------------------- */
  333. /* Read a Shape record */
  334. /* -------------------------------------------------------------------- */
  335. psCShape = SHPReadObject( hSHP, iRecord );
  336. /* -------------------------------------------------------------------- */
  337. /* Clip coordinates of shapes if needed. */
  338. /* -------------------------------------------------------------------- */
  339. if (iclip)
  340. if (clip_boundary() == 0) goto SKIP_RECORD; /** SKIP RECORD **/
  341. /* -------------------------------------------------------------------- */
  342. /* Read a DBF record and copy each field. */
  343. /* -------------------------------------------------------------------- */
  344. for( i = 0; i < DBFGetFieldCount(hDBF); i++ )
  345. {
  346. /* -------------------------------------------------------------------- */
  347. /* Store the record according to the type and formatting */
  348. /* information implicit in the DBF field description. */
  349. /* -------------------------------------------------------------------- */
  350. if (pt[i] > -1) /* if the current field exists in output file */
  351. {
  352. switch( DBFGetFieldInfo( hDBF, i, NULL, &iWidth, &iDecimals ) )
  353. {
  354. case FTString:
  355. DBFWriteStringAttribute(hDBFappend, jRecord, pt[i],
  356. (DBFReadStringAttribute( hDBF, iRecord, i )) );
  357. break;
  358. case FTInteger:
  359. DBFWriteIntegerAttribute(hDBFappend, jRecord, pt[i],
  360. (DBFReadIntegerAttribute( hDBF, iRecord, i )) );
  361. break;
  362. case FTDouble:
  363. DBFWriteDoubleAttribute(hDBFappend, jRecord, pt[i],
  364. (DBFReadDoubleAttribute( hDBF, iRecord, i )) );
  365. break;
  366. }
  367. }
  368. }
  369. jRecord++;
  370. /* -------------------------------------------------------------------- */
  371. /* Change FACTOR and SHIFT coordinates of shapes if needed. */
  372. /* -------------------------------------------------------------------- */
  373. if (iunit)
  374. {
  375. for( j = 0; j < psCShape->nVertices; j++ )
  376. {
  377. psCShape->padfX[j] = psCShape->padfX[j] * factor + xshift;
  378. psCShape->padfY[j] = psCShape->padfY[j] * factor + yshift;
  379. }
  380. }
  381. /* -------------------------------------------------------------------- */
  382. /* Write the Shape record after recomputing current extents. */
  383. /* -------------------------------------------------------------------- */
  384. SHPComputeExtents( psCShape );
  385. SHPWriteObject( hSHPappend, -1, psCShape );
  386. SKIP_RECORD:
  387. SHPDestroyObject( psCShape );
  388. psCShape = NULL;
  389. j=0;
  390. }
  391. /* -------------------------------------------------------------------- */
  392. /* Print out the # of Entities and the file bounds. */
  393. /* -------------------------------------------------------------------- */
  394. jRecord = DBFGetRecordCount( hDBFappend );
  395. SHPGetInfo( hSHPappend, &nEntitiesAppend, &nShapeTypeAppend,
  396. adfBoundsMin, adfBoundsMax );
  397. printf( "Output Bounds: (%lg,%lg) - (%lg,%lg) Entities: %d DBF: %d\n\n",
  398. adfBoundsMin[0], adfBoundsMin[1],
  399. adfBoundsMax[0], adfBoundsMax[1],
  400. nEntitiesAppend, jRecord );
  401. /* -------------------------------------------------------------------- */
  402. /* Close the both shapefiles. */
  403. /* -------------------------------------------------------------------- */
  404. SHPClose( hSHP );
  405. SHPClose( hSHPappend );
  406. DBFClose( hDBF );
  407. DBFClose( hDBFappend );
  408. if (nEntitiesAppend == 0) {
  409. puts("Remove the output files.");
  410. setext(outfile, "dbf");
  411. remove(outfile);
  412. setext(outfile, "shp");
  413. remove(outfile);
  414. setext(outfile, "shx");
  415. remove(outfile);
  416. }
  417. return( 0 );
  418. }
  419. /************************************************************************/
  420. /* openfiles() */
  421. /************************************************************************/
  422. void openfiles() {
  423. /* -------------------------------------------------------------------- */
  424. /* Open the DBF file. */
  425. /* -------------------------------------------------------------------- */
  426. setext(infile, "dbf");
  427. hDBF = DBFOpen( infile, "rb" );
  428. if( hDBF == NULL )
  429. {
  430. printf( "ERROR: Unable to open the input DBF:%s\n", infile );
  431. exit( 1 );
  432. }
  433. /* -------------------------------------------------------------------- */
  434. /* Open the append DBF file. */
  435. /* -------------------------------------------------------------------- */
  436. if (strcmp(outfile,"")) {
  437. setext(outfile, "dbf");
  438. hDBFappend = DBFOpen( outfile, "rb+" );
  439. newdbf=0;
  440. if( hDBFappend == NULL )
  441. {
  442. newdbf=1;
  443. hDBFappend = DBFCreate( outfile );
  444. if( hDBFappend == NULL )
  445. {
  446. printf( "ERROR: Unable to open the append DBF:%s\n", outfile );
  447. exit( 1 );
  448. }
  449. }
  450. }
  451. /* -------------------------------------------------------------------- */
  452. /* Open the passed shapefile. */
  453. /* -------------------------------------------------------------------- */
  454. setext(infile, "shp");
  455. hSHP = SHPOpen( infile, "rb" );
  456. if( hSHP == NULL )
  457. {
  458. printf( "ERROR: Unable to open the input shape file:%s\n", infile );
  459. exit( 1 );
  460. }
  461. SHPGetInfo( hSHP, &nEntities, &nShapeType, NULL, NULL );
  462. /* -------------------------------------------------------------------- */
  463. /* Open the passed append shapefile. */
  464. /* -------------------------------------------------------------------- */
  465. if (strcmp(outfile,"")) {
  466. setext(outfile, "shp");
  467. hSHPappend = SHPOpen( outfile, "rb+" );
  468. if( hSHPappend == NULL )
  469. {
  470. hSHPappend = SHPCreate( outfile, nShapeType );
  471. if( hSHPappend == NULL )
  472. {
  473. printf( "ERROR: Unable to open the append shape file:%s\n",
  474. outfile );
  475. exit( 1 );
  476. }
  477. }
  478. SHPGetInfo( hSHPappend, &nEntitiesAppend, &nShapeTypeAppend,
  479. NULL, NULL );
  480. if (nShapeType != nShapeTypeAppend)
  481. {
  482. puts( "ERROR: Input and Append shape files are of different types.");
  483. exit( 1 );
  484. }
  485. }
  486. }
  487. /* -------------------------------------------------------------------- */
  488. /* Change the extension. If there is any extension on the */
  489. /* filename, strip it off and add the new extension */
  490. /* -------------------------------------------------------------------- */
  491. void setext(char *pt, char *ext)
  492. {
  493. int i;
  494. for( i = strlen(pt)-1;
  495. i > 0 && pt[i] != '.' && pt[i] != '/' && pt[i] != '\\';
  496. i-- ) {}
  497. if( pt[i] == '.' )
  498. pt[i] = '\0';
  499. strcat(pt,".");
  500. strcat(pt,ext);
  501. }
  502. /* -------------------------------------------------------------------- */
  503. /* Find matching fields in the append file. */
  504. /* Output file must have zero records to add any new fields. */
  505. /* -------------------------------------------------------------------- */
  506. void mergefields()
  507. {
  508. int i,j;
  509. ti = DBFGetFieldCount( hDBF );
  510. tj = DBFGetFieldCount( hDBFappend );
  511. /* Create a pointer array for the max # of fields in the output file */
  512. pt = (int *) malloc( (ti+tj+1) * sizeof(int) );
  513. for( i = 0; i < ti; i++ )
  514. {
  515. pt[i]= -1; /* Initial pt values to -1 */
  516. }
  517. /* DBF must be empty before adding items */
  518. jRecord = DBFGetRecordCount( hDBFappend );
  519. for( i = 0; i < ti; i++ )
  520. {
  521. iType = DBFGetFieldInfo( hDBF, i, iszTitle, &iWidth, &iDecimals );
  522. found=FALSE;
  523. {
  524. for( j = 0; j < tj; j++ ) /* Search all field names for a match */
  525. {
  526. jType = DBFGetFieldInfo( hDBFappend, j, jszTitle, &jWidth, &jDecimals );
  527. if (iType == jType && (strcmp(iszTitle, jszTitle) == 0) )
  528. {
  529. if (found || newdbf)
  530. {
  531. if (i == j) pt[i]=j;
  532. printf("Warning: Duplicate field name found (%s)\n",iszTitle);
  533. /* Duplicate field name
  534. (Try to guess the correct field by position) */
  535. }
  536. else
  537. {
  538. pt[i]=j; found=TRUE;
  539. }
  540. }
  541. }
  542. }
  543. if (pt[i] == -1 && (! found) ) /* Try to force into an existing field */
  544. { /* Ignore the field name, width, and decimal places */
  545. jType = DBFGetFieldInfo( hDBFappend, j, jszTitle, &jWidth, &jDecimals );
  546. if (iType == jType)
  547. {
  548. pt[i]=i; found=1;
  549. }
  550. }
  551. if ( (! found) && jRecord == 0) /* Add missing field to the append table */
  552. { /* The output DBF must be is empty */
  553. pt[i]=tj;
  554. tj++;
  555. if( DBFAddField( hDBFappend, iszTitle, iType, iWidth, iDecimals )
  556. == -1 )
  557. {
  558. printf( "Warning: DBFAddField(%s, TYPE:%d, WIDTH:%d DEC:%d, ITEM#:%d of %d) failed.\n",
  559. iszTitle, iType, iWidth, iDecimals, (i+1), (ti+1) );
  560. pt[i]=-1;
  561. }
  562. }
  563. }
  564. }
  565. void findselect()
  566. {
  567. /* Find the select field name */
  568. iselectitem = -1;
  569. for( i = 0; i < ti && iselectitem < 0; i++ )
  570. {
  571. iType = DBFGetFieldInfo( hDBF, i, iszTitle, &iWidth, &iDecimals );
  572. if (strncasecmp2(iszTitle, selectitem, 0) == 0) iselectitem = i;
  573. }
  574. if (iselectitem == -1)
  575. {
  576. printf("Warning: Item not found for selection (%s)\n",selectitem);
  577. iselect = FALSE;
  578. iall = FALSE;
  579. showitems();
  580. printf("Continued... (Selecting entire file)\n");
  581. }
  582. /* Extract all of the select values (by field type) */
  583. }
  584. void showitems()
  585. {
  586. char stmp[40],slow[40],shigh[40];
  587. double dtmp,dlow,dhigh,dsum,mean;
  588. long int itmp,ilow,ihigh,isum;
  589. long int maxrec;
  590. char *pt;
  591. printf("Available Items: (%d)",ti);
  592. maxrec = DBFGetRecordCount(hDBF);
  593. if (maxrec > 5000 && ! iall)
  594. { maxrec=5000; printf(" ** ESTIMATED RANGES (MEAN) For more records use \"All\""); }
  595. else { printf(" RANGES (MEAN)"); }
  596. for( i = 0; i < ti; i++ )
  597. {
  598. switch( DBFGetFieldInfo( hDBF, i, iszTitle, &iWidth, &iDecimals ) )
  599. {
  600. case FTString:
  601. strcpy(slow, "~");
  602. strcpy(shigh,"\0");
  603. printf("\n String %3d %-16s",iWidth,iszTitle);
  604. for( iRecord = 0; iRecord < maxrec; iRecord++ ) {
  605. strncpy(stmp,DBFReadStringAttribute( hDBF, iRecord, i ),39);
  606. if (strcmp(stmp,"!!") > 0) {
  607. if (strncasecmp2(stmp,slow,0) < 0) strncpy(slow, stmp,39);
  608. if (strncasecmp2(stmp,shigh,0) > 0) strncpy(shigh,stmp,39);
  609. }
  610. }
  611. pt=slow+strlen(slow)-1;
  612. while(*pt == ' ') { *pt='\0'; pt--; }
  613. pt=shigh+strlen(shigh)-1;
  614. while(*pt == ' ') { *pt='\0'; pt--; }
  615. if (strncasecmp2(slow,shigh,0) < 0) printf("%s to %s",slow,shigh);
  616. else if (strncasecmp2(slow,shigh,0) == 0) printf("= %s",slow);
  617. else printf("No Values");
  618. break;
  619. case FTInteger:
  620. printf("\n Integer %3d %-16s",iWidth,iszTitle);
  621. ilow = 1999999999;
  622. ihigh= -1999999999;
  623. isum = 0;
  624. for( iRecord = 0; iRecord < maxrec; iRecord++ ) {
  625. itmp = DBFReadIntegerAttribute( hDBF, iRecord, i );
  626. if (ilow > itmp) ilow = itmp;
  627. if (ihigh < itmp) ihigh = itmp;
  628. isum = isum + itmp;
  629. }
  630. mean=isum/maxrec;
  631. if (ilow < ihigh) printf("%d to %d \t(%.1f)",ilow,ihigh,mean);
  632. else if (ilow == ihigh) printf("= %d",ilow);
  633. else printf("No Values");
  634. break;
  635. case FTDouble:
  636. printf("\n Real %3d,%d %-16s",iWidth,iDecimals,iszTitle);
  637. dlow = 999999999999999.0;
  638. dhigh= -999999999999999.0;
  639. dsum = 0;
  640. for( iRecord = 0; iRecord < maxrec; iRecord++ ) {
  641. dtmp = DBFReadDoubleAttribute( hDBF, iRecord, i );
  642. if (dlow > dtmp) dlow = dtmp;
  643. if (dhigh < dtmp) dhigh = dtmp;
  644. dsum = dsum + dtmp;
  645. }
  646. mean=dsum/maxrec;
  647. sprintf(stmp,"%%.%df to %%.%df \t(%%.%df)",iDecimals,iDecimals,iDecimals);
  648. if (dlow < dhigh) printf(stmp,dlow,dhigh,mean);
  649. else if (dlow == dhigh) {
  650. sprintf(stmp,"= %%.%df",iDecimals);
  651. printf(stmp,dlow);
  652. }
  653. else printf("No Values");
  654. break;
  655. }
  656. }
  657. printf("\n");
  658. }
  659. int selectrec()
  660. {
  661. long int value, ty;
  662. ty = DBFGetFieldInfo( hDBF, iselectitem, NULL, &iWidth, &iDecimals);
  663. switch(ty)
  664. {
  665. case FTString:
  666. puts("Invalid Item");
  667. iselect=FALSE;
  668. break;
  669. case FTInteger:
  670. value = DBFReadIntegerAttribute( hDBF, iRecord, iselectitem );
  671. for (j = 0; j<selcount; j++)
  672. {
  673. if (selectvalues[j] == value)
  674. if (iunselect) return(0); /* Keep this record */
  675. else return(1); /* Skip this record */
  676. }
  677. break;
  678. case FTDouble:
  679. puts("Invalid Item");
  680. iselect=FALSE;
  681. break;
  682. }
  683. if (iunselect) return(1); /* Skip this record */
  684. else return(0); /* Keep this record */
  685. }
  686. int check_theme_bnd()
  687. {
  688. if ( (adfBoundsMin[0] >= cxmin) && (adfBoundsMax[0] <= cxmax) &&
  689. (adfBoundsMin[1] >= cymin) && (adfBoundsMax[1] <= cymax) )
  690. { /** Theme is totally inside clip area **/
  691. if (ierase) nEntities=0; /** SKIP THEME **/
  692. else iclip=FALSE; /** WRITE THEME (Clip not needed) **/
  693. }
  694. if ( ( (adfBoundsMin[0] < cxmin) && (adfBoundsMax[0] < cxmin) ) ||
  695. ( (adfBoundsMin[1] < cymin) && (adfBoundsMax[1] < cymin) ) ||
  696. ( (adfBoundsMin[0] > cxmax) && (adfBoundsMax[0] > cxmax) ) ||
  697. ( (adfBoundsMin[1] > cymax) && (adfBoundsMax[1] > cymax) ) )
  698. { /** Theme is totally outside clip area **/
  699. if (ierase) iclip=FALSE; /** WRITE THEME (Clip not needed) **/
  700. else nEntities=0; /** SKIP THEME **/
  701. }
  702. if (nEntities == 0)
  703. puts("WARNING: Theme is outside the clip area."); /** SKIP THEME **/
  704. }
  705. clip_boundary()
  706. {
  707. int inside;
  708. int prev_outside;
  709. int i2;
  710. int j2;
  711. /*** FIRST check the boundary of the feature ***/
  712. if ( ( (psCShape->dfXMin < cxmin) && (psCShape->dfXMax < cxmin) ) ||
  713. ( (psCShape->dfYMin < cymin) && (psCShape->dfYMax < cymin) ) ||
  714. ( (psCShape->dfXMin > cxmax) && (psCShape->dfXMax > cxmax) ) ||
  715. ( (psCShape->dfYMin > cymax) && (psCShape->dfYMax > cymax) ) )
  716. { /** Feature is totally outside clip area **/
  717. if (ierase) return(1); /** WRITE RECORD **/
  718. else return(0); /** SKIP RECORD **/
  719. }
  720. if ( (psCShape->dfXMin >= cxmin) && (psCShape->dfXMax <= cxmax) &&
  721. (psCShape->dfYMin >= cymin) && (psCShape->dfYMax <= cymax) )
  722. { /** Feature is totally inside clip area **/
  723. if (ierase) return(0); /** SKIP RECORD **/
  724. else return(1); /** WRITE RECORD **/
  725. }
  726. if (iinside)
  727. { /** INSIDE * Feature might touch the boundary or could be outside **/
  728. if (ierase) return(1); /** WRITE RECORD **/
  729. else return(0); /** SKIP RECORD **/
  730. }
  731. if (itouch)
  732. { /** TOUCH **/
  733. if ( ( (psCShape->dfXMin <= cxmin) || (psCShape->dfXMax >= cxmax) ) &&
  734. (psCShape->dfYMin >= cymin) && (psCShape->dfYMax <= cymax) )
  735. { /** Feature intersects the clip boundary only on the X axis **/
  736. if (ierase) return(0); /** SKIP RECORD **/
  737. else return(1); /** WRITE RECORD **/
  738. }
  739. if ( (psCShape->dfXMin >= cxmin) && (psCShape->dfXMax <= cxmax) &&
  740. ( (psCShape->dfYMin <= cymin) || (psCShape->dfYMax >= cymax) ) )
  741. { /** Feature intersects the clip boundary only on the Y axis **/
  742. if (ierase) return(0); /** SKIP RECORD **/
  743. else return(1); /** WRITE RECORD **/
  744. }
  745. for( j2 = 0; j2 < psCShape->nVertices; j2++ )
  746. { /** At least one vertex must be inside the clip boundary **/
  747. if ( (psCShape->padfX[j2] >= cxmin && psCShape->padfX[j2] <= cxmax) ||
  748. (psCShape->padfY[j2] >= cymin && psCShape->padfY[j2] <= cymax) )
  749. if (ierase) return(0); /** SKIP RECORD **/
  750. else return(1); /** WRITE RECORD **/
  751. }
  752. /** All vertices are outside the clip boundary **/
  753. if (ierase) return(1); /** WRITE RECORD **/
  754. else return(0); /** SKIP RECORD **/
  755. } /** End TOUCH **/
  756. if (icut)
  757. { /** CUT **/
  758. /*** Check each vertex in the feature with the Boundary and "CUT" ***/
  759. /*** THIS CODE WAS NOT COMPLETED! READ NOTE AT THE BOTTOM ***/
  760. i2=0;
  761. prev_outside=FALSE;
  762. for( j2 = 0; j2 < psCShape->nVertices; j2++ )
  763. {
  764. inside = psCShape->padfX[j2] >= cxmin && psCShape->padfX[j2] <= cxmax &&
  765. psCShape->padfY[j2] >= cymin && psCShape->padfY[j2] <= cymax ;
  766. if (ierase) inside=(! inside);
  767. if (inside)
  768. {
  769. if (i2 != j2)
  770. {
  771. if (prev_outside)
  772. {
  773. /*** AddIntersection(i2); /*** Add intersection ***/
  774. prev_outside=FALSE;
  775. }
  776. psCShape->padfX[i2]=psCShape->padfX[j2]; /** move vertex **/
  777. psCShape->padfY[i2]=psCShape->padfY[j2];
  778. }
  779. i2++;
  780. } else {
  781. if ( (! prev_outside) && (j2 > 0) )
  782. {
  783. /*** AddIntersection(i2); /*** Add intersection (Watch out for j2==i2-1) ***/
  784. /*** Also a polygon may overlap twice and will split into a several parts ***/
  785. prev_outside=TRUE;
  786. }
  787. }
  788. }
  789. printf("Vertices:%d OUT:%d Number of Parts:%d\n",
  790. psCShape->nVertices,i2, psCShape->nParts );
  791. psCShape->nVertices = i2;
  792. if (i2 < 2) return(0); /** SKIP RECORD **/
  793. /*** (WE ARE NOT CREATING INTERESECTIONS and some lines could be reduced to one point) **/
  794. if (i2 == 0) return(0); /** SKIP RECORD **/
  795. else return(1); /** WRITE RECORD **/
  796. } /** End CUT **/
  797. }
  798. /************************************************************************/
  799. /* strncasecmp2() */
  800. /* */
  801. /* Compare two strings up to n characters */
  802. /* If n=0 then s1 and s2 must be an exact match */
  803. /************************************************************************/
  804. int strncasecmp2(char *s1, char *s2, int n)
  805. {
  806. int j,i;
  807. if (n<1) n=strlen(s1)+1;
  808. for (i=0; i<n; i++)
  809. {
  810. if (*s1 != *s2)
  811. {
  812. if (*s1 >= 'a' && *s1 <= 'z') {
  813. j=*s1-32;
  814. if (j != *s2) return(*s1-*s2);
  815. } else {
  816. if (*s1 >= 'A' && *s1 <= 'Z') { j=*s1+32; }
  817. else { j=*s1; }
  818. if (j != *s2) return(*s1-*s2);
  819. }
  820. }
  821. s1++;
  822. s2++;
  823. }
  824. return(0);
  825. }
  826. #define NKEYS (sizeof(unitkeytab) / sizeof(struct unitkey))
  827. findunit(unit)
  828. char *unit;
  829. {
  830. struct unitkey {
  831. char *name;
  832. double value;
  833. } unitkeytab[] = {
  834. "CM", 39.37,
  835. "CENTIMETER", 39.37,
  836. "CENTIMETERS", 39.37, /** # of inches * 100 in unit **/
  837. "METER", 3937,
  838. "METERS", 3937,
  839. "KM", 3937000,
  840. "KILOMETER", 3937000,
  841. "KILOMETERS", 3937000,
  842. "INCH", 100,
  843. "INCHES", 100,
  844. "FEET", 1200,
  845. "FOOT", 1200,
  846. "YARD", 3600,
  847. "YARDS", 3600,
  848. "MILE", 6336000,
  849. "MILES", 6336000
  850. };
  851. double unitfactor=0;
  852. for (j = 0; j < NKEYS; j++) {
  853. if (strncasecmp2(unit, unitkeytab[j].name, 0) == 0) unitfactor=unitkeytab[j].value;
  854. }
  855. return(unitfactor);
  856. }
  857. /* -------------------------------------------------------------------- */
  858. /* Display a usage message. */
  859. /* -------------------------------------------------------------------- */
  860. void error()
  861. {
  862. puts( "The program will append to an existing shape file or it will" );
  863. puts( "create a new file if needed." );
  864. puts( "Only the items in the first output file will be preserved." );
  865. puts( "When an item does not match with the append theme then the item");
  866. puts( "might be placed to an existing item at the same position and type." );
  867. puts( " OTHER FUNCTIONS:" );
  868. puts( " - Describe all items in the dbase file (Use ALL for more than 5000 recs.)");
  869. puts( " - Select a group of shapes from a comma separated selection list.");
  870. puts( " - UnSelect a group of shapes from a comma separated selection list.");
  871. puts( " - Clip boundary extent or by theme boundary." );
  872. puts( " Touch writes all the shapes that touch the boundary.");
  873. puts( " Inside writes all the shapes that are completely within the boundary.");
  874. puts( " Boundary clips are only the min and max of a theme boundary." );
  875. puts( " - Erase boundary extent or by theme boundary." );
  876. puts( " Erase is the direct opposite of the Clip function." );
  877. puts( " - Change coordinate value units between meters and feet.");
  878. puts( " There is no way to determine the input unit of a shape file.");
  879. puts( " Skip this function if the shape file is already in the correct unit.");
  880. puts( " Clip and Erase will be done before the unit is changed.");
  881. puts( " A shift will be done after the unit is changed.");
  882. puts( " - Shift X and Y coordinates.\n" );
  883. puts( "Finally, There can only be one select or unselect in the command line.");
  884. puts( " There can only be one clip or erase in the command line.");
  885. puts( " There can only be one unit and only one shift in the command line.\n");
  886. puts( "Ex: shputils in.shp out.shp SELECT countycode 3,5,9,13,17,27");
  887. puts( " shputils in.shp out.shp CLIP 10 10 90 90 Touch FACTOR Meter Feet");
  888. puts( " shputils in.shp out.shp FACTOR Meter 3.0");
  889. puts( " shputils in.shp out.shp CLIP clip.shp Boundary Touch SHIFT 40 40");
  890. puts( " shputils in.shp out.shp SELECT co 112 CLIP clip.shp Boundary Touch\n");
  891. puts( "USAGE: shputils <DescribeShape> {ALL}");
  892. puts( "USAGE: shputils <InputShape> <OutShape|AppendShape>" );
  893. puts( " { <FACTOR> <FEET|MILES|METERS|KM> <FEET|MILES|METERS|KM|factor> }" );
  894. puts( " { <SHIFT> <xshift> <yshift> }" );
  895. puts( " { <SELECT|UNSEL> <Item> <valuelist> }" );
  896. puts( " { <CLIP|ERASE> <xmin> <ymin> <xmax> <ymax> <TOUCH|INSIDE|CUT> }" );
  897. puts( " { <CLIP|ERASE> <theme> <BOUNDARY> <TOUCH|INSIDE|CUT> }" );
  898. puts( " Note: CUT is not complete and does not create intersections.");
  899. puts( " For more information read programmer comment.");
  900. /**** Clip functions for Polygon and Cut is not supported
  901. There are several web pages that describe methods of doing this function.
  902. It seem easy to impliment until you start writting code. I don't have the
  903. time to add these functions but a did leave a simple cut routine in the
  904. program that can be called by using CUT instead of TOUCH in the
  905. CLIP or ERASE functions. It does not add the intersection of the line and
  906. the clip box, so polygons could look incomplete and lines will come up short.
  907. Information about clipping lines with a box:
  908. http://www.csclub.uwaterloo.ca/u/mpslager/articles/sutherland/wr.html
  909. Information about finding the intersection of two lines:
  910. http://www.whisqu.se/per/docs/math28.htm
  911. THE CODE LOOKS LIKE THIS:
  912. ********************************************************
  913. void Intersect_Lines(float x0,float y0,float x1,float y1,
  914. float x2,float y2,float x3,float y3,
  915. float *xi,float *yi)
  916. {
  917. // this function computes the intersection of the sent lines
  918. // and returns the intersection point, note that the function assumes
  919. // the lines intersect. the function can handle vertical as well
  920. // as horizontal lines. note the function isn't very clever, it simply
  921. // applies the math, but we don't need speed since this is a
  922. // pre-processing step
  923. // The Intersect_lines program came from (http://www.whisqu.se/per/docs/math28.htm)
  924. float a1,b1,c1, // constants of linear equations
  925. a2,b2,c2,
  926. det_inv, // the inverse of the determinant of the coefficientmatrix
  927. m1,m2; // the slopes of each line
  928. // compute slopes, note the cludge for infinity, however, this will
  929. // be close enough
  930. if ((x1-x0)!=0)
  931. m1 = (y1-y0)/(x1-x0);
  932. else
  933. m1 = (float)1e+10; // close enough to infinity
  934. if ((x3-x2)!=0)
  935. m2 = (y3-y2)/(x3-x2);
  936. else
  937. m2 = (float)1e+10; // close enough to infinity
  938. // compute constants
  939. a1 = m1;
  940. a2 = m2;
  941. b1 = -1;
  942. b2 = -1;
  943. c1 = (y0-m1*x0);
  944. c2 = (y2-m2*x2);
  945. // compute the inverse of the determinate
  946. det_inv = 1/(a1*b2 - a2*b1);
  947. // use Kramers rule to compute xi and yi
  948. *xi=((b1*c2 - b2*c1)*det_inv);
  949. *yi=((a2*c1 - a1*c2)*det_inv);
  950. } // end Intersect_Lines
  951. **********************************************************/
  952. exit( 1 );
  953. }