/
_srs.cc
214 lines (179 loc) · 6.95 KB
/
_srs.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// v8
#include <v8.h>
// node
#include <node.h>
#include <node_version.h>
// stl
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
// osr
#include "ogr_spatialref.h"
//#include "ogr_core.h"
//#include "cpl_conv.h"
#include "cpl_string.h"
//#include "ogr_p.h"
//#include "cpl_multiproc.h"
//#include "ogr_srs_api.h"
using namespace node;
using namespace v8;
#define TOSTR(obj) (*String::Utf8Value((obj)->ToString()))
/*
OGRERR_DICT = { 1 : (OGRException, "Not enough data."),
2 : (OGRException, "Not enough memory."),
3 : (OGRException, "Unsupported geometry type."),
4 : (OGRException, "Unsupported operation."),
5 : (OGRException, "Corrupt data."),
6 : (OGRException, "OGR failure."),
7 : (SRSException, "Unsupported SRS."),
8 : (OGRException, "Invalid handle."),
}
*/
static Handle<Value> parse(const Arguments& args)
{
HandleScope scope;
if (args.Length() != 1 || !args[0]->IsString())
return ThrowException(Exception::TypeError(
String::New("first argument must be srs string in any form readable by OGR, like WKT (.prj file) or a proj4 string")));
OGRSpatialReference oSRS;
Local<Object> result = Object::New();
result->Set(String::NewSymbol("input"), args[0]->ToString());
// intialize as undefined
result->Set(String::NewSymbol("proj4"), Undefined());
result->Set(String::NewSymbol("srid"), Undefined());
result->Set(String::NewSymbol("auth"), Undefined());
result->Set(String::NewSymbol("pretty_wkt"), Undefined());
result->Set(String::NewSymbol("esri"), Undefined());
result->Set(String::NewSymbol("name"), Undefined());
std::string wkt_string = TOSTR(args[0]->ToString());
//std::string wkt_string = std::string("ESRI::") + TOSTR(args[0]->ToString());
const char *wkt_char = wkt_string.data();
bool error = false;
Handle<Value> err;
if( oSRS.SetFromUserInput(wkt_char) != OGRERR_NONE )
{
error = true;
std::ostringstream s;
s << "OGR Error type #" << CPLE_AppDefined
<< " problem occured importing from srs wkt: " << wkt_string << ".\n";;
err = ThrowException(Exception::TypeError(String::New(s.str().c_str())));
// try again to import from ESRI
oSRS.Clear();
char **wkt_lines = NULL;
wkt_lines = CSLTokenizeString2( wkt_char, " \t\n",
CSLT_HONOURSTRINGS | CSLT_ALLOWEMPTYTOKENS );
if( oSRS.importFromESRI(wkt_lines) != OGRERR_NONE )
{
error = true;
oSRS.Clear();
//std::ostringstream s;
//s << "b: OGR Error type #" << CPLE_AppDefined
// << " problem occured importing assuming esri wkt " << wkt_string << ".\n";
//err = ThrowException(Exception::TypeError(String::New(s.str().c_str())));
}
else
{
error = false;
std::clog << "imported assuming esri format...\n";
result->Set(String::NewSymbol("esri"), Boolean::New(true));
}
}
else
{
error = false;
result->Set(String::NewSymbol("esri"), Boolean::New(false));
}
if (error)
return err;
char *srs_output = NULL;
if( oSRS.Validate() == OGRERR_NONE)
result->Set(String::NewSymbol("valid"), Boolean::New(true));
else if (oSRS.Validate() == OGRERR_UNSUPPORTED_SRS)
result->Set(String::NewSymbol("valid"), Boolean::New(false));
else
result->Set(String::NewSymbol("valid"), Boolean::New(false));
// TODO - trim output of proj4 result
if (oSRS.exportToProj4( &srs_output ) != OGRERR_NONE )
{
std::ostringstream s;
s << "OGR Error type #" << CPLE_AppDefined
<< " problem occured when converting to proj4 format " << wkt_string << ".\n";
// for now let proj4 errors be non-fatal so that some info can be known...
//std::clog << s.str();
//return ThrowException(Exception::TypeError(String::New(s.str().c_str())));
}
else
{
// proj4 strings from osr have an uneeded trailing slash, so we trim it...
result->Set(String::NewSymbol("proj4"), String::New(CPLString(srs_output).Trim()));
}
CPLFree( srs_output );
if (oSRS.AutoIdentifyEPSG() != OGRERR_NONE )
{
/*std::ostringstream s;
s << "OGR Error type #" << CPLE_AppDefined
<< " problem occured when attempting to auto identify epsg code " << wkt_string << ".\n";
std::clog << s.str();
*/
//return ThrowException(Exception::TypeError(String::New(s.str().c_str())));
}
if (oSRS.IsGeographic())
{
result->Set(String::NewSymbol("is_geographic"), Boolean::New(true));
const char *code = oSRS.GetAuthorityCode("GEOGCS");
if (code)
result->Set(String::NewSymbol("srid"), Integer::New(atoi(code)));
const char *auth = oSRS.GetAuthorityName("GEOGCS");
if (auth)
result->Set(String::NewSymbol("auth"), String::New(auth));
const char *name = oSRS.GetAttrValue("GEOGCS");
if (name)
result->Set(String::NewSymbol("name"), String::New(name));
}
else
{
result->Set(String::NewSymbol("is_geographic"), Boolean::New(false));
const char *code = oSRS.GetAuthorityCode("PROJCS");
if (code)
result->Set(String::NewSymbol("srid"), Integer::New(atoi(code)));
const char *auth = oSRS.GetAuthorityName("PROJCS");
if (auth)
result->Set(String::NewSymbol("auth"), String::New(auth));
const char *name = oSRS.GetAttrValue("PROJCS");
if (name)
result->Set(String::NewSymbol("name"), String::New(name));
}
char *srs_output2 = NULL;
if (oSRS.exportToPrettyWkt( &srs_output2 , 0) != OGRERR_NONE )
{
// this does not yet actually return errors
std::ostringstream s;
s << "OGR Error type #" << CPLE_AppDefined
<< " problem occured when converting to pretty wkt format " << wkt_string << ".\n";
//std::clog << s.str();
//return ThrowException(Exception::TypeError(String::New(s.str().c_str())));
}
else
{
result->Set(String::NewSymbol("pretty_wkt"), String::New(srs_output2));
}
CPLFree( srs_output2 );
//OGRSpatialReference::DestroySpatialReference( &oSRS );
return scope.Close(result);
}
extern "C" {
static void init (Handle<Object> target)
{
// node-srs version
target->Set(String::NewSymbol("version"), String::New("0.2.18"));
NODE_SET_METHOD(target, "_parse", parse);
// versions of deps
Local<Object> versions = Object::New();
versions->Set(String::NewSymbol("node"), String::New(NODE_VERSION+1));
versions->Set(String::NewSymbol("v8"), String::New(V8::GetVersion()));
// ogr/osr ?
target->Set(String::NewSymbol("versions"), versions);
}
NODE_MODULE(_srs, init);
}