Permalink
Browse files

close [MW-1341] [SQLED] TABLE DATA EXPORT WIZARD EXPORTING LOCATION D…

…ATA FAILS

backend: spatial_handler: add missing d-tor to free OGRGeometry
backend: db_query_Resultset: add 4 functions to export geometry data as WKT and GeoJson
python: plugins: import/export: handle geojson and wkt data

review by Mike [revid: 11167]
  • Loading branch information...
1 parent 97ba33e commit f64894fc90eaadaef8707658145fb779c0e07b80 Marcin Szalowicz committed Nov 30, 2015
@@ -445,6 +445,12 @@ spatial::Importer::Importer() : _geometry(NULL), _interrupt(false)
{
}
+spatial::Importer::~Importer()
+{
+ if (_geometry != NULL)
+ OGRFree(_geometry);
+}
+
OGRGeometry *spatial::Importer::steal_data()
{
OGRGeometry *tmp = _geometry;
@@ -139,6 +139,7 @@ namespace spatial
void extract_points(OGRGeometry *shape, std::deque<ShapeContainer> &shapes_container);
public:
Importer();
+ ~Importer();
int import_from_mysql(const std::string &data);
int import_from_wkt(std::string data);
void get_points(std::deque<ShapeContainer> &shapes_container);
@@ -17,6 +17,10 @@
* 02110-1301 USA
*/
+#include <gdal/ogrsf_frmts.h>
+#include <gdal/ogr_api.h>
+#include <gdal/gdal.h>
+
#include <grts/structs.db.query.h>
#include <grtpp_util.h>
#include "sqlide/recordset_be.h"
@@ -40,6 +44,38 @@ db_query_Resultset::ImplData::~ImplData()
//================================================================================
+static grt::StringRef getGeoRepresentation(const std::string &data, bool outputAsJson = false)
+{
+ OGRGeometry *geometry = NULL;
+ OGRErr ret_val = OGRGeometryFactory::createFromWkb((unsigned char*)const_cast<char*>(&(*(data.begin() + 4))), NULL, &geometry);
+ if (ret_val != OGRERR_NONE)
+ {
+ if (geometry)
+ OGRFree(geometry);
+ throw std::exception();
+ }
+
+ if (geometry != NULL)
+ {
+ char *data = NULL;
+ OGRErr err = OGRERR_NONE;
+ if (outputAsJson)
+ data = geometry->exportToJson();
+ else
+ err = geometry->exportToWkt(&data);
+
+ if (err == OGRERR_NONE && data != NULL)
+ {
+ grt::StringRef tmp(data);
+ OGRFree(data);
+ OGRFree(geometry);
+ return tmp;
+ }
+ else
+ throw std::runtime_error("Conversion of OGR geometry data failed");
+ }
+ return grt::StringRef();
+}
WBRecordsetResultset::WBRecordsetResultset(db_query_ResultsetRef aself, boost::shared_ptr<Recordset> rset)
: db_query_Resultset::ImplData(aself), cursor(0), recordset(rset)
@@ -222,6 +258,26 @@ grt::StringRef WBRecordsetResultset::stringFieldValueByName(const std::string &c
return grt::StringRef(); // NULL
}
+grt::StringRef WBRecordsetResultset::geoStringFieldValue(ssize_t column)
+{
+ return getGeoRepresentation(stringFieldValue(column).c_str(), false);
+}
+
+grt::StringRef WBRecordsetResultset::geoStringFieldValueByName(const std::string &column)
+{
+ return getGeoRepresentation(stringFieldValueByName(column).c_str(), false);
+}
+
+grt::StringRef WBRecordsetResultset::geoJsonFieldValue(ssize_t column)
+{
+ return getGeoRepresentation(stringFieldValue(column).c_str(), false);
+}
+
+grt::StringRef WBRecordsetResultset::geoJsonFieldValueByName(const std::string &column)
+{
+ return getGeoRepresentation(stringFieldValueByName(column).c_str(), false);
+}
+
grt::IntegerRef WBRecordsetResultset::saveFieldValueToFile(ssize_t column, const std::string &file)
{
if (column >= 0 && (size_t)column < recordset->get_column_count())
@@ -440,6 +496,74 @@ class WBPUBLICBACKEND_PUBLIC_FUNC CPPResultsetResultset : public db_query_Result
return grt::StringRef(); // NULL
}
+ virtual grt::StringRef geoStringFieldValue(ssize_t column)
+ {
+ if (column >= 0 && column < (ssize_t)column_by_name.size())
+ {
+ grt::StringRef data(recordset->getString((uint32_t)column + 1));
+
+ try {
+ return getGeoRepresentation(data.c_str(), false);
+ }
+ catch (std::exception &exc)
+ {
+ throw std::invalid_argument(base::strfmt("unable to convert geometry data to WKT for column %li", (long)column).c_str());
+ }
+ }
+ throw std::invalid_argument(base::strfmt("invalid column %li for resultset", (long)column).c_str());
+ }
+
+ virtual grt::StringRef geoStringFieldValueByName(const std::string &column)
+ {
+ if (column_by_name.find(column) != column_by_name.end())
+ {
+ grt::StringRef data(recordset->getString((uint32_t)column_by_name[column]));
+ try {
+ return getGeoRepresentation(data.c_str(), false);
+ }
+ catch (std::exception &exc)
+ {
+ throw std::invalid_argument(base::strfmt("unable to convert geometry data to WKT for column %s", column.c_str()).c_str());
+ }
+ }
+ throw std::invalid_argument(base::strfmt("invalid column %s for resultset", column.c_str()).c_str());
+ }
+
+ virtual grt::StringRef geoJsonFieldValue(ssize_t column)
+ {
+ if (column >= 0 && column < (ssize_t)column_by_name.size())
+ {
+ grt::StringRef data(recordset->getString((uint32_t)column + 1));
+ try {
+ return getGeoRepresentation(data.c_str(), true);
+ }
+ catch (std::exception &exc)
+ {
+ throw std::invalid_argument(base::strfmt("unable to convert geometry data to WKT for column %li", (long)column).c_str());
+ }
+ }
+ throw std::invalid_argument(base::strfmt("invalid column %li for resultset", (long)column).c_str());
+ return grt::StringRef(); // NULL
+ }
+
+ virtual grt::StringRef geoJsonFieldValueByName(const std::string &column)
+ {
+ if (column_by_name.find(column) != column_by_name.end())
+ {
+ grt::StringRef data(recordset->getString((uint32_t)column_by_name[column]));
+ try {
+ return getGeoRepresentation(data.c_str(), true);
+ }
+ catch (std::exception &exc)
+ {
+ throw std::invalid_argument(base::strfmt("unable to convert geometry data to WKT for column %s", column.c_str()).c_str());
+ }
+ }
+ throw std::invalid_argument(base::strfmt("invalid column %s for resultset", column.c_str()).c_str());
+ return grt::StringRef(); // NULL
+ }
+
+
virtual grt::IntegerRef saveFieldValueToFile(ssize_t column, const std::string &file)
{
return grt::IntegerRef(0);
@@ -576,13 +700,35 @@ grt::IntegerRef db_query_Resultset::refresh()
grt::StringRef db_query_Resultset::stringFieldValue(ssize_t column)
{
- return _data ? _data->stringFieldValue(column) : grt::StringRef(); // NULL
+ return _data ? _data->stringFieldValue(column) : grt::StringRef();
}
grt::StringRef db_query_Resultset::stringFieldValueByName(const std::string &column)
{
- return _data ? _data->stringFieldValueByName(column) : grt::StringRef(); // NULL
+ return _data ? _data->stringFieldValueByName(column) : grt::StringRef();
+}
+
+grt::StringRef db_query_Resultset::geoStringFieldValue(ssize_t column)
+{
+ return _data ? _data->geoStringFieldValue(column) : grt::StringRef();
+}
+
+
+grt::StringRef db_query_Resultset::geoStringFieldValueByName(const std::string &column)
+{
+ return _data ? _data->geoStringFieldValueByName(column) : grt::StringRef();
+}
+
+grt::StringRef db_query_Resultset::geoJsonFieldValue(ssize_t column)
+{
+ return _data ? _data->geoJsonFieldValue(column) : grt::StringRef();
+}
+
+
+grt::StringRef db_query_Resultset::geoJsonFieldValueByName(const std::string &column)
+{
+ return _data ? _data->geoJsonFieldValueByName(column) : grt::StringRef();
}
@@ -55,6 +55,10 @@ class WBPUBLICBACKEND_PUBLIC_FUNC db_query_Resultset::ImplData
virtual grt::IntegerRef saveFieldValueToFile(ssize_t column, const std::string &file) = 0;
virtual grt::StringRef stringFieldValue(ssize_t column) = 0;
virtual grt::StringRef stringFieldValueByName(const std::string &column) = 0;
+ virtual grt::StringRef geoStringFieldValue(ssize_t column) = 0;
+ virtual grt::StringRef geoStringFieldValueByName(const std::string &column) = 0;
+ virtual grt::StringRef geoJsonFieldValue(ssize_t column) = 0;
+ virtual grt::StringRef geoJsonFieldValueByName(const std::string &column) = 0;
};
@@ -81,6 +85,10 @@ class WBPUBLICBACKEND_PUBLIC_FUNC WBRecordsetResultset : public db_query_Results
virtual void refresh();
virtual grt::StringRef stringFieldValue(ssize_t column);
virtual grt::StringRef stringFieldValueByName(const std::string &column);
+ virtual grt::StringRef geoStringFieldValue(ssize_t column);
+ virtual grt::StringRef geoStringFieldValueByName(const std::string &column);
+ virtual grt::StringRef geoJsonFieldValue(ssize_t column);
+ virtual grt::StringRef geoJsonFieldValueByName(const std::string &column);
virtual grt::IntegerRef saveFieldValueToFile(ssize_t column, const std::string &file);
};
#endif
Oops, something went wrong.

0 comments on commit f64894f

Please sign in to comment.