shp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +34 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +3 -0
  5. data/LICENSE +28 -0
  6. data/README.md +30 -0
  7. data/Rakefile +23 -0
  8. data/ext/shp/base.hpp +113 -0
  9. data/ext/shp/dbf.cpp +381 -0
  10. data/ext/shp/dbf.hpp +44 -0
  11. data/ext/shp/extconf.rb +13 -0
  12. data/ext/shp/shape_object.cpp +58 -0
  13. data/ext/shp/shape_object.hpp +27 -0
  14. data/ext/shp/shapefile.cpp +299 -0
  15. data/ext/shp/shapefile.hpp +35 -0
  16. data/ext/shp/shapelib/.cvsignore +15 -0
  17. data/ext/shp/shapelib/ChangeLog +450 -0
  18. data/ext/shp/shapelib/HOWTO-RELEASE +16 -0
  19. data/ext/shp/shapelib/LICENSE.LGPL +483 -0
  20. data/ext/shp/shapelib/Makefile +113 -0
  21. data/ext/shp/shapelib/README +41 -0
  22. data/ext/shp/shapelib/README.tree +172 -0
  23. data/ext/shp/shapelib/contrib/.cvsignore +12 -0
  24. data/ext/shp/shapelib/contrib/Makefile +66 -0
  25. data/ext/shp/shapelib/contrib/ShapeFileII.pas +234 -0
  26. data/ext/shp/shapelib/contrib/Shape_PointInPoly.cpp +238 -0
  27. data/ext/shp/shapelib/contrib/Shape_PointInPoly_README.txt +59 -0
  28. data/ext/shp/shapelib/contrib/csv2shp.c +558 -0
  29. data/ext/shp/shapelib/contrib/dbfcat.c +166 -0
  30. data/ext/shp/shapelib/contrib/dbfinfo.c +106 -0
  31. data/ext/shp/shapelib/contrib/makefile.vc +34 -0
  32. data/ext/shp/shapelib/contrib/my_nan.h +46 -0
  33. data/ext/shp/shapelib/contrib/shpcat.c +100 -0
  34. data/ext/shp/shapelib/contrib/shpcentrd.c +159 -0
  35. data/ext/shp/shapelib/contrib/shpdata.c +129 -0
  36. data/ext/shp/shapelib/contrib/shpdxf.c +340 -0
  37. data/ext/shp/shapelib/contrib/shpfix.c +110 -0
  38. data/ext/shp/shapelib/contrib/shpgeo.c +1595 -0
  39. data/ext/shp/shapelib/contrib/shpgeo.h +154 -0
  40. data/ext/shp/shapelib/contrib/shpinfo.c +113 -0
  41. data/ext/shp/shapelib/contrib/shpproj.c +260 -0
  42. data/ext/shp/shapelib/contrib/shpsort.c +605 -0
  43. data/ext/shp/shapelib/contrib/shpsort.txt +44 -0
  44. data/ext/shp/shapelib/contrib/shpwkb.c +123 -0
  45. data/ext/shp/shapelib/contrib/tests/shpproj.sh +38 -0
  46. data/ext/shp/shapelib/dbfopen.c +2221 -0
  47. data/ext/shp/shapelib/makefile.vc +86 -0
  48. data/ext/shp/shapelib/makeshape.sh +21 -0
  49. data/ext/shp/shapelib/mkdist.sh +37 -0
  50. data/ext/shp/shapelib/mkinstalldirs +38 -0
  51. data/ext/shp/shapelib/mkrelease.sh +55 -0
  52. data/ext/shp/shapelib/safileio.c +286 -0
  53. data/ext/shp/shapelib/shapefil.h +647 -0
  54. data/ext/shp/shapelib/shapelib.def +46 -0
  55. data/ext/shp/shapelib/shpopen.c +2388 -0
  56. data/ext/shp/shapelib/shptree.c +1187 -0
  57. data/ext/shp/shapelib/shputils.c +1072 -0
  58. data/ext/shp/shapelib/stream1.out +1465 -0
  59. data/ext/shp/shapelib/stream1.sh +28 -0
  60. data/ext/shp/shapelib/stream2.out +530 -0
  61. data/ext/shp/shapelib/stream2.sh +11 -0
  62. data/ext/shp/shapelib/stream3.out +37 -0
  63. data/ext/shp/shapelib/web/.cvsignore +2 -0
  64. data/ext/shp/shapelib/web/codepage.html +403 -0
  65. data/ext/shp/shapelib/web/dbf_api.html +436 -0
  66. data/ext/shp/shapelib/web/index.html +235 -0
  67. data/ext/shp/shapelib/web/license.html +78 -0
  68. data/ext/shp/shapelib/web/manifest.html +87 -0
  69. data/ext/shp/shapelib/web/release.html +80 -0
  70. data/ext/shp/shapelib/web/shapelib-tools.html +352 -0
  71. data/ext/shp/shapelib/web/shp_api.html +376 -0
  72. data/ext/shp/shp.cpp +19 -0
  73. data/ext/shp/shp.hpp +47 -0
  74. data/lib/shp.rb +35 -0
  75. data/lib/shp/version.rb +3 -0
  76. data/shp.gemspec +23 -0
  77. data/spec/shp_spec.rb +127 -0
  78. metadata +176 -0
