From 8aed26651c7bbeef66350a0597c38be2cf45270f Mon Sep 17 00:00:00 2001 From: Xun Li Date: Mon, 5 Jun 2017 20:35:21 -0500 Subject: [PATCH 1/7] fix possible crash when open sample panel update maxp class --- Algorithms/maxp.cpp | 103 +++++++++++++++++++++++++-- Algorithms/maxp.h | 22 +++++- DialogTools/AutoUpdateDlg.cpp | 2 +- DialogTools/ConnectDatasourceDlg.cpp | 8 ++- DialogTools/CreateGridDlg.cpp | 19 ++++- DialogTools/ExportDataDlg.cpp | 2 +- GenUtils.cpp | 6 +- GeoDa.cpp | 7 +- Project.cpp | 2 +- version.h | 4 +- 10 files changed, 157 insertions(+), 18 deletions(-) diff --git a/Algorithms/maxp.cpp b/Algorithms/maxp.cpp index 2fec31e17..a382c2496 100644 --- a/Algorithms/maxp.cpp +++ b/Algorithms/maxp.cpp @@ -31,11 +31,9 @@ #include "cluster.h" #include "maxp.h" - - using namespace std; -Maxp::Maxp(const GalElement* _w, const vector >& _z, int floor, vector floor_variable, int initial, vector seed) +Maxp::Maxp(const GalElement* _w, const vector >& _z, int floor, vector > floor_variable, int initial, vector seed) : w(_w), z(_z), LARGE(1000000), MAX_ATTEMPTS(100) { num_obs = floor_variable.size(); @@ -54,9 +52,41 @@ Maxp::Maxp(const GalElement* _w, const vector >& _z, int floor, feasible = false; else { feasible = true; + double best_val = objective_function(); + // deep copy + vector > current_regions = regions; + map current_area2region = area2region; + vector initial_wss; + int attemps = 0; + + for (int i=0; i 0) { + double val = objective_function(); + initial_wss.push_back(val); + + wxString str; + str << "initial solution"; + str << i; + str << val; + str << best_val; + LOG_MSG(str.ToStdString()); + + if (val < best_val) { + current_regions = regions; + current_area2region = area2region; + best_val = val; + } + attemps += 1; + } + } + + regions = current_regions; + p = regions.size(); + area2region = current_area2region; + swap(); } - } Maxp::~Maxp() @@ -243,3 +273,68 @@ void Maxp::init_solution() } } + +void Maxp::swap() +{ + bool swapping = true; + int swap_iteration = 0; + int total_moves = 0; + int k = regions.size(); + + vector::iterator iter; + vector changed_regions(k, 1); + while (swapping) { + int moves_made = 0; + vector regionIds; + for (int r=0; r0) { + regionIds.push_back(r); + } + } + random_shuffle(regionIds.begin(), regionIds.end()); + for (int r=0; r neighbors; + vector& members = regions[seed]; + for (int j=0; j block = regions[ area2region[ nbr ] ]; // deep copy + //if (check_contiguity(block, neighbor)) { + // block.erase(neighbor); + //} + } + } + } + } +} + +bool Maxp::is_component(const GalElement *w, const vector &ids) +{ + int components = 0; + map masks; + for (int i=0; i q; + for (int i=0; i vdata; + map mdict; + vector::iterator v_iter; + map::iterator m_iter; +}; + /*! A Max-p class */ class Maxp @@ -44,7 +61,7 @@ class Maxp \param initial int number of initial solutions to generate \param seed list ids of observations to form initial seeds. If len(ids) is less than the number of observations, the complementary ids are added to the end of seeds. Thus the specified seeds get priority in the solution */ - Maxp(const GalElement* w, const vector >& z, int floor, vector floor_variable, int initial, vector seed); + Maxp(const GalElement* w, const vector >& z, int floor, vector > floor_variable, int initial, vector seed); //! A Deconstructor @@ -152,6 +169,9 @@ class Maxp double objective_function(); + void swap(); + + bool is_component(const GalElement* w, const vector& ids); }; #endif diff --git a/DialogTools/AutoUpdateDlg.cpp b/DialogTools/AutoUpdateDlg.cpp index 29ce667b4..1579b46f2 100644 --- a/DialogTools/AutoUpdateDlg.cpp +++ b/DialogTools/AutoUpdateDlg.cpp @@ -76,7 +76,7 @@ size_t write_to_file(void *ptr, size_t size, size_t nmemb, void* userdata) string ReadUrlContent(const char* url) { - + wxLogMessage("AutoUpdate::ReadUrlContent()"); string response; CURL* curl = curl_easy_init(); diff --git a/DialogTools/ConnectDatasourceDlg.cpp b/DialogTools/ConnectDatasourceDlg.cpp index 01c8e39d9..9da10784a 100644 --- a/DialogTools/ConnectDatasourceDlg.cpp +++ b/DialogTools/ConnectDatasourceDlg.cpp @@ -454,7 +454,13 @@ void ConnectDatasourceDlg::AddRecentItem(wxBoxSizer* sizer, wxScrolledWindow* sc } file_path_str = GenUtils::GetSamplesDir() + ds_thumb; - wxImage img(file_path_str); + wxImage img; + if (wxFileExists(file_path_str)) { + ds_thumb = "no_map.png"; + file_path_str = GenUtils::GetSamplesDir() + ds_thumb; + + } + img.LoadFile(file_path_str); if (!img.IsOk()) { ds_thumb = "no_map.png"; file_path_str = GenUtils::GetSamplesDir() + ds_thumb; diff --git a/DialogTools/CreateGridDlg.cpp b/DialogTools/CreateGridDlg.cpp index 7ee8d29b0..42c99bdb4 100644 --- a/DialogTools/CreateGridDlg.cpp +++ b/DialogTools/CreateGridDlg.cpp @@ -238,6 +238,20 @@ void CreateGridDlg::OnCReferencefile2Click( wxCommandEvent& event ) wxLogMessage("In CreateGridDlg::OnCReferencefile2Click()"); try{ + wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst(); + while (node) { + wxWindow* win = node->GetData(); + if (ConnectDatasourceDlg* w = dynamic_cast(win)) { + if (w->GetType() == 0) { + w->Show(true); + w->Maximize(false); + w->Raise(); + return; + } + } + node = node->GetNext(); + } + ConnectDatasourceDlg dlg(this); if (dlg.ShowModal() != wxID_OK) return; @@ -374,11 +388,12 @@ void CreateGridDlg::CreateGrid() grids.push_back(new GdaPolygon(n_pts,pts)); - delete pts; + delete[] pts; } } - ExportDataDlg dlg(this, grids, Shapefile::POLYGON); + ExportDataDlg dlg(NULL, grids, Shapefile::POLYGON); + dlg.Raise(); dlg.ShowModal(); m_nCount = nMaxCount; diff --git a/DialogTools/ExportDataDlg.cpp b/DialogTools/ExportDataDlg.cpp index 66cf586f7..a9a60148b 100644 --- a/DialogTools/ExportDataDlg.cpp +++ b/DialogTools/ExportDataDlg.cpp @@ -581,7 +581,7 @@ ExportDataDlg::CreateOGRLayer(wxString& ds_name, OGRDataAdapter& ogr_adapter = OGRDataAdapter::GetInstance(); OGRLayerProxy* new_layer = ogr_adapter.ExportDataSource(ds_format.ToStdString(), - ds_name.ToStdString(), + ds_name, layer_name.ToStdString(), geom_type, ogr_geometries, diff --git a/GenUtils.cpp b/GenUtils.cpp index 517b67648..188f9cd0a 100644 --- a/GenUtils.cpp +++ b/GenUtils.cpp @@ -1340,7 +1340,7 @@ std::string GenUtils::GetBasemapCacheDir() wxString exePath = wxStandardPaths::Get().GetExecutablePath(); wxFileName exeFile(exePath); wxString exeDir = exeFile.GetPathWithSep(); - return std::string(exeDir.mb_str()); + return std::string(GET_ENCODED_FILENAME(exeDir)); } std::string GenUtils::GetWebPluginsDir() @@ -1350,7 +1350,7 @@ std::string GenUtils::GetWebPluginsDir() wxString exeDir = exeFile.GetPathWithSep(); exeDir << "web_plugins" << wxFileName::GetPathSeparator(); - return std::string(exeDir.mb_str()); + return std::string(GET_ENCODED_FILENAME(exeDir)); } std::string GenUtils::GetResourceDir() @@ -1360,7 +1360,7 @@ std::string GenUtils::GetResourceDir() wxString exeDir = exeFile.GetPathWithSep(); exeDir << "../Resources" << wxFileName::GetPathSeparator(); - return std::string(exeDir.mb_str()); + return std::string(GET_ENCODED_FILENAME(exeDir)); } std::string GenUtils::GetSamplesDir() diff --git a/GeoDa.cpp b/GeoDa.cpp index af1c84e52..2e56f5455 100644 --- a/GeoDa.cpp +++ b/GeoDa.cpp @@ -1926,8 +1926,11 @@ void GdaFrame::OnShapePointsFromASCII(wxCommandEvent& WXUNUSED(event) ) void GdaFrame::OnShapePolygonsFromGrid(wxCommandEvent& WXUNUSED(event) ) { wxLogMessage("Open CreateGridDlg"); - CreateGridDlg dlg(this); - dlg.ShowModal(); + //Project* p = GetProject(); + //if (!p || !p->GetTableInt()) return 0; + + CreateGridDlg* dlg = new CreateGridDlg(this); + dlg->Show(true); } #include "DialogTools/Bnd2ShpDlg.h" diff --git a/Project.cpp b/Project.cpp index 0d25debff..8897fc236 100644 --- a/Project.cpp +++ b/Project.cpp @@ -1500,7 +1500,7 @@ bool Project::InitFromOgrLayer() } - OGRDatasourceProxy* ds_proxy = OGRDataAdapter::GetInstance().GetDatasourceProxy(datasource_name.ToStdString(), ds_type); + OGRDatasourceProxy* ds_proxy = OGRDataAdapter::GetInstance().GetDatasourceProxy(datasource_name, ds_type); datasource->UpdateWritable(ds_proxy->is_writable); diff --git a/version.h b/version.h index 38b5100a2..ae9c67c75 100644 --- a/version.h +++ b/version.h @@ -2,10 +2,10 @@ namespace Gda { const int version_major = 1; const int version_minor = 10; const int version_build = 0; - const int version_subbuild = 0; + const int version_subbuild = 2; const int version_year = 2017; const int version_month = 6; - const int version_day = 1; + const int version_day = 5; const int version_night = 0; const int version_type = 2; // 0: alpha, 1: beta, 2: release } From aa33326717e3256716b06a8bd5646c8a093c6166 Mon Sep 17 00:00:00 2001 From: Xun Li Date: Mon, 5 Jun 2017 21:26:39 -0500 Subject: [PATCH 2/7] update for better encoding support --- DialogTools/ConnectDatasourceDlg.cpp | 2 +- DialogTools/DatasourceDlg.cpp | 2 +- Project.cpp | 2 +- ShapeOperations/GdaCache.cpp | 8 ++++---- ShapeOperations/GdaCache.h | 4 ++-- ShapeOperations/OGRDataAdapter.cpp | 4 ++-- ShapeOperations/OGRDataAdapter.h | 6 +++--- ShapeOperations/OGRDatasourceProxy.cpp | 24 ++++++++++++------------ ShapeOperations/OGRDatasourceProxy.h | 10 +++++----- 9 files changed, 31 insertions(+), 31 deletions(-) diff --git a/DialogTools/ConnectDatasourceDlg.cpp b/DialogTools/ConnectDatasourceDlg.cpp index 9da10784a..4d6a885e0 100644 --- a/DialogTools/ConnectDatasourceDlg.cpp +++ b/DialogTools/ConnectDatasourceDlg.cpp @@ -455,7 +455,7 @@ void ConnectDatasourceDlg::AddRecentItem(wxBoxSizer* sizer, wxScrolledWindow* sc file_path_str = GenUtils::GetSamplesDir() + ds_thumb; wxImage img; - if (wxFileExists(file_path_str)) { + if (!wxFileExists(file_path_str)) { ds_thumb = "no_map.png"; file_path_str = GenUtils::GetSamplesDir() + ds_thumb; diff --git a/DialogTools/DatasourceDlg.cpp b/DialogTools/DatasourceDlg.cpp index 8a5f3d749..c3313fdc0 100644 --- a/DialogTools/DatasourceDlg.cpp +++ b/DialogTools/DatasourceDlg.cpp @@ -296,7 +296,7 @@ void DatasourceDlg::PromptDSLayers(IDataSource* datasource) throw GdaException(msg.mb_str()); } - vector table_names = OGRDataAdapter::GetInstance().GetLayerNames(ds_name.ToStdString(), ds_type); + vector table_names = OGRDataAdapter::GetInstance().GetLayerNames(ds_name, ds_type); int n_tables = table_names.size(); diff --git a/Project.cpp b/Project.cpp index 8897fc236..24f405a0b 100644 --- a/Project.cpp +++ b/Project.cpp @@ -516,7 +516,7 @@ void Project::SaveDataSourceAs(const wxString& new_ds_name, bool is_update) wxPD_CAN_ABORT|wxPD_AUTO_HIDE|wxPD_APP_MODAL); OGRLayerProxy* new_layer; new_layer = OGRDataAdapter::GetInstance().ExportDataSource(ds_format.ToStdString(), - new_ds_name.ToStdString(), + new_ds_name, layername.ToStdString(), geom_type, ogr_geometries, diff --git a/ShapeOperations/GdaCache.cpp b/ShapeOperations/GdaCache.cpp index 5599e171f..5cd4d0cfc 100644 --- a/ShapeOperations/GdaCache.cpp +++ b/ShapeOperations/GdaCache.cpp @@ -144,11 +144,11 @@ bool GdaCache::IsLayerUpdated(std::string ext_ds_name, bool GdaCache::IsLayerCached(std::string ext_ds_name, std::string ext_layer_name) { + bool caseSensitive = false; for (size_t i=0; i layer_names; // layer_names; //CleanHistory(); } -vector OGRDataAdapter::GetLayerNames(string ds_name, GdaConst::DataSourceType ds_type) +vector OGRDataAdapter::GetLayerNames(wxString ds_name, GdaConst::DataSourceType ds_type) { OGRDatasourceProxy* ds_proxy = GetDatasourceProxy(ds_name, ds_type); return ds_proxy->GetLayerNames(); @@ -308,7 +308,7 @@ OGRDataAdapter::MakeOGRGeometries(vector& geometries, OGRLayerProxy* OGRDataAdapter::ExportDataSource(string o_ds_format, wxString o_ds_name, - string o_layer_name, + wxString o_layer_name, OGRwkbGeometryType geom_type, vector ogr_geometries, TableInterface* table, diff --git a/ShapeOperations/OGRDataAdapter.h b/ShapeOperations/OGRDataAdapter.h index 9b7b0df0e..190223c69 100644 --- a/ShapeOperations/OGRDataAdapter.h +++ b/ShapeOperations/OGRDataAdapter.h @@ -130,7 +130,7 @@ class OGRDataAdapter { * @param ds_name OGR data source name * @param layer_names a reference to a string vector that stores layer names */ - vector GetLayerNames(string ds_name, GdaConst::DataSourceType ds_type); + vector GetLayerNames(wxString ds_name, GdaConst::DataSourceType ds_type); /** * cacher existing layer (memory) to local spatialite @@ -163,7 +163,7 @@ class OGRDataAdapter { */ OGRLayerProxy* ExportDataSource(string o_ds_format, wxString o_ds_name, - string o_layer_name, + wxString o_layer_name, OGRwkbGeometryType geom_type, vector ogr_geometries, TableInterface* table, @@ -185,4 +185,4 @@ class OGRDataAdapter { vector& ogr_geometries, vector& selected_rows); }; -#endif \ No newline at end of file +#endif diff --git a/ShapeOperations/OGRDatasourceProxy.cpp b/ShapeOperations/OGRDatasourceProxy.cpp index a9f345748..b05cb589c 100644 --- a/ShapeOperations/OGRDatasourceProxy.cpp +++ b/ShapeOperations/OGRDatasourceProxy.cpp @@ -145,7 +145,7 @@ OGRDatasourceProxy::OGRDatasourceProxy(wxString format, wxString dest_datasource OGRDatasourceProxy::~OGRDatasourceProxy() { // clean map of layer_pool - map::iterator it; + map::iterator it; for (it=layer_pool.begin(); it!=layer_pool.end(); it++) { if (it->second) delete it->second; @@ -177,7 +177,7 @@ OGRDatasourceProxy::GetGdaDataSourceType(GDALDriver *poDriver) } } -vector OGRDatasourceProxy::GetLayerNames() +vector OGRDatasourceProxy::GetLayerNames() { // GetLayerNames can happen before actually read data from layer // , so this provide us a chance to store all OGRLayer instance @@ -197,9 +197,9 @@ vector OGRDatasourceProxy::GetLayerNames() OGRLayerProxy* layer_proxy = NULL; OGRLayer* layer = ds->GetLayer(0); if (layer) { - string layer_name(layer->GetName()); + wxString layer_name(layer->GetName()); this->layer_names.push_back(layer_name); - layer_pool[layer_name] = new OGRLayerProxy(layer_name,layer,ds_type); + layer_pool[layer_name] = new OGRLayerProxy(std::string(GET_ENCODED_FILENAME(layer_name)),layer,ds_type); } } else { @@ -209,8 +209,8 @@ vector OGRDatasourceProxy::GetLayerNames() for (int i=0; iGetLayer(i); - string layer_name(layer->GetName()); - // don't show some system tables in postgres + wxString layer_name(layer->GetName()); + // don't \show some system tables in postgres if ( layer_name == "raster_columns" || layer_name == "raster_overviews" || layer_name == "topology.topology" || @@ -220,7 +220,7 @@ vector OGRDatasourceProxy::GetLayerNames() continue; } this->layer_names.push_back(layer_name); - layer_pool[layer_name] = new OGRLayerProxy(layer_name,layer,ds_type); + layer_pool[layer_name] = new OGRLayerProxy(std::string(GET_ENCODED_FILENAME(layer_name)),layer,ds_type); } layer_count = layer_count - system_layers; @@ -254,7 +254,7 @@ bool OGRDatasourceProxy::DeleteLayer(string layer_name) string tmp_layer_name(layer->GetName()); if ( tmp_layer_name.compare(layer_name) == 0) { if ( ds->DeleteLayer(i) == OGRERR_NONE ) { - map::iterator it = + map::iterator it = layer_pool.find(layer_name); if ( it != layer_pool.end()) { layer_pool.erase(it); @@ -333,7 +333,7 @@ void OGRDatasourceProxy::CreateDataSource(string format, // Create a geom only layer, the table will added ID column automatically OGRLayerProxy* -OGRDatasourceProxy::CreateLayer(string layer_name, +OGRDatasourceProxy::CreateLayer(wxString layer_name, OGRwkbGeometryType eGType, vector& geometries, TableInterface* table, @@ -355,11 +355,11 @@ OGRDatasourceProxy::CreateLayer(string layer_name, // LAUNDER is for database: rename desired field name char* papszLCO[50] = {"OVERWRITE=yes", "PRECISION=no", "LAUNDER=yes"}; - OGRLayer *poDstLayer = ds->CreateLayer(layer_name.c_str(), + OGRLayer *poDstLayer = ds->CreateLayer(layer_name.mb_str(), poOutputSRS, eGType, papszLCO); if( poDstLayer == NULL ) { - error_message << "Can't write/create layer \"" << layer_name << "\". \n\nDetails: Attemp to write a readonly database, or " + error_message << "Can't write/create layer \"" << layer_name.mb_str() << "\". \n\nDetails: Attemp to write a readonly database, or " << CPLGetLastErrorMsg(); throw GdaException(error_message.str().c_str()); } @@ -381,7 +381,7 @@ OGRDatasourceProxy::CreateLayer(string layer_name, wxString fname = table->GetColName(col_id_map[id], t); if (fname.empty()) { - error_message << "Can't create layer \"" << layer_name + error_message << "Can't create layer \"" << layer_name.mb_str() << "\" with empty field name."; throw GdaException(error_message.str().c_str()); } diff --git a/ShapeOperations/OGRDatasourceProxy.h b/ShapeOperations/OGRDatasourceProxy.h index f0a536b7f..86ad888ee 100644 --- a/ShapeOperations/OGRDatasourceProxy.h +++ b/ShapeOperations/OGRDatasourceProxy.h @@ -72,9 +72,9 @@ class OGRDatasourceProxy { size_t layer_count; //!< number of layer in this data source - map layer_pool; //!< dict for all opened layers + map layer_pool; //!< dict for all opened layers - vector layer_names; + vector layer_names; public: /** @@ -99,7 +99,7 @@ class OGRDatasourceProxy { * Return the number of layers, in case there is no layer in datasource. * (e.g. an empty spatialite file db). */ - vector GetLayerNames(); + vector GetLayerNames(); /** * Return layer proxy according to the layer_name @@ -115,7 +115,7 @@ class OGRDatasourceProxy { OGRLayerProxy* ExecuteSQL(string sql); - OGRLayerProxy* CreateLayer(string layer_name, + OGRLayerProxy* CreateLayer(wxString layer_name, OGRwkbGeometryType eGType, vector& geometries, TableInterface* table, @@ -127,4 +127,4 @@ class OGRDatasourceProxy { }; -#endif \ No newline at end of file +#endif From bdf6a6353145389121c2cf11478e558aaeb0d1ff Mon Sep 17 00:00:00 2001 From: Xun Li Date: Mon, 5 Jun 2017 21:33:45 -0500 Subject: [PATCH 3/7] fix compiling issue on win --- Algorithms/maxp.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Algorithms/maxp.cpp b/Algorithms/maxp.cpp index a382c2496..3f00186aa 100644 --- a/Algorithms/maxp.cpp +++ b/Algorithms/maxp.cpp @@ -337,4 +337,5 @@ bool Maxp::is_component(const GalElement *w, const vector &ids) { int node = ids[i]; } + return false; } From d189b8492c5d53fb88e98b226ae99c4d712e851b Mon Sep 17 00:00:00 2001 From: Xun Li Date: Tue, 6 Jun 2017 00:28:06 -0500 Subject: [PATCH 4/7] fix possible recent panel issue with international characters --- DialogTools/ConnectDatasourceDlg.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/DialogTools/ConnectDatasourceDlg.cpp b/DialogTools/ConnectDatasourceDlg.cpp index 4d6a885e0..ab9852a06 100644 --- a/DialogTools/ConnectDatasourceDlg.cpp +++ b/DialogTools/ConnectDatasourceDlg.cpp @@ -146,7 +146,7 @@ void RecentDatasource::Init(wxString json_str_) } else if (i->name_ == "ds_thumb") { val = i->value_; - ds_thumb = val.get_str(); + ds_thumb = wxString(val.get_str()); } } ds_names.push_back(ds_name); @@ -173,7 +173,8 @@ void RecentDatasource::Save() json_spirit::Object ds_obj; std::string ds_name( GET_ENCODED_FILENAME(ds_names[i])); std::string layer_name( GET_ENCODED_FILENAME(ds_layernames[i])); - std::string ds_conf( ds_confs[i].mb_str() ); + //std::string ds_conf( ds_confs[i].mb_str() ); + std::string ds_conf( GET_ENCODED_FILENAME(ds_confs[i]) ); std::string ds_thumb( GET_ENCODED_FILENAME(ds_thumbnails[i]) ); ds_obj.push_back( json_spirit::Pair("ds_name", ds_name) ); ds_obj.push_back( json_spirit::Pair("layer_name", layer_name) ); @@ -344,7 +345,7 @@ IDataSource* RecentDatasource::GetDatasource(wxString ds_name) if (ds_names[i] == ds_name) { wxString ds_conf = ds_confs[i]; try { - return IDataSource::CreateDataSource(ds_conf); + return IDataSource::CreateDataSource(ds_confs[i]); } catch(GdaException& ex){ return NULL; } From 136b97d6532ddf76c01ca8bbb99ab85ed8cffab7 Mon Sep 17 00:00:00 2001 From: Xun Li Date: Tue, 6 Jun 2017 00:28:43 -0500 Subject: [PATCH 5/7] fix possible issue of recent panel --- DataViewer/DataSource.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/DataViewer/DataSource.cpp b/DataViewer/DataSource.cpp index 73a38b373..622372c4d 100644 --- a/DataViewer/DataSource.cpp +++ b/DataViewer/DataSource.cpp @@ -217,8 +217,7 @@ IDataSource* IDataSource::CreateDataSource(wxString data_type_name, IDataSource* IDataSource::CreateDataSource(wxString ds_json) { // '{"ds_type":"ds_shapefile", "ds_path": "/test.shp", "db_name":"test"..}' - std::string ds_json_str(ds_json.mb_str()); - + std::string ds_json_str(GET_ENCODED_FILENAME(ds_json)); json_spirit::Value v; try { @@ -369,15 +368,20 @@ void FileDataSource::ReadPtree(const ptree& pt, wxString FileDataSource::GetJsonStr() { - std::string ds_type_str(GetDataTypeNameByGdaDSType(ds_type).mb_str()); - std::string ds_path_str(GET_ENCODED_FILENAME(file_repository_path)); + //std::string ds_type_str(GetDataTypeNameByGdaDSType(ds_type).mb_str()); + //std::string ds_path_str(GET_ENCODED_FILENAME(file_repository_path)); // utf-8 coded - json_spirit::Object ret_obj; - ret_obj.push_back(json_spirit::Pair("ds_type", ds_type_str)); - ret_obj.push_back(json_spirit::Pair("ds_path", ds_path_str)); + //json_spirit::Object ret_obj; + //ret_obj.push_back(json_spirit::Pair("ds_type", ds_type_str)); + //ret_obj.push_back(json_spirit::Pair("ds_path", ds_path_str)); - std::string json_str = json_spirit::write(ret_obj); - return wxString(json_str); + //std::string json_str1 = json_spirit::write(ret_obj); + + wxString json_tmp = "{\"ds_type\":\"%s\", \"ds_path\":\"%s\"}"; + wxString json_path = file_repository_path; + json_path.Replace("\\", "\\\\"); + wxString json_str = wxString::Format(json_tmp, GetDataTypeNameByGdaDSType(ds_type), json_path); + return json_str; } void FileDataSource::WritePtree(ptree& pt, From ab8308427e89e6415f1df654d6194686b380c36f Mon Sep 17 00:00:00 2001 From: Xun Li Date: Tue, 6 Jun 2017 01:22:31 -0500 Subject: [PATCH 6/7] update possible basemap issue due to international characters --- DataViewer/DataSource.cpp | 5 +++- Explore/Basemap.cpp | 54 ++++++++++++++------------------------- Explore/Basemap.h | 6 ++--- GenUtils.cpp | 14 +++++----- GenUtils.h | 8 +++--- 5 files changed, 37 insertions(+), 50 deletions(-) diff --git a/DataViewer/DataSource.cpp b/DataViewer/DataSource.cpp index 622372c4d..6eefd6bbf 100644 --- a/DataViewer/DataSource.cpp +++ b/DataViewer/DataSource.cpp @@ -380,7 +380,10 @@ wxString FileDataSource::GetJsonStr() wxString json_tmp = "{\"ds_type\":\"%s\", \"ds_path\":\"%s\"}"; wxString json_path = file_repository_path; json_path.Replace("\\", "\\\\"); - wxString json_str = wxString::Format(json_tmp, GetDataTypeNameByGdaDSType(ds_type), json_path); + + //wxString json_str = wxString::Format(json_tmp, GetDataTypeNameByGdaDSType(ds_type), json_path); + wxString json_str = "{\"ds_type\":\""+GetDataTypeNameByGdaDSType(ds_type)+"\", \"ds_path\":\""+json_path+"\"}"; + return json_str; } diff --git a/Explore/Basemap.cpp b/Explore/Basemap.cpp index c6624fd87..01a0281bc 100644 --- a/Explore/Basemap.cpp +++ b/Explore/Basemap.cpp @@ -60,7 +60,7 @@ XY::XY(double _x, double _y) Basemap::Basemap(Screen* _screen, MapLayer *_map, int map_type, - string _cachePath, + wxString _cachePath, OGRCoordinateTransformation *_poCT ) { poCT = _poCT; @@ -119,9 +119,8 @@ Basemap::~Basemap() { void Basemap::CleanCache() { - std::ostringstream filepathBuf; - filepathBuf << cachePath << "basemap_cache"<< separator(); - wxString filename = filepathBuf.str(); + wxString filename; + filename << cachePath << "basemap_cache"<< separator(); wxDir dir(filename); if (dir.IsOpened() ) { wxString file; @@ -528,11 +527,10 @@ size_t curlCallback(void *ptr, size_t size, size_t nmemb, void* userdata) void Basemap::DownloadTile(int x, int y) { // detect if file exists in temp/ directory - std::string filepathStr = GetTilePath(x, y); - char* filepath = new char[filepathStr.length() + 1]; - std::strcpy(filepath, filepathStr.c_str()); + wxString filepathStr = GetTilePath(x, y); + std::string filepath = GET_ENCODED_FILENAME(filepathStr); - if (!is_file_exist(filepath)) { + if (!wxFileExists(filepathStr)) { // otherwise, download the image std::string urlStr = GetTileUrl(x, y); char* url = new char[urlStr.length() + 1]; @@ -544,7 +542,7 @@ void Basemap::DownloadTile(int x, int y) image = curl_easy_init(); if (image) { - fp = fopen(filepath, "wb"); + fp = fopen(filepath.c_str(), "wb"); if (fp) { curl_easy_setopt(image, CURLOPT_URL, url); @@ -557,19 +555,6 @@ void Basemap::DownloadTile(int x, int y) // Grab image imgResult = curl_easy_perform(image); - /* - if( imgResult ){ - cout << "Cannot grab the image!\n"; - } - - int res_code = 0; - curl_easy_getinfo(image, CURLINFO_RESPONSE_CODE, &res_code); - if (!((res_code == 200 || res_code == 201) && imgResult != CURLE_ABORTED_BY_CALLBACK)) - { - printf("!!! Response code: %d\n", res_code); - } - */ - // Clean up the resources curl_easy_cleanup(image); fclose(fp); } @@ -580,7 +565,6 @@ void Basemap::DownloadTile(int x, int y) } isTileReady = false; // notice template_canvas to draw //canvas->Refresh(true); - delete[] filepath; } @@ -646,23 +630,24 @@ std::string Basemap::GetTileUrl(int x, int y) return urlStr; } -std::string Basemap::GetTilePath(int x, int y) +wxString Basemap::GetTilePath(int x, int y) { - std::ostringstream filepathBuf; + //std::ostringstream filepathBuf; + wxString filepathBuf; filepathBuf << cachePath << "basemap_cache"<< separator(); filepathBuf << mapType << "-"; - filepathBuf << zoom << "-" << x << "-" << y << imageSuffix; - std::string filepathStr = filepathBuf.str(); - std::string newpath; - for (int i = 0; i < filepathStr.length() ;i++) + filepathBuf << zoom << "-" << x << "-" << y << imageSuffix; + + wxString newpath; + for (int i = 0; i < filepathBuf.length() ;i++) { - if(filepathStr[i] == '\\') + if(filepathBuf[i] == '\\') { - newpath += filepathStr[i]; - newpath += filepathStr[i]; + newpath += filepathBuf[i]; + newpath += filepathBuf[i]; } else - newpath += filepathStr[i]; + newpath += filepathBuf[i]; } return newpath; } @@ -690,8 +675,7 @@ bool Basemap::Draw(wxBitmap* buffer) idx_x = nn + i; int idx_y = j < 0 ? nn + j : j; - std::string filepathStr = GetTilePath(idx_x, idx_y); - wxString wxFilePath(filepathStr); + wxString wxFilePath = GetTilePath(idx_x, idx_y); wxFileName fp(wxFilePath); wxBitmap bmp; if (imageSuffix == ".png") { diff --git a/Explore/Basemap.h b/Explore/Basemap.h index 61d85525e..cd9f6a038 100644 --- a/Explore/Basemap.h +++ b/Explore/Basemap.h @@ -230,7 +230,7 @@ class Basemap { Basemap(Screen *_screen, MapLayer *_map, int map_type, - std::string _cachePath, + wxString _cachePath, OGRCoordinateTransformation *_poCT ); ~Basemap(); @@ -263,7 +263,7 @@ class Basemap { Screen* screen; MapLayer* map; MapLayer* origMap; - std::string cachePath; + wxString cachePath; double Deg2Rad (double degree) { return degree * M_PI / 180.0; } double Rad2Deg (double radians) { return radians * 180.0 / M_PI;} @@ -272,7 +272,7 @@ class Basemap { void LatLngToXY(double lng, double lat, int &x, int &y); std::string GetTileUrl(int x, int y); - std::string GetTilePath(int x, int y); + wxString GetTilePath(int x, int y); bool Draw(wxBitmap* buffer); diff --git a/GenUtils.cpp b/GenUtils.cpp index 188f9cd0a..3b5175581 100644 --- a/GenUtils.cpp +++ b/GenUtils.cpp @@ -1335,35 +1335,35 @@ wxString GenUtils::WrapText(wxWindow *win, const wxString& text, int widthMax) return wrapper.GetWrapped(); } -std::string GenUtils::GetBasemapCacheDir() +wxString GenUtils::GetBasemapCacheDir() { wxString exePath = wxStandardPaths::Get().GetExecutablePath(); wxFileName exeFile(exePath); wxString exeDir = exeFile.GetPathWithSep(); - return std::string(GET_ENCODED_FILENAME(exeDir)); + return exeDir; } -std::string GenUtils::GetWebPluginsDir() +wxString GenUtils::GetWebPluginsDir() { wxString exePath = wxStandardPaths::Get().GetExecutablePath(); wxFileName exeFile(exePath); wxString exeDir = exeFile.GetPathWithSep(); exeDir << "web_plugins" << wxFileName::GetPathSeparator(); - return std::string(GET_ENCODED_FILENAME(exeDir)); + return exeDir; } -std::string GenUtils::GetResourceDir() +wxString GenUtils::GetResourceDir() { wxString exePath = wxStandardPaths::Get().GetExecutablePath(); wxFileName exeFile(exePath); wxString exeDir = exeFile.GetPathWithSep(); exeDir << "../Resources" << wxFileName::GetPathSeparator(); - return std::string(GET_ENCODED_FILENAME(exeDir)); + return exeDir; } -std::string GenUtils::GetSamplesDir() +wxString GenUtils::GetSamplesDir() { #ifdef __WXOSX__ return GetResourceDir(); diff --git a/GenUtils.h b/GenUtils.h index ffcd6e84e..6870bf61a 100644 --- a/GenUtils.h +++ b/GenUtils.h @@ -359,10 +359,10 @@ namespace GenUtils { wxString WrapText(wxWindow *win, const wxString& text, int widthMax); - std::string GetBasemapCacheDir(); - std::string GetWebPluginsDir(); - std::string GetResourceDir(); - std::string GetSamplesDir(); + wxString GetBasemapCacheDir(); + wxString GetWebPluginsDir(); + wxString GetResourceDir(); + wxString GetSamplesDir(); bool less_vectors(const std::vector& a,const std::vector& b); } From 2535f7ac11023b53184b9129932c6db7b035d90e Mon Sep 17 00:00:00 2001 From: Xun Li Date: Tue, 6 Jun 2017 01:39:22 -0500 Subject: [PATCH 7/7] update sample path to handle possible crash --- DialogTools/ConnectDatasourceDlg.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DialogTools/ConnectDatasourceDlg.cpp b/DialogTools/ConnectDatasourceDlg.cpp index ab9852a06..da91c83a8 100644 --- a/DialogTools/ConnectDatasourceDlg.cpp +++ b/DialogTools/ConnectDatasourceDlg.cpp @@ -1115,9 +1115,11 @@ void ConnectDatasourceDlg::OnSample(wxCommandEvent& event) if (ds_name == "samples.sqlite") { ds_name = GenUtils::GetSamplesDir() + ds_name; ds_name.Replace("\\", "\\\\"); - ds_json = wxString::Format("{\"ds_type\":\"SQLite\", \"ds_path\": \"%s\"}", ds_name); + //ds_json = wxString::Format("{\"ds_type\":\"SQLite\", \"ds_path\": \"%s\"}", ds_name); + ds_json = "{\"ds_type\":\"SQLite\", \"ds_path\": \""+ds_name+"\"}"; } else { - ds_json = wxString::Format("{\"ds_type\":\"GeoJSON\", \"ds_path\": \"%s\"}", ds_name); + //ds_json = wxString::Format("{\"ds_type\":\"GeoJSON\", \"ds_path\": \"%s\"}", ds_name); + ds_json = "{\"ds_type\":\"GeoJSON\", \"ds_path\": \""+ds_name+"\"}"; } IDataSource* ds = IDataSource::CreateDataSource(ds_json);