1 /**********************************************************************
2  * $Id: ogr_capi_test.c 10645 2007-01-18 02:22:39Z warmerdam $
3  **********************************************************************
4  * Copyright (c) 2003, Daniel Morissette
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  *
26  * Test the OGR C API (ogr_api.h)
27  *
28  * Compile using:
29  *
30  * gcc -g ogr_capi_test.c `gdal-config --libs` `gdal-config --cflags` \
31  *     -o ogr_capi_test
32  *
33  ****************************************************************************/
34 
35 #include "ogr_api.h"
36 
37 int OGRCDump(const char *pszFname);
38 int OGRCCreate(const char *pszFname);
39 
40 /**********************************************************************
41  *                      main()
42  **********************************************************************/
main(int argc,char * argv[])43 int main(int argc, char *argv[])
44 {
45 
46     if (argc == 3 && EQUAL(argv[1], "dump"))
47     {
48         return OGRCDump(argv[2]);
49     }
50     else if (argc == 3 && EQUAL(argv[1], "create"))
51     {
52         return OGRCCreate(argv[2]);
53     }
54     else
55     {
56         printf("Usage: ogr_capi_test <command> <filename>\n");
57     }
58 
59     return 0;
60 }
61 
62 /**********************************************************************
63  *                      OGRCDump()
64  *
65  * Open a dataset using OGR and dump all its layers.
66  *
67  **********************************************************************/
68 
OGRCDump(const char * pszFname)69 int OGRCDump(const char *pszFname)
70 {
71     OGRDataSourceH datasource;
72     int i, numLayers;
73 
74     /* Register all OGR drivers */
75     OGRRegisterAll();
76 
77     /* Open data source */
78     datasource = OGROpen(pszFname, 0 /* bUpdate */, NULL);
79 
80     if (datasource == NULL)
81     {
82         printf("Unable to open %s\n", pszFname);
83         return -1;
84     }
85 
86     /* Loop through layers and dump their contents */
87 
88     numLayers = OGR_DS_GetLayerCount(datasource);
89     for(i=0; i<numLayers; i++)
90     {
91         OGRLayerH layer;
92         int j, numFields;
93         OGRFeatureH feature;
94         OGRFeatureDefnH layerDefn;
95 
96         layer = OGR_DS_GetLayer( datasource, i );
97 
98         /* Dump info about this layer */
99         layerDefn = OGR_L_GetLayerDefn( layer );
100         numFields = OGR_FD_GetFieldCount( layerDefn );
101 
102         printf("\n===================\n");
103         printf("Layer %d: '%s'\n\n", i, OGR_FD_GetName(layerDefn));
104 
105         for(j=0; j<numFields; j++)
106         {
107             OGRFieldDefnH fieldDefn;
108 
109             fieldDefn = OGR_FD_GetFieldDefn( layerDefn, j );
110             printf(" Field %d: %s (%s)\n",
111                    j, OGR_Fld_GetNameRef(fieldDefn),
112                    OGR_GetFieldTypeName(OGR_Fld_GetType(fieldDefn)) );
113         }
114         printf("\n");
115 
116         /* And dump each feature individually */
117         while( (feature = OGR_L_GetNextFeature( layer )) != NULL )
118         {
119             OGR_F_DumpReadable( feature, stdout );
120             OGR_F_Destroy( feature );
121         }
122 
123         /* No need to free layer handle, it belongs to the datasource */
124     }
125 
126     /* Close data source */
127     OGR_DS_Destroy( datasource );
128 
129     return 0;
130 }
131 
132 /**********************************************************************
133  *                      OGRCCreate()
134  *
135  * Create a new dataset using OGR C API with a few features in it.
136  *
137  **********************************************************************/
138 
OGRCCreate(const char * pszFname)139 int OGRCCreate(const char *pszFname)
140 {
141     OGRSFDriverH    driver;
142     int             i, numDrivers;
143     OGRDataSourceH  datasource;
144     OGRLayerH       layer;
145     OGRFeatureDefnH layerDefn;
146     OGRFieldDefnH   fieldDefn;
147     OGRFeatureH     feature;
148     OGRGeometryH    geometry, ring;
149 
150     /* Register all OGR drivers */
151     OGRRegisterAll();
152 
153     /* Fetch MITAB driver - we want to create a TAB file */
154     numDrivers = OGRGetDriverCount();
155     for(i=0; i<numDrivers; i++)
156     {
157         driver = OGRGetDriver(i);
158         if (EQUAL("MapInfo File", OGR_Dr_GetName(driver)))
159             break;  /* Found it! */
160         driver = NULL;
161     }
162 
163     if (!driver)
164     {
165         printf("Driver not found!\n");
166         return -1;
167     }
168 
169     /* Create new file using this driver */
170     datasource = OGR_Dr_CreateDataSource(driver, pszFname, NULL);
171 
172     if (datasource == NULL)
173     {
174         printf("Unable to create %s\n", pszFname);
175         return -1;
176     }
177 
178     /* MapInfo data sources are created with one empty layer.
179        Fetch the layer handle */
180     layer = OGR_DS_GetLayer(datasource, 0);
181 
182     if (layer == NULL)
183     {
184         printf("Unable to create new layer in %s\n", pszFname);
185         return -1;
186     }
187 
188     /* Add a few fields to the layer defn */
189     fieldDefn = OGR_Fld_Create( "id", OFTInteger );
190     OGR_L_CreateField(layer, fieldDefn, 0);
191 
192     fieldDefn = OGR_Fld_Create( "area", OFTReal );
193     OGR_L_CreateField(layer, fieldDefn, 0);
194 
195     fieldDefn = OGR_Fld_Create( "name", OFTString );
196     OGR_L_CreateField(layer, fieldDefn, 0);
197 
198     /* We'll need the layerDefn handle to create new features in this layer */
199     layerDefn = OGR_L_GetLayerDefn( layer );
200 
201     /* Create a new point */
202     feature = OGR_F_Create( layerDefn );
203     OGR_F_SetFieldInteger( feature, 0, 1);
204     OGR_F_SetFieldDouble( feature, 1, 123.45);
205     OGR_F_SetFieldString( feature, 2, "Feature #1");
206 
207     geometry = OGR_G_CreateGeometry( wkbPoint );
208     OGR_G_SetPoint(geometry, 0, 123.45, 456.78, 0);
209 
210     OGR_F_SetGeometryDirectly(feature, geometry);
211 
212     OGR_L_CreateFeature( layer, feature );
213 
214     /* Create a new line */
215     feature = OGR_F_Create( layerDefn );
216     OGR_F_SetFieldInteger( feature, 0, 2);
217     OGR_F_SetFieldDouble( feature, 1, 42.45);
218     OGR_F_SetFieldString( feature, 2, "Feature #2");
219 
220     geometry = OGR_G_CreateGeometry( wkbLineString );
221     OGR_G_AddPoint(geometry, 123.45, 456.78, 0);
222     OGR_G_AddPoint(geometry, 12.34,  45.67, 0);
223 
224     OGR_F_SetGeometryDirectly(feature, geometry);
225 
226     OGR_L_CreateFeature( layer, feature );
227 
228     /* Create a new polygon (square) */
229     feature = OGR_F_Create( layerDefn );
230     OGR_F_SetFieldInteger( feature, 0, 3);
231     OGR_F_SetFieldDouble( feature, 1, 49.71);
232     OGR_F_SetFieldString( feature, 2, "Feature #3");
233 
234     geometry = OGR_G_CreateGeometry( wkbPolygon );
235     ring = OGR_G_CreateGeometry( wkbLinearRing );
236     OGR_G_AddPoint(ring, 123.45, 456.78, 0);
237     OGR_G_AddPoint(ring, 12.34,  456.78, 0);
238     OGR_G_AddPoint(ring, 12.34,  45.67, 0);
239     OGR_G_AddPoint(ring, 123.45, 45.67, 0);
240     OGR_G_AddPoint(ring, 123.45, 456.78, 0);
241     OGR_G_AddGeometryDirectly(geometry, ring);
242 
243     OGR_F_SetGeometryDirectly(feature, geometry);
244 
245     OGR_L_CreateFeature( layer, feature );
246 
247     /* Close data source */
248     OGR_DS_Destroy( datasource );
249 
250     return 0;
251 }
252