-
Notifications
You must be signed in to change notification settings - Fork 429
/
GeoWaveReader.cpp
390 lines (331 loc) · 15.6 KB
/
GeoWaveReader.cpp
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/******************************************************************************
* Copyright (c) 2015, James W. O'Meara (james.w.omeara@gmail.com)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/
#include "GeoWaveReader.hpp"
#include <pdal/pdal_macros.hpp>
#include <pdal/util/ProgramArgs.hpp>
#include <jace/Jace.h>
using jace::java_cast;
using jace::java_new;
#include "jace/JNIException.h"
using jace::JNIException;
#include "jace/VirtualMachineShutdownError.h"
using jace::VirtualMachineShutdownError;
#include "jace/OptionList.h"
using jace::OptionList;
using jace::Option;
using jace::ClassPath;
using jace::Verbose;
using jace::CustomOption;
#include <jace/StaticVmLoader.h>
using jace::StaticVmLoader;
#ifdef _WIN32
#include "jace/Win32VmLoader.h"
using jace::Win32VmLoader;
#else
#include "jace/UnixVmLoader.h"
using ::jace::UnixVmLoader;
#endif
#include "jace/JArray.h"
using jace::JArray;
#include "jace/proxy/types/JBoolean.h"
using jace::proxy::types::JBoolean;
#include "jace/proxy/types/JDouble.h"
using jace::proxy::types::JDouble;
#include "jace/proxy/types/JInt.h"
using jace::proxy::types::JInt;
#include "jace/proxy/java/lang/Double.h"
using jace::proxy::java::lang::Double;
#include "jace/proxy/java/lang/String.h"
using jace::proxy::java::lang::String;
#include "jace/proxy/java/util/List.h"
using jace::proxy::java::util::List;
#include "jace/proxy/org/geotools/data/simple/SimpleFeatureCollection.h"
using jace::proxy::org::geotools::data::simple::SimpleFeatureCollection;
#include "jace/proxy/org/geotools/data/simple/SimpleFeatureIterator.h"
using jace::proxy::org::geotools::data::simple::SimpleFeatureIterator;
#include "jace/proxy/com/vividsolutions/jts/geom/Polygon.h"
using jace::proxy::com::vividsolutions::jts::geom::Polygon;
#include "jace/proxy/com/vividsolutions/jts/geom/Coordinate.h"
using jace::proxy::com::vividsolutions::jts::geom::Coordinate;
#include "jace/proxy/com/vividsolutions/jts/geom/GeometryFactory.h"
using jace::proxy::com::vividsolutions::jts::geom::GeometryFactory;
#include "jace/proxy/com/vividsolutions/jts/geom/Point.h"
using jace::proxy::com::vividsolutions::jts::geom::Point;
#include "jace/proxy/org/apache/accumulo/core/client/AccumuloException.h"
using jace::proxy::org::apache::accumulo::core::client::AccumuloException;
#include "jace/proxy/org/apache/accumulo/core/client/AccumuloSecurityException.h"
using jace::proxy::org::apache::accumulo::core::client::AccumuloSecurityException;
#include "jace/proxy/org/opengis/feature/simple/SimpleFeature.h"
using jace::proxy::org::opengis::feature::simple::SimpleFeature;
#include "jace/proxy/org/opengis/feature/simple/SimpleFeatureType.h"
using jace::proxy::org::opengis::feature::simple::SimpleFeatureType;
#include "jace/proxy/org/opengis/feature/type/AttributeDescriptor.h"
using jace::proxy::org::opengis::feature::type::AttributeDescriptor;
#include "jace/proxy/mil/nga/giat/geowave/core/index/ByteArrayId.h"
using jace::proxy::mil::nga::giat::geowave::core::index::ByteArrayId;
#include "jace/proxy/mil/nga/giat/geowave/adapter/vector/FeatureDataAdapter.h"
using jace::proxy::mil::nga::giat::geowave::adapter::vector::FeatureDataAdapter;
#include "jace/proxy/mil/nga/giat/geowave/adapter/vector/FeatureCollectionDataAdapter.h"
using jace::proxy::mil::nga::giat::geowave::adapter::vector::FeatureCollectionDataAdapter;
#include "jace/proxy/mil/nga/giat/geowave/core/store/index/Index.h"
using jace::proxy::mil::nga::giat::geowave::core::store::index::Index;
#include "jace/proxy/mil/nga/giat/geowave/core/geotime/IndexType_JaceIndexType.h"
using jace::proxy::mil::nga::giat::geowave::core::geotime::IndexType_JaceIndexType;
#include "jace/proxy/mil/nga/giat/geowave/core/store/query/Query.h"
using jace::proxy::mil::nga::giat::geowave::core::store::query::Query;
#include "jace/proxy/mil/nga/giat/geowave/core/geotime/store/query/SpatialQuery.h"
using jace::proxy::mil::nga::giat::geowave::core::geotime::store::query::SpatialQuery;
#include "jace/proxy/mil/nga/giat/geowave/datastore/accumulo/BasicAccumuloOperations.h"
using jace::proxy::mil::nga::giat::geowave::datastore::accumulo::BasicAccumuloOperations;
#include "jace/proxy/mil/nga/giat/geowave/datastore/accumulo/AccumuloDataStore.h"
using jace::proxy::mil::nga::giat::geowave::datastore::accumulo::AccumuloDataStore;
#include "jace/proxy/mil/nga/giat/geowave/datastore/accumulo/metadata/AccumuloAdapterStore.h"
using jace::proxy::mil::nga::giat::geowave::datastore::accumulo::metadata::AccumuloAdapterStore;
namespace pdal
{
static PluginInfo const s_info = PluginInfo(
"readers.geowave",
"\"GeoWave\" reader support. ",
"http://pdal.io/stages/drivers.geowave.reader.html" );
CREATE_SHARED_PLUGIN(1, 0, GeoWaveReader, Reader, s_info)
std::string pdal::GeoWaveReader::getName() const { return s_info.name; }
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
void GeoWaveReader::addArgs(ProgramArgs& args)
{
args.add("zookeeper_url", "The comma-delimited URLs for all "
"zookeeper servers, this will be directly used to instantiate "
"a ZookeeperInstance", m_zookeeperUrl).setPositional();
args.add("instance_name", "The zookeeper instance name, this "
"will be directly used to instantiate a ZookeeperInstance",
m_instanceName).setPositional();
args.add("username", "The username for the account to establish "
"an Accumulo connector", m_username).setPositional();
args.add("password", "The password for the account to establish "
"an Accumulo connector", m_password).setPositional();
args.add("table_namespace", "The table name to be used when "
"interacting with GeoWave", m_tableNamespace).setPositional();
args.add("feature_type_name", "The feature type name to be used "
"when interacting with GeoWave", m_featureTypeName, "PDAL_Point");
args.add("data_adapter", "FeatureCollectionDataAdapter stores "
"multiple points per Accumulo entry. FeatureDataAdapter stores "
"a single point per Accumulo entry.", m_dataAdapter,
"FeatureCollectionDataAdapter");
args.add("points_per_entry", "The maximum number of points per "
"Accumulo entry when using FeatureCollectionDataAdapter.",
m_pointsPerEntry, 5000U);
args.add("bounds", "The extent of the bounding rectangle to use "
"to query points, expressed as a string, eg: "
"([xmin, xmax], [ymin, ymax], [zmin, zmax])", m_bounds);
}
void GeoWaveReader::initialize()
{
m_useFeatCollDataAdapter = (m_dataAdapter != "FeatureDataAdapter");
if (!jace::isRunning())
{
int status = createJvm();
if (status == 0)
log()->get(LogLevel::Debug) << "JVM Creation Successful" <<
std::endl;
else
log()->get(LogLevel::Error) << "JVM Creation Failed: "
"Error [" << status << "]" << std::endl;
}
}
void GeoWaveReader::addDimensions(PointLayoutPtr layout)
{
layout->registerDims(getDefaultDimensions());
BasicAccumuloOperations accumuloOperations;
try
{
accumuloOperations = java_new<BasicAccumuloOperations>(
java_new<String>(m_zookeeperUrl),
java_new<String>(m_instanceName),
java_new<String>(m_username),
java_new<String>(m_password),
java_new<String>(m_tableNamespace));
}
catch (AccumuloException& e)
{
log()->get(LogLevel::Error) << "There was a problem establishing a connector. " << e;
return;
}
catch (AccumuloSecurityException& e)
{
log()->get(LogLevel::Error) << "The credentials passed are invalid. " << e;
return;
}
AccumuloAdapterStore accumuloAdapterStore = java_new<AccumuloAdapterStore>(accumuloOperations);
List attribs;
if (m_useFeatCollDataAdapter)
attribs = java_cast<FeatureCollectionDataAdapter>(accumuloAdapterStore.getAdapter(java_new<ByteArrayId>(std::to_string(m_pointsPerEntry) + m_featureTypeName))).getType().getAttributeDescriptors();
else
attribs = java_cast<FeatureDataAdapter>(accumuloAdapterStore.getAdapter(java_new<ByteArrayId>(m_featureTypeName))).getType().getAttributeDescriptors();
for (int i = 0; i < attribs.size(); ++i){
std::string name = java_cast<AttributeDescriptor>(attribs.get(i)).getLocalName();
if (name.compare("location") != 0 && name.compare("X") != 0 && name.compare("Y") != 0)
layout->registerDim(Dimension::id(name));
}
}
Dimension::IdList GeoWaveReader::getDefaultDimensions()
{
Dimension::IdList ids;
ids.push_back(Dimension::Id::X);
ids.push_back(Dimension::Id::Y);
return ids;
}
void GeoWaveReader::ready(PointTableRef table)
{
if (m_bounds.empty())
return;
BasicAccumuloOperations accumuloOperations;
try
{
accumuloOperations = java_new<BasicAccumuloOperations>(
java_new<String>(m_zookeeperUrl),
java_new<String>(m_instanceName),
java_new<String>(m_username),
java_new<String>(m_password),
java_new<String>(m_tableNamespace));
}
catch (AccumuloException& e)
{
log()->get(LogLevel::Error) << "There was a problem establishing a connector. " << e;
return;
}
catch (AccumuloSecurityException& e)
{
log()->get(LogLevel::Error) << "The credentials passed are invalid. " << e;
return;
}
AccumuloDataStore accumuloDataStore = java_new<AccumuloDataStore>(
accumuloOperations);
Index index = IndexType_JaceIndexType::createSpatialVectorIndex();
GeometryFactory factory = java_new<GeometryFactory>();
JDouble lonMin = m_bounds.minx;
JDouble lonMax = m_bounds.maxx;
JDouble latMin = m_bounds.miny;
JDouble latMax = m_bounds.maxy;
JArray<Coordinate> coordArray(5);
coordArray[0] = java_new<Coordinate>(lonMin, latMin);
coordArray[1] = java_new<Coordinate>(lonMax, latMin);
coordArray[2] = java_new<Coordinate>(lonMax, latMax);
coordArray[3] = java_new<Coordinate>(lonMin, latMax);
coordArray[4] = java_new<Coordinate>(lonMin, latMin);
Polygon geom = factory.createPolygon(coordArray);
Query query = java_new<SpatialQuery>(geom);
JInt count = m_count;
m_iterator = accumuloDataStore.query(index, query, count);
}
point_count_t GeoWaveReader::read(PointViewPtr view, point_count_t count)
{
using namespace Dimension;
String location = java_new<String>("location");
point_count_t numRead = 0;
if (m_useFeatCollDataAdapter)
{
while (m_iterator.hasNext() && count > 0)
{
SimpleFeatureCollection featureCollection = java_cast<SimpleFeatureCollection>(m_iterator.next());
SimpleFeatureIterator featItr = featureCollection.features();
while (featItr.hasNext() && count-- > 0)
{
SimpleFeature simpleFeature = java_cast<SimpleFeature>(featItr.next());
List attribs = simpleFeature.getType().getAttributeDescriptors();
for (int i = 0; i < attribs.size(); ++i){
String name = java_cast<AttributeDescriptor>(attribs.get(i)).getLocalName();
if (!name.equals(location))
view->setField(id(name), numRead, java_cast<Double>(simpleFeature.getAttribute(name)).doubleValue());
}
if (m_cb)
m_cb(*view, numRead);
++numRead;
}
featItr.close();
}
}
else
{
while (m_iterator.hasNext() && count-- > 0){
SimpleFeature simpleFeature = java_cast<SimpleFeature>(m_iterator.next());
List attribs = simpleFeature.getType().getAttributeDescriptors();
for (int i = 0; i < attribs.size(); ++i){
String name = java_cast<AttributeDescriptor>(attribs.get(i)).getLocalName();
if (!name.equals(location))
view->setField(id(name), numRead, java_cast<Double>(simpleFeature.getAttribute(name)).doubleValue());
}
if (m_cb)
m_cb(*view, numRead);
++numRead;
}
}
return numRead;
}
void GeoWaveReader::done(PointTableRef table)
{
m_iterator.close();
}
int GeoWaveReader::createJvm()
{
try
{
StaticVmLoader loader(JNI_VERSION_1_2);
std::string geowaveClasspath = TOSTRING(GEOWAVE_RUNTIME_JAR);
OptionList options;
//options.push_back(CustomOption("-Xdebug"));
//options.push_back(CustomOption("-Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=y"));
//options.push_back(CustomOption("-Xcheck:jni"));
//options.push_back(Verbose (Verbose::JNI));
//options.push_back(Verbose (Verbose::CLASS));
options.push_back(ClassPath(geowaveClasspath));
jace::createVm(loader, options);
}
catch (VirtualMachineShutdownError&)
{
log()->get(LogLevel::Error) << "The JVM was terminated in mid-execution. " << std::endl;
return -1;
}
catch (JNIException& jniException)
{
log()->get(LogLevel::Error) << "An unexpected JNI error has occured: " << jniException.what() << std::endl;
return -2;
}
catch (std::exception& e)
{
log()->get(LogLevel::Error) << "An unexpected C++ error has occurred: " << e.what() << std::endl;
return -3;
}
return 0;
}
} // namespace pdal