[Gdal-dev] Text field size ogr2ogr PostgreSQL to MIF/MID
Mateusz Loskot
mateusz at loskot.net
Thu Feb 1 16:11:13 EST 2007
Frank Warmerdam wrote:
> Marc Jacquin wrote:
>> Hi all,
>>
>> I am using ogr2ogr to convert a PG table into MIF/MID.
>> The table fields are varchar(x) or char(x) type and the field width is
>> never
>> more than let say 50.
>>
>> It seems that ogr2ogr forces text fields to be 254 wide. But when you get
>> more than 20 fields the record size exceeds the 4 K limit of MapInfo 6.
>>
>> Is there a PG usable type so that ogr2ogr would keep the width for
>> exporting?
>
> Marc,
>
> It looks like the CHARACTER(n) field type in Postgres will be treated by
> OGR as a field with a particular width. The code that decides whether
> to associate a width with text fields in the driver looks like this:
Frank,
Isn't it correct assumption?
I'm not sure I understand where is the problem here, but as I know,
CHARACTER(n) type should be handled as fixed-width type.
> pszType = PQgetvalue(hResult, iRecord, 1 );
> pszFormatType = PQgetvalue(hResult,iRecord,3);
>
> ...
>
> else if( EQUAL(pszType,"bpchar") || EQUAL(pszType,"varchar") )
> {
> int nWidth;
>
> nWidth = atoi(PQgetvalue(hResult,iRecord,2));
> if( nWidth == -1 )
> {
> if( EQUALN(pszFormatType,"character(",10) )
> nWidth = atoi(pszFormatType+10);
> else if( EQUALN(pszFormatType,"character varying(",18) )
> nWidth = atoi(pszFormatType+18);
> else
> nWidth = 0;
> }
> oField.SetType( OFTString );
> oField.SetWidth( nWidth );
> }
I may not understand rationale behind the code above well, but using
libpq it's possible to retrieve lenght of character type from result
set, using PQfmod() function:
//////////////////////////////////////////////////////
const int VARCHAR_HDRSZ = 4; // usually, equal to sizeof(int)
int fmod = PQfmod(hResult, iColumnIndex);
if (-1 != fmod)
length = (fmod - VARCHAR_HDRSZ);
//////////////////////////////////////////////////////
For character columns with specified size (for both types,
variable-lenght or fixed-lenght) during table creation, the PQfmod
returns always non -1 value, so the size can be retrieved.
However, if a table was created using 'text' type or 'character varying'
(varchar) without a length specifier, then PQfmod() returns -1,
what means there is no limit of size specified.
Cheers
--
Mateusz Loskot
http://mateusz.loskot.net
More information about the Gdal-dev
mailing list