Ticket #1037 (closed defect: fixed)
PostGIS (and possibly other SQL-based sources) varchar field lengths ignored
| Reported by: | kyngchaos@… | Owned by: | warmerdam |
|---|---|---|---|
| Priority: | high | Milestone: | |
| Component: | OGR_SF | Version: | unspecified |
| Severity: | normal | Keywords: | |
| Cc: |
Description
When reading a PostGIS/Postgres layer or table, varchar field lengths are ignored. Varchar fields have a
length, the same as fixed char fields. It's just a matter of whether the data is stored padded with
spaces or not.
As a test, in a Postgres table with varchar fields, ogrinfo -al -so returns String(0.0) as the type for all
varchar fields. When a PostGIS layer is translated to shapefile, for example, which only has fixed length
char fields, varchar fields are all translated as char(80), regardless of their max length.
After a little poking around, I was able to patch the pg driver to handle the length of varchar fields.
This diff is for GDAL release 1.3.1, ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp:
--- ogrpgtablelayer-old.cpp 2005-09-25 23:37:17.000000000 -0500
+++ ogrpgtablelayer.cpp 2006-01-12 22:40:46.000000000 -0600
@@ -320,11 +320,11 @@
continue;
}
- if( EQUAL(pszType,"varchar") || EQUAL(pszType,"text") )
+ if( EQUAL(pszType,"text") )
{
oField.SetType( OFTString );
}
- else if( EQUAL(pszType,"bpchar") )
+ else if( EQUAL(pszType,"bpchar") || EQUAL(pszType,"varchar") )
{
int nWidth;
@@ -333,6 +333,8 @@
{
if( EQUALN(pszFormatType,"character(",10) )
nWidth = atoi(pszFormatType+10);
+ else if( EQUALN(pszFormatType,"character varying(",18) )
+ nWidth = atoi(pszFormatType+18);
else
nWidth = 0;
}
A similar thing should be done for other SQL-based drivers. I've seen that the MySQL driver also
returns 0 lengths for varchar fields. And others may also have the problem - OCI, ODBC, Sqlite.
Change History
Note: See
TracTickets for help on using
tickets.
