Skip to content

Commit

Permalink
postgres & ogr provider update:
Browse files Browse the repository at this point in the history
- postgres provider: report field width -1 for everything except strings (fixes #1131)
- in vector file writer, ogr provider choose field width:
  - 255 for strings, when given width is negative or larger.
    (was 80 in ogr provider and OGR determined-default for vector file writer).
  - 10 for integers, when given width is negative or larger.
  - 32 for reals, when given width is negative or larger.



git-svn-id: http://svn.osgeo.org/qgis/trunk@11464 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Aug 21, 2009
1 parent 1cb44d1 commit 793b091
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
10 changes: 6 additions & 4 deletions src/core/qgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,19 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
{
case QVariant::LongLong:
ogrType = OFTString;
ogrWidth = ogrWidth <= 21 ? ogrWidth : 21;
ogrWidth = ogrWidth > 0 && ogrWidth <= 21 ? ogrWidth : 21;
ogrPrecision = -1;
break;

case QVariant::String:
ogrType = OFTString;
if ( ogrWidth < 0 || ogrWidth > 255 )
ogrWidth = 255;
break;

case QVariant::Int:
ogrType = OFTInteger;
ogrWidth = ogrWidth <= 10 ? ogrWidth : 10;
ogrWidth = ogrWidth > 0 && ogrWidth <= 10 ? ogrWidth : 10;
ogrPrecision = 0;
break;

Expand Down Expand Up @@ -388,11 +390,11 @@ QgsVectorFileWriter::writeAsShapefile( QgsVectorLayer* layer,
// The 'CT-params' (e.g. +towgs84) does not get stripped in this way
QRegExp regExp( ".shp$" );
QString prjName = shapefileName;
prjName.replace( regExp, QString( "" ));
prjName.replace( regExp, QString( "" ) );
prjName.append( QString( ".prj" ) );
QFile prjFile( prjName );

if( !prjFile.open( QIODevice::WriteOnly ) )
if ( !prjFile.open( QIODevice::WriteOnly ) )
{
QgsDebugMsg( "Couldn't open file " + prjName );
return NoError; // For now
Expand Down
10 changes: 5 additions & 5 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1458,9 +1458,9 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,
OGRFieldDefnH field;
if ( fields[0] == "Real" )
{
if ( width == -1 )
if ( width < 0 )
width = 32;
if ( precision == -1 )
if ( precision < 0 )
precision = 3;

field = OGR_Fld_Create( codec->fromUnicode( it->first ).data(), OFTReal );
Expand All @@ -1469,7 +1469,7 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,
}
else if ( fields[0] == "Integer" )
{
if ( width == -1 || width > 10 )
if ( width < 0 || width > 10 )
width = 10;

field = OGR_Fld_Create( codec->fromUnicode( it->first ).data(), OFTInteger );
Expand All @@ -1478,8 +1478,8 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,
}
else if ( fields[0] == "String" )
{
if ( width == -1 )
width = 80;
if ( width < 0 || width > 255 )
width = 255;

field = OGR_Fld_Create( codec->fromUnicode( it->first ).data(), OFTString );
OGR_Fld_SetWidth( field, width );
Expand Down
17 changes: 15 additions & 2 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ void QgsPostgresProvider::loadFields()
QString fieldTypeName = QString::fromUtf8( PQgetvalue( oidResult, 0, 0 ) );
QString fieldTType = QString::fromUtf8( PQgetvalue( oidResult, 0, 1 ) );
QString fieldElem = QString::fromUtf8( PQgetvalue( oidResult, 0, 2 ) );
QString fieldSize = QString::fromUtf8( PQgetvalue( oidResult, 0, 3 ) );
int fieldSize = QString::fromUtf8( PQgetvalue( oidResult, 0, 3 ) ).toInt();

sql = QString( "SELECT attnum FROM pg_attribute WHERE attrelid=%1 AND attname=%2" )
.arg( tableoid ).arg( quotedValue( fieldName ) );
Expand All @@ -888,15 +888,24 @@ void QgsPostgresProvider::loadFields()
fieldTypeName = fieldTypeName.mid( 1 );

if ( fieldTypeName == "int8" )
{
fieldType = QVariant::LongLong;
fieldSize = -1;
}
else if ( fieldTypeName.startsWith( "int" ) ||
fieldTypeName == "serial" )
{
fieldType = QVariant::Int;
fieldSize = -1;
}
else if ( fieldTypeName == "real" ||
fieldTypeName == "double precision" ||
fieldTypeName.startsWith( "float" ) ||
fieldTypeName == "numeric" )
{
fieldType = QVariant::Double;
fieldSize = -1;
}
else if ( fieldTypeName == "text" ||
fieldTypeName == "char" ||
fieldTypeName == "bpchar" ||
Expand All @@ -906,7 +915,9 @@ void QgsPostgresProvider::loadFields()
fieldTypeName == "money" ||
fieldTypeName.startsWith( "time" ) ||
fieldTypeName.startsWith( "date" ) )
{
fieldType = QVariant::String;
}
else
{
QgsDebugMsg( "Field " + fieldName + " ignored, because of unsupported type " + fieldTypeName );
Expand All @@ -917,20 +928,22 @@ void QgsPostgresProvider::loadFields()
{
fieldTypeName = "_" + fieldTypeName;
fieldType = QVariant::String;
fieldSize = -1;
}
}
else if ( fieldTType == "e" )
{
// enum
fieldType = QVariant::String;
fieldSize = -1;
}
else
{
QgsDebugMsg( "Field " + fieldName + " ignored, because of unsupported type type " + fieldTType );
continue;
}

attributeFields.insert( i, QgsField( fieldName, fieldType, fieldTypeName, fieldSize.toInt(), fieldModifier, fieldComment ) );
attributeFields.insert( i, QgsField( fieldName, fieldType, fieldTypeName, fieldSize, fieldModifier, fieldComment ) );
}
}
}
Expand Down

0 comments on commit 793b091

Please sign in to comment.