예제 #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);
}