static int _setAtt(S57_geo *geoData, OGRFeatureH hFeature) { int field_count = OGR_F_GetFieldCount(hFeature); for (int field_index=0; field_index<field_count; ++field_index) { if (OGR_F_IsFieldSet(hFeature, field_index)) { const char *propName = OGR_Fld_GetNameRef(OGR_F_GetFieldDefnRef(hFeature,field_index)); const char *propValue = OGR_F_GetFieldAsString(hFeature, field_index); S57_setAtt(geoData, propName, propValue); /* debug if (0 == g_strcmp0(S57_getName(geoData), "M_NPUB")) { PRINTF("DEBUG: M_NPUB-%i: %s --> %s\n", field_index, propName, propValue); } if (0 == g_strcmp0(S57_getName(geoData), "C_AGGR")) { PRINTF("DEBUG: C_AGGR-%i: %s --> %s\n", field_index, propName, propValue); } if (0 == g_strcmp0(S57_getName(geoData), "C_ASSO")) { PRINTF("DEBUG: C_ASSO-%i: %s --> %s\n", field_index, propName, propValue); } */ } } // optimisation: direct link to the value of Att (GString) // save the search in attList GString *scamin = S57_getAttVal(geoData, "SCAMIN"); if ((NULL!=scamin) && (NULL!=scamin->str)){ S57_setScamin(geoData, S52_atof(scamin->str)); } return TRUE; }
QgsFields QgsOgrUtils::readOgrFields( OGRFeatureH ogrFet, QTextCodec* encoding ) { QgsFields fields; if ( !ogrFet ) return fields; int fieldCount = OGR_F_GetFieldCount( ogrFet ); for ( int i = 0; i < fieldCount; ++i ) { OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef( ogrFet, i ); if ( !fldDef ) { fields.append( QgsField() ); continue; } QString name = encoding ? encoding->toUnicode( OGR_Fld_GetNameRef( fldDef ) ) : QString::fromUtf8( OGR_Fld_GetNameRef( fldDef ) ); QVariant::Type varType; switch ( OGR_Fld_GetType( fldDef ) ) { case OFTInteger: varType = QVariant::Int; break; #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 2000000 case OFTInteger64: varType = QVariant::LongLong; break; #endif case OFTReal: varType = QVariant::Double; break; #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1400 case OFTDate: varType = QVariant::Date; break; case OFTTime: varType = QVariant::Time; break; case OFTDateTime: varType = QVariant::DateTime; break; case OFTString: #endif default: varType = QVariant::String; // other unsupported, leave it as a string } fields.append( QgsField( name, varType ) ); } return fields; }
void AttributeFilter::UpdateGEOSBuffer(PointBuffer& buffer, AttributeInfo& info) { QuadIndex idx(buffer); idx.build(); if (!info.lyr) // wake up the layer { if (info.layer.size()) info.lyr = OGR_DS_GetLayerByName(info.ds.get(), info.layer.c_str()); else if (info.query.size()) { info.lyr = OGR_DS_ExecuteSQL(info.ds.get(), info.query.c_str(), 0, 0); } else info.lyr = OGR_DS_GetLayer(info.ds.get(), 0); if (!info.lyr) { std::ostringstream oss; oss << "Unable to select layer '" << info.layer << "'"; throw pdal_error(oss.str()); } } OGRFeaturePtr feature = OGRFeaturePtr(OGR_L_GetNextFeature(info.lyr), OGRFeatureDeleter()); int field_index(1); // default to first column if nothing was set if (info.column.size()) { field_index = OGR_F_GetFieldIndex(feature.get(), info.column.c_str()); if (field_index == -1) { std::ostringstream oss; oss << "No column name '" << info.column << "' was found."; throw pdal_error(oss.str()); } } while(feature) { OGRGeometryH geom = OGR_F_GetGeometryRef(feature.get()); OGRwkbGeometryType t = OGR_G_GetGeometryType(geom); int f_count = OGR_F_GetFieldCount (feature.get()); if (!(t == wkbPolygon || t == wkbMultiPolygon || t == wkbPolygon25D || t == wkbMultiPolygon25D)) { std::ostringstream oss; oss << "Geometry is not Polygon or MultiPolygon!"; throw pdal::pdal_error(oss.str()); } OGRGeometry* ogr_g = (OGRGeometry*) geom; GEOSGeometry* geos_g (0); if (!m_geosEnvironment) { #if (GDAL_VERSION_MINOR < 11) && (GDAL_VERSION_MAJOR == 1) geos_g = ogr_g->exportToGEOS(); #else m_geosEnvironment = ogr_g->createGEOSContext(); geos_g = ogr_g->exportToGEOS(m_geosEnvironment); #endif } GEOSPreparedGeometry const* geos_pg = GEOSPrepare_r(m_geosEnvironment, geos_g); if (!geos_pg) throw pdal_error("unable to prepare geometry for index-accelerated intersection"); // Compute a total bounds for the geometry. Query the QuadTree to // find out the points that are inside the bbox. Then test each // point in the bbox against the prepared geometry. BOX3D box = computeBounds(m_geosEnvironment, geos_g); std::vector<std::size_t> ids = idx.getPoints(box); for (const auto& i : ids) { double x = buffer.getFieldAs<double>(Dimension::Id::X, i); double y = buffer.getFieldAs<double>(Dimension::Id::Y, i); double z = buffer.getFieldAs<double>(Dimension::Id::Z, i); GEOSGeometry* p = createGEOSPoint(m_geosEnvironment, x, y ,z); if (static_cast<bool>(GEOSPreparedContains_r(m_geosEnvironment, geos_pg, p))) { // We're in the poly, write the attribute value int32_t v = OGR_F_GetFieldAsInteger(feature.get(), field_index); buffer.setField(info.dim, i, v); // log()->get(LogLevel::Debug) << "Setting value: " << v << std::endl; } GEOSGeom_destroy_r(m_geosEnvironment, p); } feature = OGRFeaturePtr(OGR_L_GetNextFeature(info.lyr), OGRFeatureDeleter()); } }
QString QgsShapeFile::getFeatureClass() { // scan the whole layer to try to determine the geometry // type. qApp->processEvents(); isMulti = scanGeometries(); OGRFeatureH feat; // skip features without geometry while (( feat = OGR_L_GetNextFeature( ogrLayer ) ) != NULL ) { if ( OGR_F_GetGeometryRef( feat ) ) break; } if ( feat ) { OGRGeometryH geom = OGR_F_GetGeometryRef( feat ); if ( geom ) { /* OGR doesn't appear to report geometry type properly * for a layer containing both polygon and multipolygon * entities * // get the feature type from the layer OGRFeatureDefn * gDef = ogrLayer->GetLayerDefn(); OGRwkbGeometryType gType = gDef->GetGeomType(); geom_type = QGis::qgisFeatureTypes[gType]; */ //geom_type = QString(geom->getGeometryName()); //geom_type = "GEOMETRY"; QgsDebugMsg( "Preparing to escape " + geom_type ); char * esc_str = new char[geom_type.length()*2+1]; PQescapeString( esc_str, geom_type.toUtf8(), geom_type.length() ); geom_type = QString( esc_str ); QgsDebugMsg( "After escaping, geom_type is : " + geom_type ); delete[] esc_str; QString file( fileName ); file.replace( file.length() - 3, 3, "dbf" ); // open the dbf file std::ifstream dbf( file.toUtf8(), std::ios::in | std::ios::binary ); // read header DbaseHeader dbh; dbf.read(( char * )&dbh, sizeof( dbh ) ); // Check byte order if ( htonl( 1 ) == 1 ) { /* DbaseHeader is stored in little-endian format. * The num_recs, size_hdr and size_rec fields must be byte-swapped when read * on a big-endian processor. Currently only size_hdr is used. */ unsigned char *byte = reinterpret_cast<unsigned char *>( &dbh.size_hdr ); unsigned char t = *byte; *byte = *( byte + 1 ); *( byte + 1 ) = t; } Fda fda; QString str_type = "varchar("; for ( int field_count = 0, bytes_read = sizeof( dbh ); bytes_read < dbh.size_hdr - 1; field_count++, bytes_read += sizeof( fda ) ) { dbf.read(( char * )&fda, sizeof( fda ) ); switch ( fda.field_type ) { case 'N': if (( int )fda.field_decimal > 0 ) column_types.push_back( "float" ); else column_types.push_back( "int" ); break; case 'F': column_types.push_back( "float" ); break; case 'D': column_types.push_back( "date" ); break; case 'C': str_type = QString( "varchar(%1)" ).arg( fda.field_length ); column_types.push_back( str_type ); break; case 'L': column_types.push_back( "boolean" ); break; default: column_types.push_back( "varchar(256)" ); break; } } dbf.close(); int numFields = OGR_F_GetFieldCount( feat ); for ( int n = 0; n < numFields; n++ ) { QString s = codec->toUnicode( OGR_Fld_GetNameRef( OGR_F_GetFieldDefnRef( feat, n ) ) ); column_names.push_back( s ); } } else valid = false; OGR_F_Destroy( feat ); } else valid = false; OGR_L_ResetReading( ogrLayer ); return valid ? geom_type : QString::null; }
QString QgsShapeFile::getFeatureClass() { // scan the whole layer to try to determine the geometry // type. qApp->processEvents(); isMulti = scanGeometries(); OGRFeatureH feat; // skip features without geometry while (( feat = OGR_L_GetNextFeature( ogrLayer ) ) != NULL ) { if ( OGR_F_GetGeometryRef( feat ) ) break; } if ( feat ) { OGRGeometryH geom = OGR_F_GetGeometryRef( feat ); if ( geom ) { /* OGR doesn't appear to report geometry type properly * for a layer containing both polygon and multipolygon * entities * // get the feature type from the layer OGRFeatureDefn * gDef = ogrLayer->GetLayerDefn(); OGRwkbGeometryType gType = gDef->GetGeomType(); geom_type = QGis::qgisFeatureTypes[gType]; */ //geom_type = QString(geom->getGeometryName()); //geom_type = "GEOMETRY"; QgsDebugMsg( "Preparing to escape " + geom_type ); char * esc_str = new char[geom_type.length()*2+1]; PQescapeString( esc_str, geom_type.toUtf8(), geom_type.length() ); geom_type = QString( esc_str ); QgsDebugMsg( "After escaping, geom_type is : " + geom_type ); delete[] esc_str; int numFields = OGR_F_GetFieldCount( feat ); for ( int n = 0; n < numFields; n++ ) { OGRFieldDefnH fld = OGR_F_GetFieldDefnRef( feat, n ); column_names.push_back( codec->toUnicode( OGR_Fld_GetNameRef( fld ) ) ); switch ( OGR_Fld_GetType( fld ) ) { case OFTInteger: column_types.push_back( "int" ); break; case OFTReal: column_types.push_back( "float" ); break; case OFTString: column_types.push_back( QString( "varchar(%1)" ).arg( OGR_Fld_GetWidth( fld ) ) ); break; case OFTDate: column_types.push_back( "date" ); break; case OFTTime: column_types.push_back( "time" ); break; case OFTDateTime: column_types.push_back( "timestamp" ); break; default: column_types.push_back( "varchar(256)" ); break; } } } else valid = false; OGR_F_Destroy( feat ); } else valid = false; OGR_L_ResetReading( ogrLayer ); return valid ? geom_type : QString::null; }