/* Compare our assumed "primary key", or identifying record */ bool compareDBFkey(DBFHandle iDBF, int iRecord, DBFHandle jDBF, int jRecord, bool print) { int i,j; int temp; bool result; DBFFieldType eType; char szTitle[12]; // Column heading int nWidth, nDecimals; result = false; if( identifyKey >= 0 ) { i = identifyKey; j = columnMatchArray[i]; eType = DBFGetFieldInfo( iDBF, i, szTitle, &nWidth, &nDecimals ); assert( nWidth < BUFFER_MAX ); memcpy( &iBuffer, DBFReadStringAttribute( iDBF, iRecord, i ), nWidth ); memcpy( &jBuffer, DBFReadStringAttribute( jDBF, jRecord, j ), nWidth ); temp = memcmp ( &iBuffer, &jBuffer, nWidth ); if( temp ) result = true; } return( result ); }
// extract info from dbf void shapes_load_dbf(const char* filename, const char* column){ DBFHandle hDBF; hDBF = DBFOpen( filename, "rb" ); if( hDBF == NULL ) { fprintf(stderr, "DBFOpen(%s,\"r\") failed.\n", filename ); return; } // INFO ABOUT DBF // int *panWidth; // int nWidth, nDecimals; // fprintf (stderr, "Info for %s\n", filename); // i = DBFGetFieldCount(hDBF); // fprintf (stderr, "%d Columns, %d Records in file\n",i,DBFGetRecordCount(hDBF)); // panWidth = (int *) malloc( DBFGetFieldCount( hDBF ) * sizeof(int) ); // for( int i = 0; i < DBFGetFieldCount(hDBF); i++ ) { // DBFFieldType eType; // char szTitle[256]; // eType = DBFGetFieldInfo( hDBF, i, szTitle, &nWidth, &nDecimals ); // fprintf(stderr, "%4d: %10s %c", i, szTitle, i%4 ? '|':'\n'); // } // fprintf(stderr, "\n"); // print names uint32_t fid = DBFGetFieldIndex(hDBF, column); for(uint32_t i = 0; i < DBFGetRecordCount(hDBF); i++ ) { char* name_long = (char *) DBFReadStringAttribute(hDBF, i, fid); fprintf(stderr, "%d: %s\n", i, name_long); } DBFClose( hDBF ); }
int DBFIsAttributeNULL( DBFHandle psDBF, int iRecord, int iField ) { const char *pszValue; pszValue = DBFReadStringAttribute( psDBF, iRecord, iField ); switch(psDBF->pachFieldType[iField]) { case 'N': case 'F': /* NULL numeric fields have value "****************" */ return pszValue[0] == '*'; case 'D': /* NULL date fields have value "00000000" */ return strncmp(pszValue,"00000000",8) == 0; case 'L': /* NULL boolean fields have value "?" */ return pszValue[0] == '?'; default: /* empty string fields are considered NULL */ return strlen(pszValue) == 0; } }
bool ShapelibProxy::getFieldAttributes(const ShapelibHandle &handle, std::string &name, std::string &type, std::string &value, int field, int feature) { char cname[12]; // defined as maximum length DBFFieldType fieldType = DBFGetFieldInfo(handle.getDbfHandle(), field, cname, NULL, NULL); name = cname; switch (fieldType) { case FTString: type = "String"; value = DBFReadStringAttribute(handle.getDbfHandle(), feature, field); break; case FTInteger: type = "Integer"; value = boost::lexical_cast<std::string>( DBFReadIntegerAttribute(handle.getDbfHandle(), feature, field)); break; case FTDouble: type = "Double"; value = boost::lexical_cast<std::string>( DBFReadDoubleAttribute(handle.getDbfHandle(), feature, field)); break; default: type = "Unknown"; value = "[Error: Not stringable]"; return false; } return true; }
inline Tags parseTags(DBFHandle dbfFile, int k) const { char title[12]; int fieldCount = DBFGetFieldCount(dbfFile); Tags tags; tags.reserve(fieldCount); for (int i = 0; i < fieldCount; i++) { if (DBFIsAttributeNULL(dbfFile, k, i)) continue; utymap::formats::Tag tag; int width, decimals; DBFFieldType eType = DBFGetFieldInfo(dbfFile, i, title, &width, &decimals); tag.key = std::string(title); { switch (eType) { case FTString: tag.value = DBFReadStringAttribute(dbfFile, k, i); break; case FTInteger: tag.value = to_string(DBFReadIntegerAttribute(dbfFile, k, i)); break; case FTDouble: tag.value = to_string(DBFReadDoubleAttribute(dbfFile, k, i)); break; default: break; } } tags.push_back(tag); } return std::move(tags); }
bool compareDBF(DBFHandle iDBF, int iRecord, DBFHandle jDBF, int jRecord, bool print) { int i,j; int temp; bool result; DBFFieldType eType; char szTitle[12]; // Column heading int nWidth, nDecimals; result = false; for( i = 0; i < DBFGetFieldCount(iDBF); i++ ) { j = columnMatchArray[i]; if( j < 0 ) continue; eType = DBFGetFieldInfo( iDBF, i, szTitle, &nWidth, &nDecimals ); assert( nWidth < BUFFER_MAX ); memcpy( &iBuffer, DBFReadStringAttribute( iDBF, iRecord, i ), nWidth ); memcpy( &jBuffer, DBFReadStringAttribute( jDBF, jRecord, j ), nWidth ); /* Convert numbers, compare everything else as a string */ if( eType == FTDouble ) temp = (atof( iBuffer ) != atof( jBuffer ) ); else if( eType == FTInteger ) temp = (atoi( iBuffer ) != atoi( jBuffer ) ); else temp = memcmp ( &iBuffer, &jBuffer, nWidth ); /* Unless they are both NULL, flag record as different */ if( temp ) { result = true; if( DBFIsAttributeNULL( iDBF, iRecord, i ) && DBFIsAttributeNULL( jDBF, jRecord, j ) ) result = false; } if( temp && print ) { //printf("%d wide record %d and %d differ (etype=%d)\n", nWidth, iRecord, jRecord, eType ); iBuffer[ nWidth ] = 0; jBuffer[ nWidth ] = 0; printf("%s: %s >>> %s\n", szTitle, &iBuffer, &jBuffer); } } return( result ); }
void dibSHP::readAttributes(DBFHandle dh, int i){ if (layerF >= 0) { attdata.layer = DBFReadStringAttribute( dh, i, layerF ); currDoc->setLayer(attdata.layer); } /* if (colorF >= 0) readColor(dh, i); if (ltypeF >= 0) readLType(dh, i); if (lwidthF >= 0) readWidth(dh, i);*/ }
void dibSHP::readPoint(DBFHandle dh, int i){ Plug_Entity *ent =NULL; QHash<int, QVariant> data; if (pointF < 0) { ent = currDoc->newEntity(DPI::POINT); ent->getData(&data); } else { ent = currDoc->newEntity(DPI::TEXT); ent->getData(&data); data.insert(DPI::TEXTCONTENT, DBFReadStringAttribute( dh, i, pointF ) ); } data.insert(DPI::STARTX, *(sobject->padfX)); data.insert(DPI::STARTY, *(sobject->padfY)); readAttributes(dh, i); data.insert(DPI::LAYER, attdata.layer); ent->updateData(&data); currDoc->addEntity(ent); }
// --------------------------------------------------------------------------- // // ----------- int bDBFTable::ReadVal(int o, int f, void* val){ int sgn; (void)FieldSign(f,&sgn); switch(sgn){ case _int: (*(int*)val)=DBFReadIntegerAttribute(_dbf,o-1,f-1); break; case _double: (*(double*)val)=DBFReadDoubleAttribute(_dbf,o-1,f-1); break; case _char: strcpy((char*)val,DBFReadStringAttribute(_dbf,o-1,f-1)); break; default: return(-1); } return(0); }
// TODO: separate tag parsing void buildNodes(xmlNodePtr root_node, MULTIPOINT pts, SHAPE *shape) { int i, k = 0; int l; char val[1024]; xmlNodePtr osmNode; NODE node; KEYVAL tags; initList(&tags); int j = 0 - pts.num_points; // negative ids required for new osm nodes for (i = -1; i > j; i--) { node.id = i; node.x = pts.points[k].x; node.y = pts.points[k].y; osmNode = nodeElement(node); if (shape->num_fields > 0) { // has tags for (l = 0; l < shape->num_fields; l++) { setKey(&tags, getValue(&shape->fields[l])); // set tag value snprintf(val, 1024, "%s", DBFReadStringAttribute(shape->handleDbf, k, l)); setValue(&tags, val); xmlAddChild(osmNode, tagElement(&tags)); } } xmlAddChild(root_node, osmNode); k++; } }
static Tcl_Obj * NewAttributeObj (DBFHandle const dbfHandle, int const record, int const field) { if (DBFIsAttributeNULL (dbfHandle, record, field)) { return Tcl_NewObj (); } switch (DBFGetFieldInfo (dbfHandle, field, NULL, NULL, NULL)) { case FTInteger: return Tcl_NewIntObj (DBFReadIntegerAttribute (dbfHandle, record, field)); case FTDouble: return Tcl_NewDoubleObj (DBFReadDoubleAttribute (dbfHandle, record, field)); default: return Tcl_NewStringObj (DBFReadStringAttribute (dbfHandle, record, field), -1); } }
int SHPAPI_CALL DBFIsAttributeNULL( DBFHandle psDBF, int iRecord, int iField ) { const char *pszValue; int i; pszValue = DBFReadStringAttribute( psDBF, iRecord, iField ); if(pszValue == NULL) return TRUE; switch(psDBF->pachFieldType[iField]) { case 'N': case 'F': /* ** We accept all asterisks or all blanks as NULL ** though according to the spec I think it should be all ** asterisks. */ if(pszValue[0] == '*') return TRUE; for(i = 0; pszValue[i] != '\0'; i++) if(pszValue[i] != ' ') return FALSE; return TRUE; case 'D': /* NULL date fields have value "00000000" */ return strlen(pszValue) == 0 || strncmp(pszValue,"00000000",8) == 0; case 'L': /* NULL boolean fields have value "?" */ return pszValue[0] == '?'; default: /* empty string fields are considered NULL */ return strlen(pszValue) == 0; } }
static void SetAttributeObj (Tcl_Obj * const objPtr, DBFHandle const dbfHandle, int const record, int const field) { if (DBFIsAttributeNULL (dbfHandle, record, field)) { return; } switch (DBFGetFieldInfo (dbfHandle, field, NULL, NULL, NULL)) { case FTInteger: Tcl_SetIntObj (objPtr, DBFReadIntegerAttribute (dbfHandle, record, field)); break; case FTDouble: Tcl_SetDoubleObj (objPtr, DBFReadDoubleAttribute (dbfHandle, record, field)); break; default: Tcl_SetStringObj (objPtr, DBFReadStringAttribute (dbfHandle, record, field), -1); } }
strings_v shape_load_names(const char* filename, const char* colname) { DBFHandle hDBF; strings_v col; kv_init(col); hDBF = DBFOpen( filename, "rb" ); if( hDBF == NULL ) { fprintf(stderr, "DBFOpen(%s,\"r\") failed.\n", filename ); return col; } uint32_t fid = DBFGetFieldIndex(hDBF, colname); for(uint32_t i = 0; i < DBFGetRecordCount(hDBF); i++ ) { char* str = (char *) DBFReadStringAttribute(hDBF, i, fid); if(str != NULL) kv_push(char*, col, strdup(str)); else kv_push(char*, col, NULL); } DBFClose( hDBF ); return col; }
/* -------------------------------------------------------------------- */ void printDBF(DBFHandle iDBF, int iRecord) { int i; char szTitle[12]; // Column heading int nWidth, nDecimals; char szFormat[32]; for( i = 0; i < DBFGetFieldCount(iDBF); i++ ) { DBFFieldType eType; eType = DBFGetFieldInfo( iDBF, i, szTitle, &nWidth, &nDecimals ); if( bMultiLine ) { printf( "\n%s: ", szTitle ); } else { printf("|"); } sprintf( szFormat, "%%-%ds", nWidth ); printf( szFormat, DBFReadStringAttribute( iDBF, iRecord, i ) ); } printf("\n"); }
std::string dbf_get_string_by_field(DBFHandle handle, int row, const char *field_name) { return DBFReadStringAttribute(handle, row, dbf_get_field_index(handle, row, field_name)); }
int main( int argc, char ** argv ) { DBFHandle hDBF; int *panWidth, i, iRecord; char szFormat[32], *pszFilename = NULL; int nWidth, nDecimals; int bHeader = 0; int bRaw = 0; int bMultiLine = 0; char szTitle[12]; /* -------------------------------------------------------------------- */ /* Handle arguments. */ /* -------------------------------------------------------------------- */ for( i = 1; i < argc; i++ ) { if( strcmp(argv[i],"-h") == 0 ) bHeader = 1; else if( strcmp(argv[i],"-r") == 0 ) bRaw = 1; else if( strcmp(argv[i],"-m") == 0 ) bMultiLine = 1; else pszFilename = argv[i]; } /* -------------------------------------------------------------------- */ /* Display a usage message. */ /* -------------------------------------------------------------------- */ if( pszFilename == NULL ) { printf( "dbfdump [-h] [-r] [-m] xbase_file\n" ); printf( " -h: Write header info (field descriptions)\n" ); printf( " -r: Write raw field info, numeric values not reformatted\n" ); printf( " -m: Multiline, one line per field.\n" ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Open the file. */ /* -------------------------------------------------------------------- */ hDBF = DBFOpen( pszFilename, "rb" ); if( hDBF == NULL ) { printf( "DBFOpen(%s,\"r\") failed.\n", argv[1] ); exit( 2 ); } /* -------------------------------------------------------------------- */ /* If there is no data in this file let the user know. */ /* -------------------------------------------------------------------- */ if( DBFGetFieldCount(hDBF) == 0 ) { printf( "There are no fields in this table!\n" ); exit( 3 ); } /* -------------------------------------------------------------------- */ /* Dump header definitions. */ /* -------------------------------------------------------------------- */ if( bHeader ) { for( i = 0; i < DBFGetFieldCount(hDBF); i++ ) { DBFFieldType eType; const char *pszTypeName; eType = DBFGetFieldInfo( hDBF, i, szTitle, &nWidth, &nDecimals ); if( eType == FTString ) pszTypeName = "String"; else if( eType == FTInteger ) pszTypeName = "Integer"; else if( eType == FTDouble ) pszTypeName = "Double"; else if( eType == FTInvalid ) pszTypeName = "Invalid"; printf( "Field %d: Type=%s, Title=`%s', Width=%d, Decimals=%d\n", i, pszTypeName, szTitle, nWidth, nDecimals ); } } /* -------------------------------------------------------------------- */ /* Compute offsets to use when printing each of the field */ /* values. We make each field as wide as the field title+1, or */ /* the field value + 1. */ /* -------------------------------------------------------------------- */ panWidth = (int *) malloc( DBFGetFieldCount( hDBF ) * sizeof(int) ); for( i = 0; i < DBFGetFieldCount(hDBF) && !bMultiLine; i++ ) { DBFFieldType eType; eType = DBFGetFieldInfo( hDBF, i, szTitle, &nWidth, &nDecimals ); if( strlen(szTitle) > nWidth ) panWidth[i] = strlen(szTitle); else panWidth[i] = nWidth; if( eType == FTString ) sprintf( szFormat, "%%-%ds ", panWidth[i] ); else sprintf( szFormat, "%%%ds ", panWidth[i] ); printf( szFormat, szTitle ); } printf( "\n" ); /* -------------------------------------------------------------------- */ /* Read all the records */ /* -------------------------------------------------------------------- */ for( iRecord = 0; iRecord < DBFGetRecordCount(hDBF); iRecord++ ) { if( bMultiLine ) printf( "Record: %d\n", iRecord ); for( i = 0; i < DBFGetFieldCount(hDBF); i++ ) { DBFFieldType eType; eType = DBFGetFieldInfo( hDBF, i, szTitle, &nWidth, &nDecimals ); if( bMultiLine ) { printf( "%s: ", szTitle ); } /* -------------------------------------------------------------------- */ /* Print the record according to the type and formatting */ /* information implicit in the DBF field description. */ /* -------------------------------------------------------------------- */ if( !bRaw ) { if( DBFIsAttributeNULL( hDBF, iRecord, i ) ) { if( eType == FTString ) sprintf( szFormat, "%%-%ds", nWidth ); else sprintf( szFormat, "%%%ds", nWidth ); printf( szFormat, "(NULL)" ); } else { switch( eType ) { case FTString: sprintf( szFormat, "%%-%ds", nWidth ); printf( szFormat, DBFReadStringAttribute( hDBF, iRecord, i ) ); break; case FTInteger: sprintf( szFormat, "%%%dd", nWidth ); printf( szFormat, DBFReadIntegerAttribute( hDBF, iRecord, i ) ); break; case FTDouble: sprintf( szFormat, "%%%d.%dlf", nWidth, nDecimals ); printf( szFormat, DBFReadDoubleAttribute( hDBF, iRecord, i ) ); break; default: break; } } } /* -------------------------------------------------------------------- */ /* Just dump in raw form (as formatted in the file). */ /* -------------------------------------------------------------------- */ else { sprintf( szFormat, "%%-%ds", nWidth ); printf( szFormat, DBFReadStringAttribute( hDBF, iRecord, i ) ); } /* -------------------------------------------------------------------- */ /* Write out any extra spaces required to pad out the field */ /* width. */ /* -------------------------------------------------------------------- */ if( !bMultiLine ) { sprintf( szFormat, "%%%ds", panWidth[i] - nWidth + 1 ); printf( szFormat, "" ); } if( bMultiLine ) printf( "\n" ); fflush( stdout ); } printf( "\n" ); } DBFClose( hDBF ); return( 0 ); }
bool writeAdminRegionsToDatabase(QString const &a0_dbf, QString const &a1_dbf, Kompex::SQLiteStatement * pStmt) { // because shapefiles are evil QTextCodec * codec = QTextCodec::codecForName("windows-1252"); // populate the admin0 temp table { DBFHandle a0_hDBF = DBFOpen(a0_dbf.toLocal8Bit().data(),"rb"); if(a0_hDBF == NULL) { qDebug() << "ERROR: Could not open admin0 dbf file"; return -1; } size_t a0_numRecords = DBFGetRecordCount(a0_hDBF); if(a0_numRecords == 0) { qDebug() << "ERROR: admin0 dbf file has no records!"; return -1; } size_t a0_idx_adm_name = DBFGetFieldIndex(a0_hDBF,"name"); size_t a0_idx_adm_a3 = DBFGetFieldIndex(a0_hDBF,"adm0_a3"); size_t a0_idx_sov_name = DBFGetFieldIndex(a0_hDBF,"sovereignt"); size_t a0_idx_sov_a3 = DBFGetFieldIndex(a0_hDBF,"sov_a3"); size_t a0_idx_type = DBFGetFieldIndex(a0_hDBF,"type"); size_t a0_idx_note = DBFGetFieldIndex(a0_hDBF,"note_adm0"); // create a temporary table we can use to lookup // the admin0 data we want for each admin1 entry pStmt->SqlStatement("CREATE TABLE IF NOT EXISTS temp(" "id INTEGER PRIMARY KEY NOT NULL UNIQUE," "adm_a3 TEXT NOT NULL UNIQUE," "sov_a3 TEXT NOT NULL," "adm_name TEXT," "sov_name TEXT," "type TEXT," "note TEXT);"); pStmt->BeginTransaction(); for(size_t i=0; i < a0_numRecords; i++) { QString s0 = QString::number(i,10); QString s1(codec->toUnicode(DBFReadStringAttribute(a0_hDBF, i, a0_idx_adm_a3))); QString s2(codec->toUnicode(DBFReadStringAttribute(a0_hDBF, i, a0_idx_sov_a3))); QString s3(codec->toUnicode(DBFReadStringAttribute(a0_hDBF, i, a0_idx_adm_name))); QString s4(codec->toUnicode(DBFReadStringAttribute(a0_hDBF, i, a0_idx_sov_name))); QString s5(codec->toUnicode(DBFReadStringAttribute(a0_hDBF, i, a0_idx_type))); QString s6(codec->toUnicode(DBFReadStringAttribute(a0_hDBF, i, a0_idx_note))); QString stmt("INSERT INTO temp(" "id," "adm_a3," "sov_a3," "adm_name," "sov_name," "type," "note) VALUES(" + s0 + "," + "\"" + s1 + "\"," "\"" + s2 + "\"," "\"" + s3 + "\"," "\"" + s4 + "\"," "\"" + s5 + "\"," "\"" + s6 + "\");"); pStmt->SqlStatement(stmt.toStdString()); } pStmt->CommitTransaction(); DBFClose(a0_hDBF); } // populate the admin1 table { DBFHandle a1_hDBF = DBFOpen(a1_dbf.toLocal8Bit().data(),"rb"); if(a1_hDBF == NULL) { qDebug() << "ERROR: Could not open admin1 dbf file"; return false; } size_t a1_numRecords = DBFGetRecordCount(a1_hDBF); if(a1_numRecords == 0) { qDebug() << "ERROR: admin1 dbf file has no records!"; return false; } // open admin1 translation csv if it exists QStringList listAdmin1Subs; QString pathSubs = a1_dbf; pathSubs.chop(4); pathSubs.append("_translations.dat"); bool admin1_csv_sub = getAdmin1TranslationSubs(pathSubs,listAdmin1Subs); if(admin1_csv_sub) { if(listAdmin1Subs.size() == a1_numRecords) { qDebug() << "INFO: Using translation substitute file: "<< pathSubs; } else { qDebug() << "WARN: Translation file has wrong number " "of entries: " << listAdmin1Subs.size(); admin1_csv_sub = false; } } size_t a1_idx_adm_name = DBFGetFieldIndex(a1_hDBF,"name"); size_t a1_idx_adm_a3 = DBFGetFieldIndex(a1_hDBF,"sr_adm0_a3"); size_t a1_idx_sov_a3 = DBFGetFieldIndex(a1_hDBF,"sr_sov_a3"); size_t a1_idx_fclass = DBFGetFieldIndex(a1_hDBF,"featurecla"); size_t a1_idx_adminname = DBFGetFieldIndex(a1_hDBF,"admin"); QStringList listSqlSaveSov; QStringList listSqlSaveAdmin0; QStringList listSqlSaveAdmin1; for(size_t i=0; i < a1_numRecords; i++) { // get the name of this admin1 entry QString admin1_idx = QString::number(i,10); QString admin1_name(codec->toUnicode(DBFReadStringAttribute(a1_hDBF, i, a1_idx_adm_name))); // if the adm1 fclass is an aggregation, minor island or // remainder, we grab the name from another field which // doesn't contain a bunch of additional metadata QString fclass(codec->toUnicode(DBFReadStringAttribute(a1_hDBF, i, a1_idx_fclass))); if(fclass.contains("aggregation") || fclass.contains("minor island") || fclass.contains("remainder")) { admin1_name = QString(codec->toUnicode(DBFReadStringAttribute( a1_hDBF, i, a1_idx_adminname))); } else { // if there's no special feature class than we check // to see if there's a translation substitute available if(admin1_csv_sub && (listAdmin1Subs[i].size() > 0)) { admin1_name = listAdmin1Subs[i]; } } // get the adm_a3,sov_a3 code for this admin1 entry QString adm_a3(codec->toUnicode(DBFReadStringAttribute(a1_hDBF, i, a1_idx_adm_a3))); QString sov_a3(codec->toUnicode(DBFReadStringAttribute(a1_hDBF, i, a1_idx_sov_a3))); // check if the adm_a3 code exists in the temp database QString stmt("SELECT * FROM temp WHERE adm_a3=\""+ adm_a3 + "\";"); pStmt->Sql(stmt.toStdString()); if(pStmt->FetchRow()) { // save admin0 info QString admin0_idx = QString::number(pStmt->GetColumnInt("id"),10); QString admin0_type = QString::fromStdString(pStmt->GetColumnString("type")); QString admin0_note = QString::fromStdString(pStmt->GetColumnString("note")); pStmt->FreeQuery(); // save admin1 info; we currently derive the // disputed field from admin0 type and note fields QString admin1_disputed = "0"; if(admin0_type.contains("Disputed") || admin0_note.contains("Disputed")) { admin1_disputed = "1"; } stmt = QString("INSERT INTO admin1(id,name,disputed,admin0,sov) VALUES(" + admin1_idx + ",\"" + admin1_name + "\"," + admin1_disputed + "," + admin0_idx + "," + admin0_idx + ");"); listSqlSaveAdmin1.push_back(stmt); } else { pStmt->FreeQuery(); // if there isn't a matching adm_a3 code in the temp // database try to get the sovereign state instead stmt = QString("SELECT * FROM temp WHERE sov_a3=\""+ sov_a3 + "\";"); pStmt->Sql(stmt.toStdString()); if(pStmt->FetchRow()) { QString sov_idx = QString::number(pStmt->GetColumnInt("id")); pStmt->FreeQuery(); // since there's no true corresponding entry for // the admin1 region through the adm_a3 code, we // can't test for disputed regions QString admin1_disputed = "0"; // to indicate that no admin0 region data exists // for this entry, we use an index of value -1 QString admin0_idx = "-1"; stmt = QString("INSERT INTO admin1(id,name,disputed,admin0,sov) VALUES(" + admin1_idx + ",\"" + admin1_name + "\"," + admin1_disputed + "," + admin0_idx + "," + sov_idx + ");"); listSqlSaveAdmin1.push_back(stmt); } else { // fail without a matching adm_a3 or sov_a3 pStmt->FreeQuery(); return false; } } } // populate the admin0 and sov tables { QString stmt("SELECT * FROM temp;"); pStmt->Sql(stmt.toStdString()); while(pStmt->FetchRow()) { QString idx = QString::number(pStmt->GetColumnInt("id"),10); QString admin0_name = QString::fromStdString(pStmt->GetColumnString("adm_name")); QString sov_name = QString::fromStdString(pStmt->GetColumnString("sov_name")); stmt = QString("INSERT INTO sov(id,name) VALUES("+ idx + ",\"" + sov_name + "\");"); listSqlSaveSov.push_back(stmt); stmt = QString("INSERT INTO admin0(id,name) VALUES("+ idx + ",\"" + admin0_name + "\");"); listSqlSaveAdmin0.push_back(stmt); } pStmt->FreeQuery(); } // write prepared statements pStmt->BeginTransaction(); for(int i=0; i < listSqlSaveSov.size(); i++) { pStmt->SqlStatement(listSqlSaveSov[i].toUtf8().data()); } pStmt->CommitTransaction(); pStmt->BeginTransaction(); for(int i=0; i < listSqlSaveAdmin0.size(); i++) { pStmt->SqlStatement(listSqlSaveAdmin0[i].toUtf8().data()); } pStmt->CommitTransaction(); pStmt->BeginTransaction(); for(int i=0; i < listSqlSaveAdmin1.size(); i++) { pStmt->SqlStatement(listSqlSaveAdmin1[i].toUtf8().data()); } pStmt->CommitTransaction(); } // delete temp table pStmt->SqlStatement("DROP TABLE temp;"); return true; }
//---------------------------------------------------------------------------- int ShapefileReader::RequestData( vtkInformation* vtkNotUsed( request ), vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* outputVector) { vtkInformation* outInfo = outputVector->GetInformationObject(0); vtkPolyData* polydata = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())); if (illegalFileName()){ return 0; } if (incompleteSrcFiles()){ return 0; } SHPHandle shpHandle = SHPOpen(this->FileName, "rb"); int fileType = 0; if (shpHandle == NULL){ vtkErrorMacro("shp file read failed."); return 0; } vtkDebugMacro("file type: " << shpHandle->nShapeType); switch (shpHandle->nShapeType) { case SHPT_NULL: vtkErrorMacro("NULL Object type." << this->FileName); SHPClose(shpHandle); return 0; break; case SHPT_POINT: case SHPT_POINTZ: case SHPT_POINTM: case SHPT_MULTIPOINT: case SHPT_MULTIPOINTZ: case SHPT_MULTIPOINTM: fileType = POINTOBJ; break; case SHPT_ARC: case SHPT_ARCZ: case SHPT_ARCM: fileType = LINEOBJ; break; case SHPT_POLYGON: case SHPT_POLYGONZ: case SHPT_POLYGONM: fileType = FACEOBJ; break; case SHPT_MULTIPATCH: fileType = MESHOBJ; break; default: vtkErrorMacro("Unknown Object type." << this->FileName); SHPClose(shpHandle); return 0; } int pCount=0, pStart=0, pEnd=0; int vTracker = 0, pid=0; SHPObject* shpObj; vtkSmartPointer<vtkPoints> pts = vtkSmartPointer<vtkPoints>::New(); vtkSmartPointer<vtkCellArray> polys = vtkSmartPointer<vtkCellArray>::New(); vtkSmartPointer<vtkCellArray> strips = vtkSmartPointer<vtkCellArray>::New(); vtkSmartPointer<vtkCellArray> pLines = vtkSmartPointer<vtkCellArray>::New(); vtkSmartPointer<vtkCellArray> verts = vtkSmartPointer<vtkCellArray>::New(); vtkSmartPointer<vtkTriangleFilter> tFilter = vtkSmartPointer<vtkTriangleFilter>::New(); vtkSmartPointer<vtkStripper> stripper = vtkSmartPointer<vtkStripper>::New(); vtkSmartPointer<vtkPolyData> pdRaw = vtkSmartPointer<vtkPolyData>::New(); vtkSmartPointer<vtkPolyData> pdRefined = vtkSmartPointer<vtkPolyData>::New(); //looping through all records in file for (int rIndex = 0; rIndex < shpHandle->nRecords; rIndex++){ shpObj = SHPReadObject(shpHandle, rIndex); //making sure shp object type is consistent with file type if (shpObj->nSHPType != shpHandle->nShapeType){ vtkErrorMacro("Inconsistent shape type of record: " << rIndex << " in file " << this->FileName); continue; } pCount = shpObj->nParts; switch (fileType){ case POINTOBJ:{ vtkIdType *ids = new vtkIdType[shpObj->nVertices]; for (int vIndex = 0; vIndex < shpObj->nVertices; vIndex++){ pts->InsertPoint(pid,shpObj->padfX[vIndex], shpObj->padfY[vIndex], shpObj->padfZ[vIndex]); ids[vIndex] = pid++; } verts->InsertNextCell(shpObj->nVertices, ids); delete ids; ids = NULL; break; } case LINEOBJ:{ for (int pIndex = 0; pIndex < pCount; pIndex++){ pStart = shpObj->panPartStart[pIndex]; if (pIndex == (pCount-1)){ pEnd = shpObj->nVertices; }else{ pEnd = shpObj->panPartStart[pIndex+1]; } vtkSmartPointer<vtkPolyLine> pLine = vtkSmartPointer<vtkPolyLine>::New(); pLine->GetPointIds()->SetNumberOfIds(pEnd-pStart); for (int vIndex = pStart; vIndex < pEnd; vIndex++){ pts->InsertNextPoint(shpObj->padfX[vIndex], shpObj->padfY[vIndex], shpObj->padfZ[vIndex]); pLine->GetPointIds()->SetId(vIndex-pStart, vTracker+vIndex-pStart); } pLines->InsertNextCell(pLine); vTracker += pEnd-pStart; } break; } case FACEOBJ:{ for (int pIndex = 0; pIndex < pCount; pIndex++){ pStart = shpObj->panPartStart[pIndex]; if (pIndex == (pCount-1)){ pEnd = shpObj->nVertices; }else{ pEnd = shpObj->panPartStart[pIndex+1]; } vtkSmartPointer<vtkPolygon> poly = vtkSmartPointer<vtkPolygon>::New(); vtkSmartPointer<vtkPolyLine> pLine = vtkSmartPointer<vtkPolyLine>::New(); poly->GetPointIds()->SetNumberOfIds(pEnd-pStart); pLine->GetPointIds()->SetNumberOfIds(pEnd-pStart); for (int vIndex = pStart; vIndex < pEnd; vIndex++){ pts->InsertNextPoint(shpObj->padfX[vIndex], shpObj->padfY[vIndex], shpObj->padfZ[vIndex]); poly->GetPointIds()->SetId(vIndex-pStart, vTracker+vIndex-pStart); pLine->GetPointIds()->SetId(vIndex-pStart, vTracker+vIndex-pStart); } polys->InsertNextCell(poly); pLines->InsertNextCell(pLine); vTracker += pEnd-pStart; } break; } case MESHOBJ:{ switch (*(shpObj->panPartType)){ case SHPP_TRISTRIP:{ for (int pIndex = 0; pIndex < pCount; pIndex++){ pStart = shpObj->panPartStart[pIndex]; if (pIndex == (pCount-1)){ pEnd = shpObj->nVertices; }else{ pEnd = shpObj->panPartStart[pIndex+1]; } vtkSmartPointer<vtkTriangleStrip> strip = vtkSmartPointer<vtkTriangleStrip>::New(); strip->GetPointIds()->SetNumberOfIds(pEnd-pStart); for (int vIndex = pStart; vIndex < pEnd; vIndex++){ pts->InsertNextPoint(shpObj->padfX[vIndex], shpObj->padfY[vIndex], shpObj->padfZ[vIndex]); strip->GetPointIds()->SetId(vIndex-pStart, vTracker+vIndex-pStart); } strips->InsertNextCell(strip); vTracker += pEnd-pStart; } break; }//SHPP_TRISTRIP case SHPP_TRIFAN:{ for (int pIndex = 0; pIndex < pCount; pIndex++){ pStart = shpObj->panPartStart[pIndex]; if (pIndex == (pCount-1)){ pEnd = shpObj->nVertices; }else{ pEnd = shpObj->panPartStart[pIndex+1]; } vtkSmartPointer<vtkTriangleStrip> strip = vtkSmartPointer<vtkTriangleStrip>::New(); strip->GetPointIds()->SetNumberOfIds(pEnd-pStart); for (int vIndex = pStart; vIndex < pEnd; vIndex++){ pts->InsertNextPoint(shpObj->padfX[vIndex], shpObj->padfY[vIndex], shpObj->padfZ[vIndex]); strip->GetPointIds()->SetId(vIndex-pStart, vTracker+vIndex-pStart); } strips->InsertNextCell(strip); vTracker += pEnd-pStart; } break; }//SHPP_TRIFAN case SHPP_OUTERRING: case SHPP_INNERRING: case SHPP_FIRSTRING: case SHPP_RING:{ for (int pIndex = 0; pIndex < pCount; pIndex++){ pStart = shpObj->panPartStart[pIndex]; if (pIndex == (pCount-1)){ pEnd = shpObj->nVertices; }else{ pEnd = shpObj->panPartStart[pIndex+1]; } vtkSmartPointer<vtkPolygon> poly = vtkSmartPointer<vtkPolygon>::New(); vtkSmartPointer<vtkPolyLine> pLine = vtkSmartPointer<vtkPolyLine>::New(); poly->GetPointIds()->SetNumberOfIds(pEnd-pStart); pLine->GetPointIds()->SetNumberOfIds(pEnd-pStart); for (int vIndex = pStart; vIndex < pEnd; vIndex++){ pts->InsertNextPoint(shpObj->padfX[vIndex], shpObj->padfY[vIndex], shpObj->padfZ[vIndex]); poly->GetPointIds()->SetId(vIndex-pStart, vTracker+vIndex-pStart); pLine->GetPointIds()->SetId(vIndex-pStart, vTracker+vIndex-pStart); } polys->InsertNextCell(poly); pLines->InsertNextCell(pLine); vTracker += pEnd-pStart; } break; }//rings }//panPartType break; }//MESHOBJ }//fileType SHPDestroyObject(shpObj); }// rIndex SHPClose(shpHandle); DBFHandle dbfHandle = DBFOpen(this->FileName, "rb"); int fieldCount = DBFGetFieldCount(dbfHandle); for (int fieldIndex = 0; fieldIndex < fieldCount; fieldIndex++){ DBFFieldType fieldType; const char *typeName; char nativeFieldType; char pszFieldName[12]; int pnWidth=0, pnDecimals=0, recordCount=0; nativeFieldType = DBFGetNativeFieldType(dbfHandle, fieldIndex); fieldType = DBFGetFieldInfo(dbfHandle, fieldIndex, pszFieldName, &pnWidth, &pnDecimals); recordCount = DBFGetRecordCount(dbfHandle); vtkDebugMacro("record count: " << recordCount); switch (fieldType){ case FTString:{ vtkSmartPointer<vtkStringArray> cellData = vtkSmartPointer<vtkStringArray>::New(); cellData->SetName(pszFieldName); for (int recordIndex = 0; recordIndex < recordCount; recordIndex++){ if (DBFIsAttributeNULL(dbfHandle, recordIndex, fieldIndex)){ cellData->InsertNextValue("NULL"); }else{ cellData->InsertNextValue(DBFReadStringAttribute(dbfHandle, recordIndex, fieldIndex)); } } polydata->GetCellData()->AddArray(cellData); break; } case FTInteger:{ vtkSmartPointer<vtkIntArray> cellData = vtkSmartPointer<vtkIntArray>::New(); cellData->SetName(pszFieldName); for (int recordIndex = 0; recordIndex < recordCount; recordIndex++){ if (DBFIsAttributeNULL(dbfHandle, recordIndex, fieldIndex)){ cellData->InsertNextValue(0); }else{ cellData->InsertNextValue(DBFReadIntegerAttribute(dbfHandle, recordIndex, fieldIndex)); } } polydata->GetCellData()->AddArray(cellData); break; } case FTDouble:{ vtkSmartPointer<vtkDoubleArray> cellData = vtkSmartPointer<vtkDoubleArray>::New(); cellData->SetName(pszFieldName); for (int recordIndex = 0; recordIndex < recordCount; recordIndex++){ if (DBFIsAttributeNULL(dbfHandle, recordIndex, fieldIndex)){ cellData->InsertNextValue(0.0); }else{ cellData->InsertNextValue(DBFReadDoubleAttribute(dbfHandle, recordIndex, fieldIndex)); } } polydata->GetCellData()->AddArray(cellData); break; } case FTLogical:{ //what is this? lack of sample data! break; } case FTInvalid:{ vtkErrorMacro("Invalid DBF field type"); break; } } } DBFClose(dbfHandle); //pdRaw->SetPoints(pts); //pdRaw->SetPolys(polys); //pdRaw->SetLines(pLines); //pdRaw->SetVerts(verts); //tFilter->SetInput(pdRaw); //tFilter->Update(); //pdRefined = tFilter->GetOutput(); //polydata->SetPoints(pdRefined->GetPoints()); //polydata->SetPolys(pdRefined->GetPolys()); //polydata->SetLines(pdRefined->GetLines()); //polydata->SetVerts(pdRefined->GetVerts()); polydata->SetPoints(pts); polydata->SetLines(pLines); polydata->SetVerts(verts); polydata->SetStrips(strips); return 1; }
int load_table(int t) { int i, j, ncols, nrows, dbfcol; DBFHandle dbf; char *buf; ROW *rows; VALUE *val; G_debug(2, "load_table(): tab = %d", t); if (db.tables[t].loaded == TRUE) /*already loaded */ return DB_OK; dbf = DBFOpen(db.tables[t].file, "r"); if (dbf == NULL) { append_error("Cannot open dbf file.\n"); return DB_FAILED; } ncols = db.tables[t].ncols; nrows = DBFGetRecordCount(dbf); rows = db.tables[t].rows; rows = (ROW *) G_malloc(nrows * sizeof(ROW)); db.tables[t].arows = nrows; G_debug(2, " ncols = %d nrows = %d", ncols, nrows); for (i = 0; i < nrows; i++) { rows[i].alive = TRUE; rows[i].values = (VALUE *) G_calloc(ncols, sizeof(VALUE)); for (j = 0; j < ncols; j++) { val = &(rows[i].values[j]); dbfcol = j; val->is_null = DBFIsAttributeNULL(dbf, i, dbfcol); if (!(val->is_null)) { switch (db.tables[t].cols[j].type) { case DBF_INT: val->i = DBFReadIntegerAttribute(dbf, i, dbfcol); break; case DBF_CHAR: buf = (char *)DBFReadStringAttribute(dbf, i, dbfcol); save_string(val, buf); break; case DBF_DOUBLE: val->d = DBFReadDoubleAttribute(dbf, i, dbfcol); break; } } } } DBFClose(dbf); db.tables[t].rows = rows; db.tables[t].nrows = nrows; db.tables[t].loaded = TRUE; return DB_OK; }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) { /* Pointer to temporary matlab array */ const mxArray *mx; mxArray *m_shape_type; int nShapeType, nEntities, i, j, k, iPart, nFields, nStructElem, isPoint, firstInPoints = 1; int num_dbf_fields, num_dbf_records; /* number of DBF attributes, records */ int buflen; /* length of input shapefile name */ int status; /* success or failure */ char *shapefile; /* holder for input shapefile name */ const char *pszPartType = ""; /* Not used. */ double adfMinBound[4], adfMaxBound[4]; /* pointer to the shapefile */ SHPHandle hSHP; SHPObject *psShape; DBFHandle dbh; /* handle for DBF file */ /* This structure will hold a description of each field in the DBF file. */ typedef struct DBF_Field_Descriptor { char pszFieldName[12]; DBFFieldType field_type; } DBF_Field_Descriptor; DBF_Field_Descriptor *dbf_field; /* stores individual values from the DBF. */ int dbf_integer_val, dims[2], *p_parts_ptr, nNaNs, c, i_start, i_stop; char *dbf_char_val, error_buffer[500]; char *fnames[100]; /* holds name of fields */ mxArray *out_struct, *x_out, *y_out, *z_out, *bbox, *p_parts; double *x_out_ptr, *y_out_ptr, *z_out_ptr, *bb_ptr, nan, dbf_double_val; size_t sizebuf; /* Initialize the dbf record. */ dbf_field = NULL; /* Check for proper number of arguments */ if (nrhs != 1) mexErrMsgTxt("One input arguments are required."); if (nlhs != 2) mexErrMsgTxt("Two output arguments required."); /* Make sure the input is a proper string. */ if ( mxIsChar(prhs[0]) != 1 ) mexErrMsgTxt("Shapefile parameter must be a string\n" ); if ( mxGetM(prhs[0]) != 1 ) mexErrMsgTxt("Shapefile parameter must be a row vector, not a column string\n" ); buflen = mxGetN(prhs[0]) + 1; shapefile = mxCalloc( buflen, sizeof(char) ); /* copy the string data from prhs[0] into a C string. */ status = mxGetString( prhs[0], shapefile, buflen ); if ( status != 0 ) mxErrMsgTxt( "Not enough space for shapefile argument.\n" ); /* -------------------------------------------------------------------- */ /* Open the passed shapefile. */ /* -------------------------------------------------------------------- */ hSHP = SHPOpen( shapefile, "rb" ); if( hSHP == NULL ) mxErrMsgTxt( "Unable to open:%s\n", shapefile ); /* -------------------------------------------------------------------- */ /* Get the needed information about the shapefile. */ /* -------------------------------------------------------------------- */ SHPGetInfo( hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound ); /* Make sure that we can handle the type. */ switch ( nShapeType ) { case SHPT_POINT: break; case SHPT_POINTZ: break; case SHPT_ARC: break; case SHPT_ARCZ: break; case SHPT_POLYGON: break; case SHPT_POLYGONZ: break; case SHPT_MULTIPOINT: /* JL */ break; default: sprintf ( error_buffer, "Unhandled shape code %d (%s)", nShapeType, SHPTypeName ( nShapeType ) ); mexErrMsgTxt( error_buffer ); } /* Create the output shape type parameter. */ plhs[1] = mxCreateString ( SHPTypeName ( nShapeType ) ); /* Open the DBF, and retrieve the number of fields and records */ dbh = DBFOpen (shapefile, "rb"); num_dbf_fields = DBFGetFieldCount ( dbh ); num_dbf_records = DBFGetRecordCount ( dbh ); /* Allocate space for a description of each record, and populate it. * I allocate space for two extra "dummy" records that go in positions * 0 and 1. These I reserve for the xy data. */ nFields = 3; if ( (nShapeType == SHPT_POLYGONZ) || (nShapeType == SHPT_ARCZ) || (nShapeType == SHPT_POINTZ) ) nFields++; dbf_field = (DBF_Field_Descriptor *) mxMalloc ( (num_dbf_fields+nFields) * sizeof ( DBF_Field_Descriptor ) ); if ( dbf_field == NULL ) mexErrMsgTxt("Memory allocation for DBF_Field_Descriptor failed."); for ( j = 0; j < num_dbf_fields; ++j ) dbf_field[j+nFields].field_type = DBFGetFieldInfo ( dbh, j, dbf_field[j+nFields].pszFieldName, NULL, NULL ); fnames[0] = strdup ( "X" ); fnames[1] = strdup ( "Y" ); if ( (nShapeType == SHPT_POLYGONZ) || (nShapeType == SHPT_ARCZ) || (nShapeType == SHPT_POINTZ) ) { fnames[2] = strdup ( "Z" ); fnames[3] = strdup ( "BoundingBox" ); } else fnames[2] = strdup ( "BoundingBox" ); for ( j = 0; j < num_dbf_fields; ++j ) fnames[j+nFields] = strdup ( dbf_field[j+nFields].pszFieldName ); /* To hold information on eventual polygons with rings */ /*fnames[num_dbf_fields+3] = strdup ( "nParts" );*/ /*fnames[num_dbf_fields+4] = strdup ( "PartsIndex" );*/ /* Allocate space for the output structure. */ isPoint = ( nShapeType == SHPT_POINT || nShapeType == SHPT_POINTZ ) ? 1: 0; nStructElem = ( nShapeType == SHPT_POINT || nShapeType == SHPT_POINTZ ) ? 1: nEntities; out_struct = mxCreateStructMatrix ( nStructElem, 1, nFields + num_dbf_fields, (const char **)fnames ); /* create the BoundingBox */ dims[0] = 4; dims[1] = 2; bbox = mxCreateNumericArray ( 2, dims, mxDOUBLE_CLASS, mxREAL ); bb_ptr = mxGetData ( bbox ); for (i = 0; i < 4; i++) bb_ptr[i] = adfMinBound[i]; for (i = 0; i < 4; i++) bb_ptr[i+4] = adfMaxBound[i]; mxSetField ( out_struct, 0, "BoundingBox", bbox ); nan = mxGetNaN(); /* -------------------------------------------------------------------- */ /* Skim over the list of shapes, printing all the vertices. */ /* -------------------------------------------------------------------- */ for( i = 0; i < nEntities; i++ ) { psShape = SHPReadObject( hSHP, i ); /* Create the fields in this struct element. */ if ( !isPoint ) { nNaNs = psShape->nParts > 1 ? psShape->nParts : 0; dims[0] = psShape->nVertices + nNaNs; dims[1] = 1; x_out = mxCreateNumericArray ( 2, dims, mxDOUBLE_CLASS, mxREAL ); x_out_ptr = mxGetData ( x_out ); y_out = mxCreateNumericArray ( 2, dims, mxDOUBLE_CLASS, mxREAL ); y_out_ptr = mxGetData ( y_out ); if ( (nShapeType == SHPT_POLYGONZ) || (nShapeType == SHPT_POINTZ) || (nShapeType == SHPT_ARCZ)) { z_out = mxCreateNumericArray ( 2, dims, mxDOUBLE_CLASS, mxREAL ); z_out_ptr = mxGetData ( z_out ); } } else if (firstInPoints) { /* Allocate all memory we'll need */ x_out = mxCreateDoubleMatrix (nEntities, 1, mxREAL); x_out_ptr = mxGetPr ( x_out ); y_out = mxCreateDoubleMatrix (nEntities, 1, mxREAL); y_out_ptr = mxGetPr ( y_out ); if (nShapeType == SHPT_POINTZ) { z_out = mxCreateDoubleMatrix (nEntities, 1, mxREAL); z_out_ptr = mxGetPr ( z_out ); } firstInPoints = 0; } if (!isPoint && psShape->nParts > 1) { for (k = c = 0; k < psShape->nParts; k++) { i_start = psShape->panPartStart[k]; if (k < psShape->nParts - 1) i_stop = psShape->panPartStart[k+1]; else i_stop = psShape->nVertices; if ( nShapeType == SHPT_POLYGONZ ) { for (j = i_start; j < i_stop; c++, j++) { x_out_ptr[c] = psShape->padfX[j]; y_out_ptr[c] = psShape->padfY[j]; z_out_ptr[c] = psShape->padfZ[j]; } x_out_ptr[c] = nan; y_out_ptr[c] = nan; z_out_ptr[c] = nan; } else { for (j = i_start; j < i_stop; c++, j++) { x_out_ptr[c] = psShape->padfX[j]; y_out_ptr[c] = psShape->padfY[j]; } x_out_ptr[c] = nan; y_out_ptr[c] = nan; } c++; } } else if ( isPoint ) { x_out_ptr[i] = *psShape->padfX; y_out_ptr[i] = *psShape->padfY; if (nShapeType == SHPT_POINTZ) z_out_ptr[i] = *psShape->padfZ; if (i > 0) { SHPDestroyObject( psShape ); continue; } } else { /* Just copy the vertices over. */ sizebuf = mxGetElementSize ( x_out ) * psShape->nVertices; memcpy ( (void *) x_out_ptr, (void *) psShape->padfX, sizebuf ); memcpy ( (void *) y_out_ptr, (void *) psShape->padfY, sizebuf ); if ( (nShapeType == SHPT_POLYGONZ) || (nShapeType == SHPT_ARCZ)) memcpy ( (void *) z_out_ptr, (void *) psShape->padfZ, sizebuf ); } mxSetField ( out_struct, i, "X", x_out ); mxSetField ( out_struct, i, "Y", y_out ); if ( (nShapeType == SHPT_POLYGONZ) || (nShapeType == SHPT_ARCZ) ) mxSetField ( out_struct, i, "Z", z_out ); bbox = mxCreateNumericMatrix ( 4, 2, mxDOUBLE_CLASS, mxREAL ); bb_ptr = (double *)mxGetData ( bbox ); bb_ptr[0] = psShape->dfXMin; bb_ptr[1] = psShape->dfYMin; bb_ptr[2] = psShape->dfZMin; bb_ptr[3] = psShape->dfMMin; bb_ptr[4] = psShape->dfXMax; bb_ptr[5] = psShape->dfYMax; bb_ptr[6] = psShape->dfZMax; bb_ptr[7] = psShape->dfMMax; if (i > 0) /* First BB contains the ensemble extent */ mxSetField ( out_struct, i, "BoundingBox", bbox ); for ( j = 0; j < num_dbf_fields; ++j ) { switch ( dbf_field[j+nFields].field_type ) { case FTString: dbf_char_val = (char *) DBFReadStringAttribute ( dbh, i, j ); mxSetField ( out_struct, i, dbf_field[j+nFields].pszFieldName, mxCreateString ( dbf_char_val ) ); break; case FTDouble: dbf_double_val = DBFReadDoubleAttribute ( dbh, i, j ); mxSetField ( out_struct, i, dbf_field[j+nFields].pszFieldName, mxCreateDoubleScalar ( dbf_double_val ) ); break; case FTInteger: case FTLogical: dbf_integer_val = DBFReadIntegerAttribute ( dbh, i, j ); dbf_double_val = dbf_integer_val; mxSetField ( out_struct, i, dbf_field[j+nFields].pszFieldName, mxCreateDoubleScalar ( dbf_double_val ) ); break; default: sprintf ( error_buffer, "Unhandled code %d, shape %d, record %d\n", dbf_field[j+nFields].field_type, i, j ); mexErrMsgTxt("Unhandled code"); } } SHPDestroyObject( psShape ); } if ( isPoint ) { /* In this case we still need to "send the true data out" */ mxSetField ( out_struct, 0, "X", x_out ); mxSetField ( out_struct, 0, "Y", y_out ); if (nShapeType == SHPT_POINTZ) mxSetField ( out_struct, 0, "Z", z_out ); } /* Clean up, close up shop. */ SHPClose( hSHP ); DBFClose ( dbh ); if ( dbf_field != NULL ) mxFree ( (void *)dbf_field ); plhs[0] = out_struct; }
std::ostream& rspfShapeDatabase::print(std::ostream& out)const { if(isOpen()) { out << std::setw(15)<<setiosflags(std::ios::left) <<"DB filename:" << theFilename << std::endl << std::setw(15)<<setiosflags(std::ios::left) <<"records:" << theHandle->nRecords << std::endl << std::setw(15)<<setiosflags(std::ios::left) <<"fields:" << theHandle->nFields << std::endl; char name[1024] = {'\0'}; int width = 0; int decimals = 0; int iField = 0; std::vector<int> fieldWidths; for(iField = 0; iField < theHandle->nFields; ++iField) { DBFFieldType fieldType = DBFGetFieldInfo(theHandle, iField, name, &width, &decimals); rspfString s = "field " + rspfString::toString(iField+1) + " name:"; switch(fieldType) { case FTString: case FTInteger: case FTDouble: { out << std::setw(15) << setiosflags(std::ios::left) << s.c_str() << name << std::endl; break; } default: { out << std::setw(15) << setiosflags(std::ios::left) << s.c_str() << "INVALID"<<std::endl; break; } } } for(int iShape = 0; iShape < theHandle->nRecords; ++iShape) { for(iField = 0; iField < theHandle->nFields; ++iField) { DBFFieldType fieldType = DBFGetFieldInfo(theHandle, iField, name, &width, &decimals); rspfString key = "field"; key+=rspfString::toString(iField+1); key+=(rspfString(".")+name+":"); switch(fieldType) { case FTString: { out << std::setw(25) << setiosflags(std::ios::left) << key.c_str() << DBFReadStringAttribute(theHandle, iShape, iField) <<std::endl; break; } case FTInteger: { out << std::setw(25) << setiosflags(std::ios::left) << key.c_str() << DBFReadIntegerAttribute(theHandle, iShape, iField) << std::endl; break; } case FTDouble: { out << std::setw(25) << setiosflags(std::ios::left) << key.c_str() << DBFReadDoubleAttribute(theHandle, iShape, iField) << std::endl; break; } case FTLogical: { break; } case FTInvalid: { break; } } } out << "_________________________________"<<std::endl; } } return out; }
int main(int argc, char *argv[]) { QCoreApplication myApp(argc, argv); // check input args QStringList inputArgs = myApp.arguments(); if(inputArgs.size() < 2) { qDebug() << "Error: No dbf directory: "; qDebug() << "Pass the dbf directory as an argument: "; qDebug() << "./dbf2sqlite /my/dbfdir"; return -1; } // filter shapefile types QStringList dbfFilterList; dbfFilterList << "*.dbf"; // check for all files QDir dbfDir = inputArgs[1]; QStringList dbfDirList = dbfDir.entryList(dbfFilterList, QDir::Files); // get all file paths QString fileDbf; for(int i=0; i < dbfDirList.size(); i++) { if(dbfDirList[i].contains(".dbf")) { fileDbf = dbfDir.absoluteFilePath(dbfDirList[i]); } } // open the database file DBFHandle hDBF = DBFOpen(fileDbf.toLocal8Bit().data(),"rb"); if(hDBF == NULL) { qDebug() << "Error: Could not open dbf file"; return -1; } // set fields to keep based on data type QStringList listFieldsToKeep; QString dbFileName; if(ADMIN0) { dbFileName = "admin0.sqlite"; listFieldsToKeep << "ADMIN" // administrative name of country << "ADM0_A3"; // 3 letter abbreviatesion of admin name } if(ADMIN1) { dbFileName = "admin1.sqlite"; listFieldsToKeep << "OBJECTID" << "NAME_1" // Admin1 region name << "VARNAME_1" // Admin1 alt name (not very reliable) << "NL_NAME_1" // Admin1 region name in national language (not reliable) << "Postal"; // 2 Letter Postal Code (not reliable) } // get number of fields in db size_t numRecords = 0; numRecords = DBFGetRecordCount(hDBF); if(numRecords > 0) { qDebug() << "Info: DBF file has" << numRecords << "records"; } else { qDebug() << "Error: DBF file has no records!"; return -1; } // create sqlite database qDebug() << "Info: Creating SQLite Database..."; Kompex::SQLiteDatabase * pDatabase = new Kompex::SQLiteDatabase(dbFileName.toStdString(), SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,0); Kompex::SQLiteStatement * pStmt = new Kompex::SQLiteStatement(pDatabase); // create database schema (flat) if(ADMIN1) { qDebug() << "Info: Creating database schema for ADMIN1 profile"; pStmt->SqlStatement("CREATE TABLE IF NOT EXISTS data(" "regionid INTEGER PRIMARY KEY NOT NULL UNIQUE," "name TEXT NOT NULL," "code TEXT);"); // regionid <-> internel shape/record id [integer] size_t idx_objectid = DBFGetFieldIndex(hDBF,"OBJECTID"); // name <-> NAME_1 [text] size_t idx_name = DBFGetFieldIndex(hDBF,"NAME_1"); // code <-> Postal [text] size_t idx_code = DBFGetFieldIndex(hDBF,"Postal"); qDebug() << "Info: Writing records to database..."; pStmt->BeginTransaction(); for(size_t i=0; i < numRecords; i++) { QString record_id = QString::number(i+1); QString record_name(DBFReadStringAttribute(hDBF,i,idx_name)); QString record_code(DBFReadStringAttribute(hDBF,i,idx_code)); QString inStmt = "INSERT INTO data(regionid,name,code) VALUES(\"" + record_id + "\",\"" +record_name + "\",\"" + record_code + "\");"; pStmt->SqlStatement(inStmt.toUtf8().data()); // qDebug() << record_name; if(i % 1000 == 0) { qDebug() << "Info: Wrote" << i+1 << "/" << numRecords << "records"; } } pStmt->CommitTransaction(); qDebug() << "Info: Done!"; } // close dbf file DBFClose(hDBF); // clean up database delete pStmt; delete pDatabase; return 0; }
GValue* FeatureShp::GetValue(g_uint i) const { GValue* pValue = NULL; SHPHandle pSHPHandle = m_pFeatureClass->m_pshpHandle; DBFHandle pDBFHandle = m_pFeatureClass->m_pdbfHandle; //if(index == m_lFIDIndex) //{ // pValue = new GValue(m_fid); //} //else { if(pDBFHandle==NULL) return NULL; //int innerIndex = GetFieldInnerIndex(index); GField* pField = (GField*)m_pFeatureClass->GetFields()->GetField(i); if(pField==NULL) return NULL; const char* szFieldName = pField->GetName(); int innerIndex = ::DBFGetFieldIndex(pDBFHandle, szFieldName); switch(pField->GetType()) { case augeFieldTypeString: { const char* szTemp = DBFReadStringAttribute (pDBFHandle, m_fid, innerIndex); if(NULL == szTemp) { return NULL; } pValue = new GValue(szTemp); } break; case augeFieldTypeInt: { int val; val = ::DBFReadIntegerAttribute(pDBFHandle, m_fid, innerIndex); pValue = new GValue(val); } break; case augeFieldTypeDouble: { double val; val = ::DBFReadDoubleAttribute(pDBFHandle, m_fid, innerIndex); pValue = new GValue(val); } break; case augeFieldTypeBool: { const char* val; val = ::DBFReadLogicalAttribute(pDBFHandle, m_fid, innerIndex); if(g_stricmp(val, "N")==0) pValue = new GValue(false); else pValue = new GValue(true); } break; case augeFieldTypeTime: { //const char* val; //val = ::DBFReadDateAttribute(pDBFHandle, m_fid, innerIndex); //TIME_STRU timStru; //Parse(timStru, val); //pValue = new GValue(&timStru, true); } break; case augeFieldTypeNone: { } break; case augeFieldTypeGeometry: { if(m_pGeometry==NULL) { FeatureShp* pThis = (FeatureShp*)this; pThis->m_pGeometry = pThis->CreateGeometry(m_fid, m_pFeatureClass->m_pshpHandle); } if(m_pGeometry!=NULL) { pValue = new GValue(m_pGeometry); m_pGeometry->AddRef(); } } break; } } return pValue; }
SEXP Rdbfread(SEXP dbfnm) { DBFHandle hDBF; int i, iRecord, nflds, nrecs, nRvar, pc=0; char labelbuff[81]; const char *pszFilename = NULL; int nWidth, nDecimals, val; char szTitle[12], buf[2]; const char *p; DBFFieldType eType; SEXP df, tmp, varlabels, row_names, DataTypes; short *types; /* -------------------------------------------------------------------- */ /* Handle arguments. */ /* -------------------------------------------------------------------- */ pszFilename = CHAR(STRING_ELT(dbfnm, 0)); /* -------------------------------------------------------------------- */ /* Open the file. */ /* -------------------------------------------------------------------- */ hDBF = DBFOpen(pszFilename, "rb" ); if( hDBF == NULL ) error(_("unable to open DBF file")); /* -------------------------------------------------------------------- */ /* If there is no data in this file let the user know. */ /* -------------------------------------------------------------------- */ if( DBFGetFieldCount(hDBF) == 0 ) { DBFClose( hDBF ); error(_("no fields in DBF table")); } nRvar = 0; nflds = DBFGetFieldCount(hDBF); nrecs = DBFGetRecordCount(hDBF); types = (short *) R_alloc(nflds, sizeof(short)); PROTECT(DataTypes = allocVector(STRSXP, nflds)); pc++; for( i = 0; i < nflds; i++ ) { eType = DBFGetFieldInfo( hDBF, i, szTitle, &nWidth, &nDecimals ); switch(eType) { case FTString: types[i] = 1; nRvar++; break; case FTInteger: types[i] = 2; nRvar++; break; case FTDouble: types[i] = 3; nRvar++; break; case FTLogical: types[i] = 4; nRvar++; break; default: /* doesn't seem to be possible */ types[i] = 0; } buf[0] = hDBF->pachFieldType[i]; buf[1] = '\0'; SET_STRING_ELT(DataTypes, i, mkChar(buf)); } PROTECT(df = allocVector(VECSXP, nRvar)); pc++; PROTECT(varlabels = allocVector(STRSXP, nRvar)); pc++; for(i = 0, nRvar = 0; i < nflds; i++) { eType = DBFGetFieldInfo( hDBF, i, szTitle, &nWidth, &nDecimals ); switch(types[i]) { case 1: SET_VECTOR_ELT(df, nRvar, allocVector(STRSXP,nrecs)); break; case 2: SET_VECTOR_ELT(df, nRvar, allocVector(INTSXP,nrecs)); break; case 3: SET_VECTOR_ELT(df, nRvar, allocVector(REALSXP,nrecs)); break; case 4: SET_VECTOR_ELT(df, nRvar, allocVector(LGLSXP,nrecs)); break; default: continue; } SET_STRING_ELT(varlabels, nRvar, mkChar(szTitle)); nRvar++; } for(iRecord = 0; iRecord < nrecs; iRecord++) { nRvar = 0; for(i = 0; i < nflds; i++) switch(types[i]) { case 1: if( DBFIsAttributeNULL( hDBF, iRecord, i )) SET_STRING_ELT(VECTOR_ELT(df, nRvar), iRecord, NA_STRING); else SET_STRING_ELT(VECTOR_ELT(df, nRvar), iRecord, mkChar(DBFReadStringAttribute( hDBF, iRecord, i))); nRvar++; break; case 2: if( DBFIsAttributeNULL( hDBF, iRecord, i )) INTEGER(VECTOR_ELT(df, nRvar))[iRecord] = NA_INTEGER; else { double dtmp = DBFReadDoubleAttribute( hDBF, iRecord, i ); if((dtmp > 2147483647.0) || (dtmp < -2147483646.0)) { int ii, *it; double *r; /* allow for NA_INTEGER = -(2^31 -1)*/ PROTECT(tmp = VECTOR_ELT(df, nRvar)); it = INTEGER(tmp); SET_VECTOR_ELT(df, nRvar, allocVector(REALSXP, nrecs)); r = REAL(VECTOR_ELT(df, nRvar)); for (ii = 0; ii < iRecord; ii++) { int itmp = it[ii]; r[ii] = (itmp == NA_INTEGER) ? NA_REAL : itmp; } UNPROTECT(1); r[iRecord] = dtmp; types[i] = 3; } else INTEGER(VECTOR_ELT(df, nRvar))[iRecord] = (int) dtmp; } nRvar++; break; case 3: if( DBFIsAttributeNULL( hDBF, iRecord, i )) REAL(VECTOR_ELT(df, nRvar))[iRecord] = NA_REAL; else REAL(VECTOR_ELT(df, nRvar))[iRecord] = DBFReadDoubleAttribute( hDBF, iRecord, i ); nRvar++; break; case 4: if( DBFIsAttributeNULL( hDBF, iRecord, i )) LOGICAL(VECTOR_ELT(df, nRvar))[iRecord] = NA_LOGICAL; else { p = DBFReadStringAttribute( hDBF, iRecord, i ); switch(*p){ case 'f': case 'F': case 'n': case 'N': val = 0; break; case 't': case 'T': case 'y': case 'Y': val = 1; break; case '?': val = NA_LOGICAL; break; default: warning(_("value |%d| found in logical field"), *p); val = NA_LOGICAL; break; } LOGICAL(VECTOR_ELT(df, nRvar))[iRecord] = val; } nRvar++; break; default: break; } } DBFClose( hDBF ); PROTECT(tmp = mkString("data.frame")); pc++; setAttrib(df, R_ClassSymbol, tmp); setAttrib(df, R_NamesSymbol, varlabels); setAttrib(df, install("data_types"), DataTypes); PROTECT(row_names = allocVector(STRSXP, nrecs)); pc++; for (i = 0; i < nrecs; i++) { sprintf(labelbuff, "%d", i+1); SET_STRING_ELT(row_names, i, mkChar(labelbuff)); } setAttrib(df, R_RowNamesSymbol, row_names); UNPROTECT(pc); return(df); }
static struct DataStruct * build_index (SHPHandle shp, DBFHandle dbf) { struct DataStruct *data; SHPObject *feat; int i; int j; /* make array */ data = malloc (sizeof *data * nShapes); if (!data) { fputs("malloc failed!\n", stderr); exit(EXIT_FAILURE); } /* populate array */ for (i = 0; i < nShapes; i++) { data[i].value = malloc(sizeof data[0].value[0] * nFields); if (0 == data[i].value) { fputs("malloc failed!\n", stderr); exit(EXIT_FAILURE); } data[i].record = i; for (j = 0; j < nFields; j++) { data[i].value[j].null = 0; switch (fldType[j]) { case FIDType: data[i].value[j].u.i = i; break; case SHPType: feat = SHPReadObject(shp, i); switch (feat->nSHPType) { case SHPT_NULL: fprintf(stderr, "Shape %d is a null feature!\n", i); data[i].value[j].null = 1; break; case SHPT_POINT: case SHPT_POINTZ: case SHPT_POINTM: case SHPT_MULTIPOINT: case SHPT_MULTIPOINTZ: case SHPT_MULTIPOINTM: case SHPT_MULTIPATCH: /* Y-sort bounds */ data[i].value[j].u.d = feat->dfYMax; break; case SHPT_ARC: case SHPT_ARCZ: case SHPT_ARCM: data[i].value[j].u.d = shp_length(feat); break; case SHPT_POLYGON: case SHPT_POLYGONZ: case SHPT_POLYGONM: data[i].value[j].u.d = shp_area(feat); break; default: fputs("Can't sort on Shapefile feature type!\n", stderr); exit(EXIT_FAILURE); } SHPDestroyObject(feat); break; case FTString: data[i].value[j].null = DBFIsAttributeNULL(dbf, i, fldIdx[j]); if (!data[i].value[j].null) { data[i].value[j].u.s = dupstr(DBFReadStringAttribute(dbf, i, fldIdx[j])); } break; case FTInteger: case FTLogical: data[i].value[j].null = DBFIsAttributeNULL(dbf, i, fldIdx[j]); if (!data[i].value[j].null) { data[i].value[j].u.i = DBFReadIntegerAttribute(dbf, i, fldIdx[j]); } break; case FTDouble: data[i].value[j].null = DBFIsAttributeNULL(dbf, i, fldIdx[j]); if (!data[i].value[j].null) { data[i].value[j].u.d = DBFReadDoubleAttribute(dbf, i, fldIdx[j]); } break; } } } #ifdef DEBUG PrintDataStruct(data); fputs("build_index: sorting array\n", stdout); #endif qsort (data, nShapes, sizeof data[0], compare); #ifdef DEBUG PrintDataStruct(data); fputs("build_index: returning array\n", stdout); #endif return data; }
bool rspfShapeDatabase::getRecord(rspfShapeDatabaseRecord& result) { if(isOpen()&&( (theRecordNumber < theHandle->nRecords) )) { if(result.getNumberOfFields() != theHandle->nFields) { result.setNumberOfFields(theHandle->nFields); } char name[1024] = {'\0'}; int width = 0; int decimals = 0; int iField = 0; std::vector<int> fieldWidths; for(iField = 0; iField < theHandle->nFields; ++iField) { DBFFieldType fieldType = DBFGetFieldInfo(theHandle, iField, name, &width, &decimals); rspfShapeDatabaseField field; field.theName = name; field.theWidth = width; field.theDecimals = decimals; field.theFieldType = fieldType; rspfString key = "field"; key+=rspfString::toString(iField+1); key+=(rspfString(".")+name+":"); switch(fieldType) { case FTString: { field.theValue = DBFReadStringAttribute(theHandle, theRecordNumber, iField); break; } case FTInteger: { field.theValue = rspfString::toString(DBFReadIntegerAttribute(theHandle, theRecordNumber, iField)); break; } case FTDouble: { field.theValue = rspfString::toString(DBFReadDoubleAttribute(theHandle, theRecordNumber, iField)); break; } case FTLogical: { break; } case FTInvalid: { break; } } result.setField(field, iField); } return true; } return false; }
void parseLine(xmlNodePtr root_node, SHAPE *shape) { int i, l, m, v; int k = 0; int n = -1; int o = -1; int start_vertex, end_vertex; char val[1024]; xmlNodePtr osmWay; SHPObject *obj = NULL; NODE node; KEYVAL tags; for (i = 0; i < shape->num_entities; i++) { obj = SHPReadObject(shape->handleShp, k); for (m = 0; m < obj->nParts; m++) { osmWay = wayElement(o); if (m == obj->nParts-1) { // is linestring end_vertex = obj->nVertices; } else { // is multilinestring end_vertex = obj->panPartStart[m+1]; } start_vertex = obj->panPartStart[m]; for (v = start_vertex; v < end_vertex; v++) { node.id = n; node.x = obj->padfX[v]; node.y = obj->padfY[v]; xmlAddChild(root_node, nodeElement(node)); xmlAddChild(osmWay, nodeRef(node)); n--; } if (shape->num_fields > 0) { // has tags for (l = 0; l < shape->num_fields; l++) { setKey(&tags, shape->field_names[l]); // set tag value snprintf(val, 1024, "%s", DBFReadStringAttribute(shape->handleDbf, k, l)); setValue(&tags, val); xmlAddChild(osmWay, tagElement(&tags)); } } xmlAddChild(root_node, osmWay); o--; } k++; } }
int main(int argc, char **argv) { if (argc == 1) { printf("usage: shapefile_to_kml [FILENAME]\n"); exit(1); } DBFHandle d = DBFOpen(argv[1], "rb"); if (d == NULL) { printf("DBFOpen error (%s.dbf)\n", argv[1]); exit(1); } SHPHandle h = SHPOpen(argv[1], "rb"); if (h == NULL) { printf("SHPOpen error (%s.dbf)\n", argv[1]); exit(1); } char filename[60]; sprintf(filename, "%s.kml", argv[1]); printf("%s\n", filename); FILE *fp = fopen(filename, "w"); if (fp == NULL) { printf("fopen error\n"); exit(1); } int nRecordCount = DBFGetRecordCount(d); int nFieldCount = DBFGetFieldCount(d); printf("DBF has %d records (with %d fields)\n", nRecordCount, nFieldCount); fprintf(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); fprintf(fp, "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"); /*for (int i = 0 ; i < nFieldCount ; i++) { char pszFieldName[12]; int pnWidth; int pnDecimals; DBFFieldType ft = DBFGetFieldInfo(d, i, pszFieldName, &pnWidth, &pnDecimals); switch (ft) { case FTString: fprintf(fp, ", %s VARCHAR(%d)", pszFieldName, pnWidth); break; case FTInteger: fprintf(fp, ", %s INT", pszFieldName); break; case FTDouble: fprintf(fp, ", %s FLOAT(15,10)", pszFieldName); break; case FTLogical: break; case FTInvalid: break; } }*/ fprintf(fp, " <Document>\n"); int i; for (i = 0 ; i < nRecordCount ; i++) { fprintf(fp, " <Placemark>\n"); fprintf(fp, " <name>%s</name>\n", (char *)DBFReadStringAttribute(d, i, 2)); fprintf(fp, " <Polygon>\n"); fprintf(fp, " <extrude>1</extrude>\n"); fprintf(fp, " <altitudeMode>relativeToGround</altitudeMode>\n"); fprintf(fp, " <outerBoundaryIs>\n"); fprintf(fp, " <LinearRing>\n"); fprintf(fp, " <coordinates>\n"); SHPObject *psShape = SHPReadObject(h, i); int j, iPart; for (j = 0, iPart = 1; j < psShape->nVertices; j++) { fprintf(fp, "%f,%f,100\n", psShape->padfX[j], psShape->padfY[j]); } fprintf(fp, " </coordinates>\n"); fprintf(fp, " </LinearRing>\n"); fprintf(fp, " </outerBoundaryIs>\n"); fprintf(fp, " </Polygon>\n"); fprintf(fp, " </Placemark>\n"); } fprintf(fp, " </Document>\n"); /*int nShapeType; int nEntities; const char *pszPlus; double adfMinBound[4], adfMaxBound[4]; SHPGetInfo(h, &nEntities, &nShapeType, adfMinBound, adfMaxBound); printf("SHP has %d entities\n", nEntities); for (i = 0; i < nEntities; i++) { SHPObject *psShape = SHPReadObject(h, i); //fprintf(fp, "INSERT INTO edges (id) VALUES (%d);\n", i+1); int j, iPart; for (j = 0, iPart = 1; j < psShape->nVertices; j++) { const char *pszPartType = ""; if (j == 0 && psShape->nParts > 0) pszPartType = SHPPartTypeName(psShape->panPartType[0]); if (iPart < psShape->nParts && psShape->panPartStart[iPart] == j) { pszPartType = SHPPartTypeName(psShape->panPartType[iPart]); iPart++; pszPlus = "+"; } else pszPlus = " "; //if (j%500==0) // fprintf(fp, "%sINSERT INTO vertexes (edge_id, x, y) VALUES (", (j!=0 ? ");\n": "")); //else // fprintf(fp, "),("); //fprintf(fp, "%d, %f, %f", i+1, psShape->padfX[j], psShape->padfY[j]); } //fprintf(fp, ");\n"); SHPDestroyObject(psShape); }*/ fprintf(fp, "</kml>\n"); printf("all done\n"); fclose(fp); if (h != NULL) SHPClose(h); if (d != NULL) DBFClose(d); }
GeoDataDocument *ShpRunner::parseFile(const QString &fileName, DocumentRole role, QString &error) { QFileInfo fileinfo( fileName ); if (fileinfo.suffix().compare(QLatin1String("shp"), Qt::CaseInsensitive) != 0) { error = QStringLiteral("File %1 does not have a shp suffix").arg(fileName); mDebug() << error; return nullptr; } SHPHandle handle = SHPOpen( fileName.toStdString().c_str(), "rb" ); if ( !handle ) { error = QStringLiteral("Failed to read %1").arg(fileName); mDebug() << error; return nullptr; } int entities; int shapeType; SHPGetInfo( handle, &entities, &shapeType, NULL, NULL ); mDebug() << " SHP info " << entities << " Entities " << shapeType << " Shape Type "; DBFHandle dbfhandle; dbfhandle = DBFOpen( fileName.toStdString().c_str(), "rb"); int nameField = DBFGetFieldIndex( dbfhandle, "Name" ); int noteField = DBFGetFieldIndex( dbfhandle, "Note" ); int mapColorField = DBFGetFieldIndex( dbfhandle, "mapcolor13" ); GeoDataDocument *document = new GeoDataDocument; document->setDocumentRole( role ); if ( mapColorField != -1 ) { GeoDataSchema schema; schema.setId(QStringLiteral("default")); GeoDataSimpleField simpleField; simpleField.setName(QStringLiteral("mapcolor13")); simpleField.setType( GeoDataSimpleField::Double ); schema.addSimpleField( simpleField ); document->addSchema( schema ); } for ( int i=0; i< entities; ++i ) { GeoDataPlacemark *placemark = 0; placemark = new GeoDataPlacemark; document->append( placemark ); SHPObject *shape = SHPReadObject( handle, i ); if (nameField != -1) { const char* info = DBFReadStringAttribute( dbfhandle, i, nameField ); // TODO: defaults to utf-8 encoding, but could be also something else, optionally noted in a .cpg file placemark->setName( info ); mDebug() << "name " << placemark->name(); } if (noteField != -1) { const char* note = DBFReadStringAttribute( dbfhandle, i, noteField ); // TODO: defaults to utf-8 encoding, see comment for name placemark->setDescription( note ); mDebug() << "desc " << placemark->description(); } double mapColor = DBFReadDoubleAttribute( dbfhandle, i, mapColorField ); if ( mapColor ) { GeoDataStyle::Ptr style(new GeoDataStyle); if ( mapColor >= 0 && mapColor <=255 ) { quint8 colorIndex = quint8( mapColor ); style->polyStyle().setColorIndex( colorIndex ); } else { quint8 colorIndex = 0; // mapColor is undefined in this case style->polyStyle().setColorIndex( colorIndex ); } placemark->setStyle( style ); } switch ( shapeType ) { case SHPT_POINT: { GeoDataPoint *point = new GeoDataPoint( *shape->padfX, *shape->padfY, 0, GeoDataCoordinates::Degree ); placemark->setGeometry( point ); mDebug() << "point " << placemark->name(); break; } case SHPT_MULTIPOINT: { GeoDataMultiGeometry *geom = new GeoDataMultiGeometry; for( int j=0; j<shape->nVertices; ++j ) { geom->append( new GeoDataPoint( GeoDataCoordinates( shape->padfX[j], shape->padfY[j], 0, GeoDataCoordinates::Degree ) ) ); } placemark->setGeometry( geom ); mDebug() << "multipoint " << placemark->name(); break; } case SHPT_ARC: { if ( shape->nParts != 1 ) { GeoDataMultiGeometry *geom = new GeoDataMultiGeometry; for( int j=0; j<shape->nParts; ++j ) { GeoDataLineString *line = new GeoDataLineString; int itEnd = (j + 1 < shape->nParts) ? shape->panPartStart[j+1] : shape->nVertices; for( int k=shape->panPartStart[j]; k<itEnd; ++k ) { line->append( GeoDataCoordinates( shape->padfX[k], shape->padfY[k], 0, GeoDataCoordinates::Degree ) ); } geom->append( line ); } placemark->setGeometry( geom ); mDebug() << "arc " << placemark->name() << " " << shape->nParts; } else { GeoDataLineString *line = new GeoDataLineString; for( int j=0; j<shape->nVertices; ++j ) { line->append( GeoDataCoordinates( shape->padfX[j], shape->padfY[j], 0, GeoDataCoordinates::Degree ) ); } placemark->setGeometry( line ); mDebug() << "arc " << placemark->name() << " " << shape->nParts; } break; } case SHPT_POLYGON: { if ( shape->nParts != 1 ) { bool isRingClockwise = false; GeoDataMultiGeometry *multigeom = new GeoDataMultiGeometry; GeoDataPolygon *poly = 0; int polygonCount = 0; for( int j=0; j<shape->nParts; ++j ) { GeoDataLinearRing ring; int itStart = shape->panPartStart[j]; int itEnd = (j + 1 < shape->nParts) ? shape->panPartStart[j+1] : shape->nVertices; for( int k = itStart; k<itEnd; ++k ) { ring.append( GeoDataCoordinates( shape->padfX[k], shape->padfY[k], 0, GeoDataCoordinates::Degree ) ); } isRingClockwise = ring.isClockwise(); if ( j == 0 || isRingClockwise ) { poly = new GeoDataPolygon; ++polygonCount; poly->setOuterBoundary( ring ); if ( polygonCount > 1 ) { multigeom->append( poly ); } } else { poly->appendInnerBoundary( ring ); } } if ( polygonCount > 1 ) { placemark->setGeometry( multigeom ); } else { placemark->setGeometry( poly ); delete multigeom; multigeom = 0; } mDebug() << "donut " << placemark->name() << " " << shape->nParts; } else { GeoDataPolygon *poly = new GeoDataPolygon; GeoDataLinearRing ring; for( int j=0; j<shape->nVertices; ++j ) { ring.append( GeoDataCoordinates( shape->padfX[j], shape->padfY[j], 0, GeoDataCoordinates::Degree ) ); } poly->setOuterBoundary( ring ); placemark->setGeometry( poly ); mDebug() << "poly " << placemark->name() << " " << shape->nParts; } break; } } } SHPClose( handle ); DBFClose( dbfhandle ); if ( document->size() ) { document->setFileName( fileName ); return document; } else { delete document; return nullptr; } }