1 #include <grass/glocale.h>
2
3 #include "local_proto.h"
4
create_ogr_layer(const char * dsn,const char * format,const char * layer,unsigned int wkbtype,char ** papszDSCO,char ** papszLCO)5 void create_ogr_layer(const char *dsn, const char *format, const char *layer,
6 unsigned int wkbtype, char **papszDSCO,
7 char **papszLCO)
8 {
9 char *pszDriverName;
10 dr_t hDriver;
11 ds_t hDS;
12 OGRLayerH hLayer;
13
14 pszDriverName = G_store(format);
15 G_strchg(pszDriverName, '_', ' '); /* '_' -> ' ' */
16
17 /* start driver */
18 hDriver = get_driver_by_name(pszDriverName);
19 if (hDriver == NULL) {
20 G_fatal_error(_("OGR driver <%s> not available"), pszDriverName);
21 }
22
23 /* create datasource */
24 #if GDAL_VERSION_NUM >= 2020000
25 hDS = GDALCreate(hDriver, dsn, 0, 0, 0, GDT_Unknown, papszDSCO);
26 #else
27 hDS = OGR_Dr_CreateDataSource(hDriver, dsn, papszDSCO);
28 #endif
29 if (hDS == NULL) {
30 G_fatal_error(_("Creation of output OGR datasource <%s> failed"),
31 dsn);
32 }
33
34 G_free(pszDriverName);
35
36 /* create layer */
37 /* todo: SRS */
38 #if GDAL_VERSION_NUM >= 2020000
39 hLayer = GDALDatasetCreateLayer(hDS, layer, NULL, wkbtype, papszLCO);
40 #else
41 hLayer = OGR_DS_CreateLayer(hDS, layer, NULL, wkbtype, papszLCO);
42 #endif
43 if (hLayer == NULL) {
44 G_fatal_error(_("Creation of OGR layer <%s> failed"), layer);
45 }
46
47 ds_close(hDS);
48 }
49
get_multi_wkbtype(OGRwkbGeometryType wkbtype)50 OGRwkbGeometryType get_multi_wkbtype(OGRwkbGeometryType wkbtype)
51 {
52 OGRwkbGeometryType multiwkbtype;
53
54 switch (wkbtype) {
55 case wkbPoint:
56 multiwkbtype = wkbMultiPoint;
57 break;
58 case wkbLineString:
59 multiwkbtype = wkbMultiLineString;
60 break;
61 case wkbPolygon:
62 multiwkbtype = wkbMultiPolygon;
63 break;
64 default:
65 multiwkbtype = wkbGeometryCollection;
66 break;
67 }
68
69 return multiwkbtype;
70 }
71
get_wkbtype(int type,int otype)72 OGRwkbGeometryType get_wkbtype(int type, int otype)
73 {
74 if (type == GV_POINT || type == GV_KERNEL ||
75 (type == GV_CENTROID && otype & GV_CENTROID))
76 return wkbPoint;
77 else if (type & GV_LINES)
78 return wkbLineString;
79 else if (type == GV_FACE)
80 return wkbPolygon25D;
81
82 return wkbGeometryCollection;
83 }
84