示例#1
0
void SHP2ASCDlg::OnCOpenIshpClick( wxCommandEvent& event )
{
    try{
        ConnectDatasourceDlg dlg(this);
        if (dlg.ShowModal() != wxID_OK) return;
        
        wxString proj_title = dlg.GetProjectTitle();
        wxString layer_name = dlg.GetLayerName();
        IDataSource* datasource = dlg.GetDataSource();
        wxString ds_name = datasource->GetOGRConnectStr();
        GdaConst::DataSourceType ds_type = datasource->GetType();
        
        ogr_ds = new OGRDatasourceProxy(ds_name, ds_type, true);
        ogr_layer = ogr_ds->GetLayerProxy(layer_name.ToStdString());
        ogr_layer->ReadData();

        bool is_valid_layer = true;
        
        if (ogr_layer->IsTableOnly()) {
            wxMessageBox("This is not a shape datasource. Please open a valid "
                         "shape datasource, e.g. ESRI Shapefile, PostGIS layer...");
            is_valid_layer = false;
        }
        if (ogr_layer->GetNumFields() == 0){
            wxMessageBox("No fields found!");
            is_valid_layer = false;
        }
        if ( !is_valid_layer) {
            delete ogr_ds;
            ogr_ds = NULL;
            return;
        }
        
        m_X->Clear();
        for (int i=0; i<ogr_layer->GetNumFields(); i++){
            m_X->Append(wxString::Format("%s",ogr_layer->GetFieldName(i)));
        }
        m_X->SetSelection(0);
        m_inputfile->SetValue(layer_name);
        
        FindWindow(XRCID("IDC_OPEN_OASC"))->Enable(true);
        FindWindow(XRCID("IDC_FIELD_ASC"))->Enable(true);
        FindWindow(XRCID("IDC_KEYVAR"))->Enable(true);
    } catch (GdaException& e) {
        wxMessageDialog dlg (this, e.what(), "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
        return;
    }
}
示例#2
0
/**
 * When user choose a data source, validate it first, 
 * then create a Project() that will be used by the 
 * main program.
 */
void ExportDataDlg::OnOkClick( wxCommandEvent& event )
{
    int datasource_type = m_ds_notebook->GetSelection();
    IDataSource* datasource = GetDatasource();
    wxString ds_name = datasource->GetOGRConnectStr();
	GdaConst::DataSourceType ds_type = datasource->GetType();
    
    if (ds_name.length() <= 0 ) {
        wxMessageDialog dlg(this, "Please specify a valid data source name.",
                            "Warning", wxOK | wxICON_WARNING);
        dlg.ShowModal();
        return;
    }
    
    bool is_update = false;
    wxString tmp_ds_name;
    
	try{
        TableInterface* table = NULL;
		OGRSpatialReference* spatial_ref = NULL;
        
        if ( project_p == NULL ) {
            //project does not exist, could be created a datasource from
            //geometries only, e.g. boundray file
        } else {
            //case: save current open datasource as a new datasource
			table = project_p->GetTableInt();
			spatial_ref = project_p->GetSpatialReference();
            // warning if saveas not compaptible
            GdaConst::DataSourceType o_ds_type = project_p->GetDatasourceType();
            bool o_ds_table_only = IDataSource::IsTableOnly(o_ds_type);
            bool n_ds_table_only = IDataSource::IsTableOnly(ds_type);
            
            if (o_ds_table_only && !n_ds_table_only) {
                if (project_p && project_p->main_data.records.size() ==0) {
                    if (ds_type == GdaConst::ds_geo_json ||
                        ds_type == GdaConst::ds_kml ||
                        ds_type == GdaConst::ds_shapefile) {
                        // can't save a table-only ds to non-table-only ds,
                        // if there is no new geometries to be saved.
                        wxString msg = "GeoDa can't export a Table-only data "
                        "source to a Geometry data source. Please try to add a "
                        "geometry layer and then export.";
                        throw GdaException(msg.mb_str());
                    }
                }
            } else if ( !o_ds_table_only && n_ds_table_only) {
                // possible loss geom data save a non-table ds to table-only ds
                wxString msg = "The geometries will not be saved when exporting "
                "a Geometry data source to a Table-only data source.\n\n"
                "Do you want to continue?";
                wxMessageDialog dlg(this, msg, "Warning: loss data",
                                    wxYES_NO | wxICON_WARNING);
                if (dlg.ShowModal() != wxID_YES)
                    return;
            }
		}

		// by default the datasource will be re-created, except for some special
        // cases: e.g. sqlite, ESRI FileGDB
		if (datasource_type == 0) {
			if (wxFileExists(ds_name)) {
				if (ds_name.EndsWith(".sqlite")) {
					// add new layer to existing sqlite
					is_update = true;
				} else {
					wxRemoveFile(ds_name);
				}
			} else if (wxDirExists(ds_name)) {
				// only for adding new layer to ESRI File Geodatabase
				is_update = true;
				wxDir dir(ds_name);
				wxString first_filename;
				if (!dir.GetFirst(&first_filename)) {
					// for an empty .gdb directory, create a new FileGDB datasource
					is_update = false;
					wxRmDir(ds_name);
				}
			}
		}

        if( !CreateOGRLayer(ds_name, table, spatial_ref, is_update) ) {
            wxString msg = "Exporting has been cancelled.";
            throw GdaException(msg.mb_str(), GdaException::NORMAL);
        }
        // save project file
        if (m_chk_create_project->IsChecked() ) {
            //wxString proj_fname = project_file_name;
            wxString proj_fname = wxEmptyString;
            ProjectConfiguration* project_conf = NULL;
            
            if ( m_chk_create_project->IsChecked() ){
                // Export case: create a project file
                // Export means exporting current datasource to a new one, and
                // create a new project file that is based on this datasource.
                // E.g. export a shape file to PostgreGIS layer, then the new
                // project file should has <datasource> content of database
                // configuration
                wxString file_dlg_title = "GeoDa Project to Save As";
                wxString file_dlg_type =  "GeoDa Project (*.gda)|*.gda";
                wxFileDialog dlg(this, file_dlg_title, wxEmptyString,
                                 wxEmptyString, file_dlg_type,
                                 wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
                if (dlg.ShowModal() != wxID_OK) return;
                wxFileName f(dlg.GetPath());
                f.SetExt("gda");
                proj_fname = f.GetFullPath();
                // copy a project_conf for exporting, will be deleted later
                project_conf = project_p->GetProjectConf()->Clone();
            }
            // save project file
            wxFileName new_proj_fname(proj_fname);
            wxString proj_title = new_proj_fname.GetName();
            LayerConfiguration* layer_conf
                = project_conf->GetLayerConfiguration();
            layer_conf->SetName(layer_name);
            layer_conf->UpdateDataSource(datasource);
            project_conf->Save(proj_fname);
          
            // in export case, delete cloned project_conf
            if ( proj_fname.empty() ) {
                delete project_conf;
                //delete datasource; Note: it is deleted in project_conf
            }
        }
	} catch (GdaException& e) {
        if (e.type() == GdaException::NORMAL)
            return;
        // special clean up for file datasource
        if ( !tmp_ds_name.empty() ) {
            if ( wxFileExists(tmp_ds_name) && !tmp_ds_name.EndsWith(".sqlite")){
                wxRemoveFile(ds_name);
                wxCopyFile(tmp_ds_name, ds_name);
                wxRemoveFile(tmp_ds_name);
            }
        }
		wxMessageDialog dlg(this, e.what() , "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}

	wxString msg = "Export successfully.";
    //msg << "\n\nTips: if you want to use exported project/datasource, please"
    //    << " close current project and then open exported project/datasource.";
	wxMessageDialog dlg(this, msg , "Info", wxOK | wxICON_INFORMATION);
    dlg.ShowModal();
    
	EndDialog(wxID_OK);
}
示例#3
0
ProjectInfoDlg::ProjectInfoDlg(Project* project)
: wxDialog(NULL, wxID_ANY, "Project Information", wxDefaultPosition, wxSize(250, 150))
{
    wxLogMessage("Open ProjectInfoDlg.");
    
	using namespace std;
	wxPanel* panel = new wxPanel(this, -1);
	
	wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
	wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
	
	
	vector<wxString> key;
	vector<wxString> val;
	key.push_back("Project Title");
	val.push_back(project->GetProjectTitle());
	
	key.push_back("Project File");
	wxString pfp = project->GetProjectFullPath();
	if (pfp.IsEmpty()) {
		val.push_back("N/A");
	} else {
		val.push_back(pfp);
	}
		
	IDataSource* ds = project->GetDataSource();
	
	key.push_back("Data Source Type");
	val.push_back(GdaConst::datasrc_type_to_str[ds->GetType()]);
	
	if (FileDataSource* d = dynamic_cast<FileDataSource*>(ds)) {
		key.push_back("Data Source File");
		val.push_back(d->GetFilePath());
	} else if (DBDataSource* d = dynamic_cast<DBDataSource*>(ds)) {
		key.push_back("Database Name");
		val.push_back(d->GetDBName());
		key.push_back("Database Host");
		val.push_back(d->GetDBHost());
		key.push_back("Database Port");
		val.push_back(d->GetDBPort());
		key.push_back("Database User");
		val.push_back(d->GetDBUser());
	} else if (WebServiceDataSource* d =
			   dynamic_cast<WebServiceDataSource*>(ds)) {
		key.push_back("Web File Service URL");
		val.push_back(d->GetURL());
	}
	
	key.push_back("Layer Name");
	wxString layername;
	if (ProjectConfiguration* pc = project->GetProjectConf()) {
		if (LayerConfiguration* lc = pc->GetLayerConfiguration()) {
			layername = lc->GetName();
		}
	}
	val.push_back(layername);
	
	TableInterface* table_int = project->GetTableInt();
	key.push_back("Number Records/Observations");
	val.push_back(wxString::Format("%d", table_int->GetNumberRows()));
	
	int table_cols = table_int->GetNumberCols();
	int grp_cnt = 0;
	int fld_cnt = 0;
	int plhdr_cnt = 0;
	for (int c=0; c<table_cols; ++c) {
		if (table_int->IsColTimeVariant(c)) {
			++grp_cnt;
			for (int t=0, ts=table_int->GetColTimeSteps(c); t<ts; ++t) {
				if (table_int->GetColType(c,t) != GdaConst::placeholder_type) {
					++fld_cnt;
				} else {
					++plhdr_cnt;
				}
			}
		} else {
			++fld_cnt;
		}
	}
	
	key.push_back("Number Data Source Fields");
	val.push_back(wxString::Format("%d", fld_cnt));
	
	key.push_back("Number Table Columns");
	val.push_back(wxString::Format("%d", table_cols));
	
	key.push_back("Number Table Groups");
	val.push_back(wxString::Format("%d", grp_cnt));
    
    if (project->IsTableOnlyProject() == false) {
        key.push_back("Map boundary");
        double minx = 0, miny = 0,  maxx = 0,  maxy = 0;
        project->GetMapExtent(minx, miny, maxx, maxy);
        val.push_back(wxString::Format("Lower left: %f, %f Upper right: %f, %f", minx, miny, maxx, maxy));
    }
		
	const int left_offset = 0;
	const int top_offset = 0;
	const int line_space = 25;
	for (int i=0, sz=key.size(); i<sz; ++i) {
		wxString s;
		s << key[i] << ": " << val[i];
		wxStaticText* st;
		wxPoint pos(left_offset, top_offset + i*line_space);
		st = new wxStaticText(panel, wxID_ANY, s, pos, wxDefaultSize, wxALIGN_LEFT);
	}
	
	
	wxButton* ok_btn = new wxButton(this, wxID_OK, "OK", wxDefaultPosition,
									wxDefaultSize, wxBU_EXACTFIT);
	
	hbox->Add(ok_btn, 1, wxLEFT, 15);
	
	vbox->Add(panel, 1, wxALL, 15);
	vbox->Add(hbox, 0, wxALIGN_CENTER | wxALL, 10);
	
	SetSizer(vbox);
	vbox->Fit(this);
	
	Center();
}