Skip to content
Permalink
Browse files

postgres & ogr provider update:

- 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/qgis@11464 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef
jef committed Aug 21, 2009
1 parent 9e57e6c commit 922597400b7da253db42f362b99b6329e443b385
@@ -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;

@@ -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
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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 ) );
@@ -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" ||
@@ -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 );
@@ -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 ) );
}
}
}

0 comments on commit 9225974

Please sign in to comment.
You can’t perform that action at this time.