@@ -0,0 +1,159 @@
1
+ /******************************************************************************
2
+ * Copyright (c) 1999, Carl Anderson
3
+ *
4
+ * this code is based in part on the earlier work of Frank Warmerdam
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a
7
+ * copy of this software and associated documentation files (the "Software"),
8
+ * to deal in the Software without restriction, including without limitation
9
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
+ * and/or sell copies of the Software, and to permit persons to whom the
11
+ * Software is furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included
14
+ * in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
+ * DEALINGS IN THE SOFTWARE.
23
+ ******************************************************************************
24
+ *
25
+ * shpcentrd.c - compute XY centroid for complex shapes
26
+ * and create a new SHPT_PT file of then
27
+ * specifically undo compound objects but not complex ones
28
+ *
29
+ *
30
+ * $Log: shpcentrd.c,v $
31
+ * Revision 1.3 2011-07-24 03:17:46 fwarmerdam
32
+ * include string.h and stdlib.h where needed in contrib (#2146)
33
+ *
34
+ * Revision 1.2 1999-05-26 02:56:31 candrsn
35
+ * updates to shpdxf, dbfinfo, port from Shapelib 1.1.5 of dbfcat and shpinfo
36
+ *
37
+ *
38
+ *
39
+ */
40
+
41
+
42
+ /* the centroid is defined as
43
+ *
44
+ * Cx = sum (x dArea ) / Total Area
45
+ * and
46
+ * Cy = sum (y dArea ) / Total Area
47
+ */
48
+
49
+ #include <stdlib.h>
50
+ #include <string.h>
51
+ #include "shapefil.h"
52
+ #include "shpgeo.h"
53
+
54
+ int main( int argc, char ** argv )
55
+
56
+ {
57
+ SHPHandle old_SHP, new_SHP;
58
+ DBFHandle old_DBF, new_DBF;
59
+ int nShapeType, nEntities, nVertices, nParts, *panParts, i, iPart;
60
+ double *padVertices, adBounds[4];
61
+ const char *pszPlus;
62
+ DBFFieldType idfld_type;
63
+ int idfld, nflds;
64
+ char kv[257] = "";
65
+ char idfldName[120] = "";
66
+ char fldName[120] = "";
67
+ char shpFileName[120] = "";
68
+ char dbfFileName[120] = "";
69
+ double apeture[4];
70
+ char *DBFRow = NULL;
71
+ int Cpan[2] = { 0,0 };
72
+ int byRing = 1;
73
+ PT Centrd;
74
+ SHPObject *psCShape, *cent_pt;
75
+
76
+
77
+ if( argc < 3 )
78
+ {
79
+ printf( "shpcentrd shp_file new_shp_file\n" );
80
+ exit( 1 );
81
+ }
82
+
83
+ old_SHP = SHPOpen (argv[1], "rb" );
84
+ old_DBF = DBFOpen (argv[1], "rb");
85
+ if( old_SHP == NULL || old_DBF == NULL )
86
+ {
87
+ printf( "Unable to open old files:%s\n", argv[1] );
88
+ exit( 1 );
89
+ }
90
+
91
+ SHPGetInfo( old_SHP, &nEntities, &nShapeType, NULL, NULL );
92
+ new_SHP = SHPCreate ( argv[2], SHPT_POINT );
93
+
94
+ new_DBF = DBFCloneEmpty (old_DBF, argv[2]);
95
+ if( new_SHP == NULL || new_DBF == NULL )
96
+ {
97
+ printf( "Unable to create new files:%s\n", argv[2] );
98
+ exit( 1 );
99
+ }
100
+
101
+ DBFRow = (char *) malloc ( (old_DBF->nRecordLength) + 15 );
102
+
103
+
104
+ #ifdef DEBUG
105
+ printf ("ShpCentrd using shpgeo \n");
106
+ #endif
107
+
108
+ for( i = 0; i < nEntities; i++ )
109
+ {
110
+ int res ;
111
+
112
+ psCShape = SHPReadObject( old_SHP, i );
113
+
114
+ if ( byRing == 1 ) {
115
+ int ring;
116
+ for ( ring = 0; ring < psCShape->nParts; ring ++ ) {
117
+ SHPObject *psO;
118
+ psO = SHPClone ( psCShape, ring, ring + 1 );
119
+
120
+ Centrd = SHPCentrd_2d ( psO );
121
+
122
+ cent_pt = SHPCreateSimpleObject ( SHPT_POINT, 1,
123
+ (double*) &(Centrd.x), (double*) &(Centrd.y), NULL );
124
+
125
+ SHPWriteObject ( new_SHP, -1, cent_pt );
126
+
127
+ memcpy ( DBFRow, DBFReadTuple ( old_DBF, i ),
128
+ old_DBF->nRecordLength );
129
+ DBFWriteTuple ( new_DBF, new_DBF->nRecords, DBFRow );
130
+
131
+ SHPDestroyObject ( cent_pt );
132
+
133
+ SHPDestroyObject ( psO );
134
+ }
135
+
136
+ }
137
+ else {
138
+
139
+ Centrd = SHPCentrd_2d ( psCShape );
140
+
141
+ cent_pt = SHPCreateSimpleObject ( SHPT_POINT, 1,
142
+ (double*) &(Centrd.x), (double*) &(Centrd.y), NULL );
143
+
144
+ SHPWriteObject ( new_SHP, -1, cent_pt );
145
+
146
+ memcpy ( DBFRow, DBFReadTuple ( old_DBF, i ),
147
+ old_DBF->nRecordLength );
148
+ DBFWriteTuple ( new_DBF, new_DBF->nRecords, DBFRow );
149
+
150
+ SHPDestroyObject ( cent_pt );
151
+ }
152
+ }
153
+
154
+ SHPClose( old_SHP );
155
+ SHPClose( new_SHP );
156
+ DBFClose( old_DBF );
157
+ DBFClose( new_DBF );
158
+ printf ("\n");
159
+ }
@@ -0,0 +1,129 @@
1
+ /******************************************************************************
2
+ * Copyright (c) 1999, Carl Anderson
3
+ *
4
+ * this code is based in part on the earlier work of Frank Warmerdam
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a
7
+ * copy of this software and associated documentation files (the "Software"),
8
+ * to deal in the Software without restriction, including without limitation
9
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
+ * and/or sell copies of the Software, and to permit persons to whom the
11
+ * Software is furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included
14
+ * in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
+ * DEALINGS IN THE SOFTWARE.
23
+ ******************************************************************************
24
+ *
25
+ * shpdata.c - utility program for testing elements of the libraries
26
+ *
27
+ *
28
+ * $Log: shpdata.c,v $
29
+ * Revision 1.2 1999-05-26 02:56:31 candrsn
30
+ * updates to shpdxf, dbfinfo, port from Shapelib 1.1.5 of dbfcat and shpinfo
31
+ *
32
+ *
33
+ *
34
+ */
35
+
36
+
37
+ #include "shapefil.h"
38
+ #include "shpgeo.h"
39
+
40
+ int main( int argc, char ** argv )
41
+
42
+ {
43
+ SHPHandle old_SHP, new_SHP;
44
+ DBFHandle old_DBF, new_DBF;
45
+ int nShapeType, nEntities, nVertices, nParts, *panParts, i, iPart;
46
+ double *padVertices, adBounds[4];
47
+ const char *pszPlus;
48
+ DBFFieldType idfld_type;
49
+ int idfld, nflds;
50
+ char kv[257] = "";
51
+ char idfldName[120] = "";
52
+ char fldName[120] = "";
53
+ char shpFileName[120] = "";
54
+ char dbfFileName[120] = "";
55
+ char *DBFRow = NULL;
56
+ int Cpan[2] = { 0,0 };
57
+ int byRing = 1;
58
+ PT oCentrd, ringCentrd;
59
+ SHPObject *psCShape, *cent_pt;
60
+ double oArea = 0.0, oLen = 0.0;
61
+
62
+ if( argc < 2 )
63
+ {
64
+ printf( "shpdata shp_file \n" );
65
+ exit( 1 );
66
+ }
67
+
68
+ old_SHP = SHPOpen (argv[1], "rb" );
69
+ old_DBF = DBFOpen (argv[1], "rb");
70
+ if( old_SHP == NULL || old_DBF == NULL )
71
+ {
72
+ printf( "Unable to open old files:%s\n", argv[1] );
73
+ exit( 1 );
74
+ }
75
+
76
+ SHPGetInfo( old_SHP, &nEntities, &nShapeType, NULL, NULL );
77
+ for( i = 0; i < nEntities; i++ )
78
+ {
79
+ int res ;
80
+
81
+ psCShape = SHPReadObject( old_SHP, i );
82
+
83
+ if ( byRing == 1 ) {
84
+ int ring, prevStart, ringDir;
85
+ double ringArea;
86
+
87
+ prevStart = psCShape->nVertices;
88
+ for ( ring = (psCShape->nParts - 1); ring >= 0; ring-- ) {
89
+ SHPObject *psO;
90
+ int j, numVtx, rStart;
91
+
92
+ rStart = psCShape->panPartStart[ring];
93
+ if ( ring == (psCShape->nParts -1) )
94
+ { numVtx = psCShape->nVertices - rStart; }
95
+ else
96
+ { numVtx = psCShape->panPartStart[ring+1] - rStart; }
97
+
98
+ printf ("(shpdata) Ring(%d) (%d for %d) \n", ring, rStart, numVtx);
99
+ psO = SHPClone ( psCShape, ring, ring + 1 );
100
+
101
+ ringDir = SHPRingDir_2d ( psO, 0 );
102
+ ringArea = RingArea_2d (psO->nVertices,(double*) psO->padfX,
103
+ (double*) psO->padfY);
104
+ RingCentroid_2d ( psO->nVertices, (double*) psO->padfX,
105
+ (double*) psO->padfY, &ringCentrd, &ringArea);
106
+
107
+
108
+ printf ("(shpdata) Ring %d, %f Area %d dir \n",
109
+ ring, ringArea, ringDir );
110
+
111
+ SHPDestroyObject ( psO );
112
+ printf ("(shpdata) End Ring \n");
113
+ } /* (ring) [0,nParts */
114
+
115
+ } /* by ring */
116
+
117
+ oArea = SHPArea_2d ( psCShape );
118
+ oLen = SHPLength_2d ( psCShape );
119
+ oCentrd = SHPCentrd_2d ( psCShape );
120
+ printf ("(shpdata) Part (%d) %f Area %f length, C (%f,%f)\n",
121
+ i, oArea, oLen, oCentrd.x, oCentrd.y );
122
+ }
123
+
124
+ SHPClose( old_SHP );
125
+
126
+ DBFClose( old_DBF );
127
+
128
+ printf ("\n");
129
+ }
@@ -0,0 +1,340 @@
1
+ /******************************************************************************
2
+ * Copyright (c) 1999, Carl Anderson
3
+ *
4
+ * This code is based in part on the earlier work of Frank Warmerdam
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a
7
+ * copy of this software and associated documentation files (the "Software"),
8
+ * to deal in the Software without restriction, including without limitation
9
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
+ * and/or sell copies of the Software, and to permit persons to whom the
11
+ * Software is furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included
14
+ * in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
+ * DEALINGS IN THE SOFTWARE.
23
+ ******************************************************************************
24
+ *
25
+ * shp2dxf.c
26
+ *
27
+ * derived from a ESRI Avenue Script
28
+ * and DXF specification from AutoCad 3 (yes 1984)
29
+ *
30
+ * modifications Carl Andrson 11/96
31
+ * modifications Carl Andrson 3/97
32
+ *
33
+ * converted to C code 12/98
34
+ *
35
+ * requires shapelib 1.2
36
+ * gcc shpdxf.c shpopen.o dbfopen.o -o shpdxf
37
+ *
38
+ */
39
+
40
+ #include <stdlib.h>
41
+ #include <string.h>
42
+ #include "shapefil.h"
43
+
44
+ #define FLOAT_PREC "%16.5f\r\n"
45
+
46
+ void dxf_hdr (x1,y1,x2,y2,df)
47
+ double x1,y1,x2,y2;
48
+ FILE *df;
49
+ {
50
+ /* Create HEADER section */
51
+
52
+ fprintf( df, " 0\r\n");
53
+ fprintf( df, "SECTION\r\n");
54
+ fprintf( df, " 2\r\n" );
55
+ fprintf( df, "HEADER\r\n" );
56
+ fprintf( df, " 9\r\n" );
57
+ fprintf( df, "$EXTMAX\r\n" );
58
+ fprintf( df, " 10\r\n" );
59
+ fprintf( df, FLOAT_PREC, x2 );
60
+ fprintf( df, " 20\r\n" );
61
+ fprintf( df, FLOAT_PREC, y2 );
62
+ fprintf( df, " 9\r\n" );
63
+ fprintf( df, "$EXTMIN\r\n" );
64
+ fprintf( df, " 10\r\n" );
65
+ fprintf( df, FLOAT_PREC, x1 );
66
+ fprintf( df, " 20\r\n" );
67
+ fprintf( df, FLOAT_PREC, y1 );
68
+ fprintf( df, " 9\r\n" );
69
+ fprintf( df, "$LUPREC\r\n" );
70
+ fprintf( df, " 70\r\n" );
71
+ fprintf( df, " 14\r\n" );
72
+ fprintf( df, " 0\r\n" );
73
+ fprintf( df, "ENDSEC\r\n" );
74
+
75
+ /* ' Create TABLES section */
76
+
77
+ fprintf( df, " 0\r\n" );
78
+ fprintf( df, "SECTION\r\n" );
79
+ fprintf( df, " 2\r\n" );
80
+ fprintf( df, "TABLES\r\n" );
81
+ /* ' Table 1 - set up line type */
82
+ fprintf( df, " 0\r\n" );
83
+ fprintf( df, "TABLE\r\n" );
84
+ fprintf( df, " 2\r\n" );
85
+ fprintf( df, "LTYPE\r\n" );
86
+ fprintf( df, " 70\r\n" );
87
+ fprintf( df, "2\r\n" );
88
+ /* ' Entry 1 of Table 1 */
89
+ fprintf( df, " 0\r\n" );
90
+ fprintf( df, "LTYPE\r\n" );
91
+ fprintf( df, " 2\r\n" );
92
+ fprintf( df, "CONTINUOUS\r\n" );
93
+ fprintf( df, " 70\r\n" );
94
+ fprintf( df, "64\r\n" );
95
+ fprintf( df, " 3\r\n" );
96
+ fprintf( df, "Solid line\r\n" );
97
+ fprintf( df, " 72\r\n" );
98
+ fprintf( df, "65\r\n" );
99
+ fprintf( df, " 73\r\n" );
100
+ fprintf( df, "0\r\n" );
101
+ fprintf( df, " 40\r\n" );
102
+ fprintf( df, "0.0\r\n" );
103
+ fprintf( df, " 0\r\n" );
104
+ fprintf( df, "ENDTAB\r\n" );
105
+ /* End of TABLES section */
106
+ fprintf( df, " 0\r\n" );
107
+ fprintf( df, "ENDSEC\r\n" );
108
+
109
+ /* Create BLOCKS section */
110
+
111
+ fprintf( df, " 0\r\n" );
112
+ fprintf( df, "SECTION\r\n" );
113
+ fprintf( df, " 2\r\n" );
114
+ fprintf( df, "BLOCKS\r\n" );
115
+ fprintf( df, " 0\r\n" );
116
+ fprintf( df, "ENDSEC\r\n" );
117
+ fprintf( df, " 0\r\n" );
118
+ fprintf( df, "SECTION\r\n" );
119
+ fprintf( df, " 2\r\n" );
120
+ fprintf( df, "ENTITIES\r\n" );
121
+
122
+ }
123
+
124
+
125
+ void
126
+ dxf_ent_preamble (dxf_type, id, df)
127
+ int dxf_type;
128
+ char *id;
129
+ FILE *df;
130
+ {
131
+
132
+ fprintf( df, " 0\r\n" );
133
+
134
+ switch (dxf_type) {
135
+ case SHPT_POLYGON:
136
+ case SHPT_ARC: fprintf (df, "POLYLINE\r\n");
137
+ break;
138
+ default: fprintf(df, "POINT\r\n");
139
+ }
140
+
141
+ fprintf( df, " 8\r\n", df);
142
+ fprintf( df, "%s\r\n", id );
143
+ switch ( dxf_type ) {
144
+ case SHPT_ARC:
145
+ fprintf( df, " 6\r\n" );
146
+ fprintf( df, "CONTINUOUS\r\n" );
147
+ fprintf( df, " 66\r\n" );
148
+ fprintf( df, "1\r\n" );
149
+ break;
150
+ case SHPT_POLYGON:
151
+ fprintf( df, " 6\r\n" );
152
+ fprintf( df, "CONTINUOUS\r\n" );
153
+ fprintf( df, " 66\r\n" );
154
+ fprintf( df, "1\r\n" );
155
+ fprintf( df, " 70\r\n");
156
+ fprintf (df, "1\r\n");
157
+ default: break;
158
+ }
159
+
160
+ }
161
+
162
+ void
163
+ dxf_ent (id, x, y, z, dxf_type, df)
164
+ char *id;
165
+ double x,y,z;
166
+ int dxf_type;
167
+ FILE *df;
168
+ {
169
+ if ((dxf_type == SHPT_ARC) || ( dxf_type == SHPT_POLYGON)) {
170
+ fprintf( df, " 0\r\n");
171
+ fprintf( df, "VERTEX\r\n");
172
+ fprintf( df, " 8\r\n");
173
+ fprintf( df, "%s\r\n", id);
174
+ }
175
+ fprintf( df, " 10\r\n" );
176
+ fprintf( df, FLOAT_PREC, x );
177
+ fprintf( df, " 20\r\n" );
178
+ fprintf( df, FLOAT_PREC, y );
179
+ fprintf( df, " 30\r\n" );
180
+ if ( z != 0 )
181
+ fprintf( df, FLOAT_PREC, z );
182
+ else
183
+ fprintf( df, "0.0\r\n" );
184
+ }
185
+
186
+
187
+ void
188
+ dxf_ent_postamble (dxf_type, df)
189
+ int dxf_type;
190
+ FILE *df;
191
+ {
192
+ if ((dxf_type == SHPT_ARC) || ( dxf_type == SHPT_POLYGON))
193
+ fprintf( df, " 0\r\nSEQEND\r\n 8\r\n0\r\n", df);
194
+ }
195
+
196
+
197
+ int
198
+ main (int argc, char **argv)
199
+
200
+ {
201
+ char shpFileName[80] = "", dbfFileName[80] = "";
202
+ char dxfFileName[80] = "";
203
+ char idfldName[15];
204
+ char zfldName[6] = "ELEV";
205
+ char fldName[15];
206
+ char id[255];
207
+ double elev;
208
+ int parts, *panParts, nParts, nVertices;
209
+ FILE *dxf;
210
+ SHPHandle shp;
211
+ DBFHandle dbf;
212
+ DBFFieldType idfld_type;
213
+ double adfBoundsMin[4], adfBoundsMax[4];
214
+ int vrtx, shp_type, shp_numrec, zfld, idfld, nflds, recNum, part;
215
+ unsigned int MaxElem = -1;
216
+
217
+ if ( argc < 2 ) {
218
+ printf ("usage: shpdxf shapefile {idfield}\r\n");
219
+ exit (-1);
220
+ }
221
+
222
+ strcpy (shpFileName,argv[1]);
223
+ strncpy (dbfFileName, shpFileName, strlen(shpFileName)-3);
224
+ strcat (dbfFileName,"dbf");
225
+
226
+ strncpy (dxfFileName, shpFileName,strlen(shpFileName)-3);
227
+ strcat( dxfFileName, "dxf");
228
+
229
+ shp = SHPOpen (shpFileName, "rb");
230
+ dbf = DBFOpen (dbfFileName, "rb");
231
+ dxf = fopen( dxfFileName, "w");
232
+
233
+ printf("Starting conversion %s(%s) -> %s\r\n",
234
+ shpFileName,dbfFileName,dxfFileName);
235
+
236
+ SHPGetInfo (shp, &shp_numrec, &shp_type, adfBoundsMin, adfBoundsMax );
237
+ printf ("file has %d objects\r\n", shp_numrec);
238
+ dxf_hdr(adfBoundsMin[0], adfBoundsMin[1], adfBoundsMax[0], adfBoundsMax[1],
239
+ dxf);
240
+
241
+ /* Before proceeding, allow the user to specify the ID field to use or default to the record number.... */
242
+
243
+ if ( argc > 3 ) MaxElem = atoi(argv[3]);
244
+
245
+ nflds = DBFGetFieldCount(dbf);
246
+ if ( argc > 2 ) {
247
+ strcpy (idfldName, argv[2]);
248
+ for ( idfld=0; idfld < nflds; idfld++ ) {
249
+ idfld_type = DBFGetFieldInfo( dbf, idfld, fldName, NULL, NULL);
250
+ if (!strcmp (idfldName, fldName ))
251
+ break;
252
+ }
253
+ if ( idfld >= nflds ) {
254
+ printf ("Id field %s not found, using default\r\n",idfldName);
255
+ idfld = -1;
256
+ } else
257
+ printf ("proceeding with field %s for LayerNames\r\n",fldName);
258
+ }
259
+ else
260
+ idfld = -1;
261
+
262
+ for ( zfld=0; zfld < nflds; zfld++ ) {
263
+ DBFGetFieldInfo( dbf, zfld, fldName, NULL, NULL);
264
+ if (!strcmp (zfldName, fldName ))
265
+ break;
266
+ }
267
+ if ( zfld >= nflds )
268
+ zfld = -1;
269
+ // printf ("proceeding with id = %d, elevation = %d\r\n",idfld, zfld);
270
+
271
+ /* Proceed to process data..... */
272
+
273
+ for ( recNum = 0; (recNum < shp_numrec) && (recNum < MaxElem); recNum++) {
274
+
275
+ SHPObject *shape;
276
+
277
+ if ( idfld >= 0 )
278
+ switch (idfld_type) {
279
+ case FTString: sprintf (id, "lvl_%s",DBFReadStringAttribute ( dbf, recNum, idfld ));
280
+ break;
281
+ default: sprintf(id, "%-20.0lf", DBFReadDoubleAttribute (dbf, recNum, idfld));
282
+ }
283
+ else
284
+ sprintf (id,"lvl_%-20d",(recNum +1 ));
285
+
286
+
287
+ if ( zfld >= 0 )
288
+ elev = 0;
289
+ else
290
+ elev = DBFReadDoubleAttribute ( dbf, recNum, zfld );
291
+
292
+ #ifdef DEBUG
293
+ printf("\r\nworking on obj %d", recNum);
294
+ #endif
295
+
296
+ shape = SHPReadObject( shp, recNum );
297
+
298
+ nVertices = shape->nVertices;
299
+ nParts = shape->nParts;
300
+ panParts = shape->panPartStart;
301
+ part = 0;
302
+ for (vrtx=0; vrtx < nVertices; vrtx ++ ) {
303
+ #ifdef DEBUG
304
+ printf("\rworking on part %d, vertex %d", part,vrtx);
305
+ #endif
306
+ if ( panParts[part] == vrtx ) {
307
+ #ifdef DEBUG
308
+ printf ("object preamble\r\n");
309
+ #endif
310
+ dxf_ent_preamble (shp_type, id, dxf);
311
+ }
312
+
313
+ dxf_ent (id, shape->padfX[vrtx], shape->padfY[vrtx],
314
+ elev, shp_type, dxf);
315
+
316
+ if ((panParts[part] == (vrtx + 1))|| (vrtx == (nVertices -1)) ) {
317
+ dxf_ent_postamble (shp_type, dxf);
318
+ part ++;
319
+ }
320
+ }
321
+ SHPDestroyObject( shape );
322
+ }
323
+
324
+ /* close out DXF file */
325
+ fprintf( dxf, "0\r\n" );
326
+ fprintf( dxf, "ENDSEC\r\n" );
327
+ fprintf( dxf, "0\r\n" );
328
+ fprintf( dxf, "EOF\r\n" );
329
+
330
+
331
+ SHPClose (shp);
332
+ DBFClose (dbf);
333
+ fclose (dxf);
334
+ }
335
+
336
+
337
+
338
+
339
+
340
+