3
3
namespace gis
4
4
{
5
5
6
- void bbox2geojson (double xmin, double ymin, double xmax, double ymax, const std::string& filepath)
6
+ void bbox2geojson (double xmin, double ymin, double xmax, double ymax, const std::string& filepath, const std::string& proj4 )
7
7
{
8
8
GDALAllRegister ();
9
9
const char *pszDriverName = " GeoJSON" ;
@@ -19,12 +19,22 @@ namespace gis
19
19
CHM_THROW_EXCEPTION (chm_error, " Creation of output file failed" );
20
20
}
21
21
22
- OGRLayer *poLayer = poDS->CreateLayer (" bbox" , nullptr , wkbPolygon, nullptr );
22
+ OGRSpatialReference oSRS;
23
+
24
+ if (oSRS.importFromProj4 (proj4.c_str ()) != OGRERR_NONE)
25
+ {
26
+ CHM_THROW_EXCEPTION (chm_error, " Failed to import PROJ.4 string" );
27
+ }
28
+
29
+ char **papszOptions = nullptr ;
30
+ papszOptions = CSLAddNameValue (papszOptions, " RFC7946" , " TRUE" );
31
+ OGRLayer *poLayer = poDS->CreateLayer (" bbox" , &oSRS, wkbPolygon, papszOptions);
23
32
if (poLayer == nullptr )
24
33
{
25
34
CHM_THROW_EXCEPTION (chm_error, " Layer creation failed" );
26
35
}
27
36
37
+
28
38
// Create a polygon from the bounding box
29
39
OGRPolygon polygon;
30
40
OGRLinearRing ring;
@@ -49,11 +59,13 @@ namespace gis
49
59
CHM_THROW_EXCEPTION (chm_error, " Failed to create feature in GeoJSON" );
50
60
}
51
61
62
+ CSLDestroy (papszOptions);
52
63
OGRFeature::DestroyFeature (poFeature);
64
+
53
65
GDALClose (poDS);
54
66
}
55
67
56
- void xy2geojson (std::vector<std::tuple<float , float >> xy, std::string filepath, std::string proj4)
68
+ void xy2geojson (std::vector<std::tuple<float , float >> xy, const std::string& filepath, const std::string& proj4)
57
69
{
58
70
GDALAllRegister ();
59
71
const char *pszDriverName = " GeoJSON" ;
@@ -69,23 +81,21 @@ namespace gis
69
81
CHM_THROW_EXCEPTION (chm_error, " Creation of output file failed" );
70
82
}
71
83
72
- OGRLayer *poLayer = poDS->CreateLayer (" layer" , nullptr , wkbPoint, nullptr );
84
+ OGRSpatialReference oSRS;
85
+
86
+ if (oSRS.importFromProj4 (proj4.c_str ()) != OGRERR_NONE)
87
+ {
88
+ CHM_THROW_EXCEPTION (chm_error, " Failed to import PROJ.4 string" );
89
+ }
90
+ char **papszOptions = nullptr ;
91
+ papszOptions = CSLAddNameValue (papszOptions, " RFC7946" , " TRUE" );
92
+
93
+ OGRLayer *poLayer = poDS->CreateLayer (" layer" , &oSRS, wkbPoint, papszOptions);
73
94
if (poLayer == nullptr )
74
95
{
75
96
CHM_THROW_EXCEPTION (chm_error, " Layer creation failed" );
76
97
}
77
98
78
- // OGRSpatialReference oSRS;
79
-
80
- // if (oSRS.importFromProj4(proj4.c_str()) != OGRERR_NONE)
81
- // {
82
- // CHM_THROW_EXCEPTION(chm_error, "Failed to import PROJ.4 string");
83
- // }
84
- // if (poDS->SetProjection(proj4.c_str()) != CE_None)
85
- // {
86
- // CHM_THROW_EXCEPTION(chm_error, "Failed to set spatial reference");
87
- // }
88
-
89
99
OGRFieldDefn oFieldX (" X" , OFTReal);
90
100
oFieldX.SetWidth (32 );
91
101
poLayer->CreateField (&oFieldX);
@@ -121,6 +131,7 @@ namespace gis
121
131
122
132
OGRFeature::DestroyFeature (poFeature);
123
133
}
134
+ CSLDestroy (papszOptions);
124
135
GDALClose (poDS);
125
136
}
126
137
0 commit comments