UpDate:
Yes, that was it!
Like mloskot said, the whole tabel needs to be dumped. selecting fields with sql loses information about fuield widths.
Thanks, that fixed it!
Hi All,
I have a problem with ogr2ogr. I'm trying to dump a PostgreSQL tabel to a Shapefile. ogr2ogr seems to change the size of data-types like integer and char.
Here's part of the DB structure that the Shapefile set was read into:
Table "test"
nd_1 - numeric(8,0)
nd_2 - numeric(2,0)
nd_3 - numeric(2,0)
nd_9 - character varying(60)
and it looks correctly.
Here's where the fun begins:
ogr2ogr air5000.shp "PG: [...]" sql 'select
CAST(nd_1 AS INTEGER),
CAST(nd_2 AS INTEGER),
CAST(nd_3 as INTEGER),
CAST(ND_9 AS CHARACTER VARYING(60))
from test' ;
then
dbview -e test.dbf
Field Name Type Length Decimal Pos
nd 1 - N - 11 - 0
nd 2 - N - 11 - 0
nd 3 - N - 11 - 0
nd 9 - C - 80 - 0
num-length goes form 8,2,2 to 11, char length goes from 10 to 80.
I read that these are the defaults if width (length) is not specified.
How can i specify the width?
I tried every combo with or without casting, like this:
select
CAST(nd_1 AS NUMERIC),
CAST(nd_2 AS NUMERIC),
CAST(nd_3 as NUMERIC),
CAST(ND_9 AS CHARACTER VARYING(60))
which gives in dbview
:
Field Name Type Length Decimal Pos
nd 1 - N - 24 - 15
nd 2 - N - 24 - 15
nd 3 - N - 24 - 15
nd 9 - C - 80 - 0
no casting (select * from test
) gives
Field Name Type Length Decimal Pos
nd 1 - N - 24 - 15
nd 2 - N - 24 - 15
nd 3 - N - 24 - 15
nd 9 - C - 80 - 0
Nowhere near the originals on any occasion.
debugfile [--debug on]
gives nothing special IMHO, here is the output of the 3 ogr2ogr command i tried:
PG: PQexec(DECLARE executeSQLCursor CURSOR for select CAST(nd_1 AS INTEGER), CAST(nd_2 AS INTEGER), CAST(nd_3 as INTEGER), CAST(ND_9 AS CHARACTER VARYING(60)) from test)
PG: PQexec(DECLARE OGRPGResultLayerReader CURSOR for select CAST(nd_1 AS INTEGER), CAST(nd_2 AS INTEGER), CAST(nd_3 as INTEGER), CAST(ND_9 AS CHARACTER VARYING(60)) from test)
PG: PQexec(DECLARE OGRPGResultLayerReader CURSOR for select CAST(nd_1 AS INTEGER), CAST(nd_2 AS INTEGER), CAST(nd_3 as INTEGER), CAST(ND_9 AS CHARACTER VARYING(60)) from test)
PG: 2 features read on layer 'sql_statement'.
PG: PQexec(DECLARE executeSQLCursor CURSOR for select CAST(nd_1 AS NUMERIC), CAST(nd_2 AS NUMERIC), CAST(nd_3 as NUMERIC), CAST(ND_9 AS CHARACTER VARYING(60)) from test)
PG: PQexec(DECLARE OGRPGResultLayerReader CURSOR for select CAST(nd_1 AS NUMERIC), CAST(nd_2 AS NUMERIC), CAST(nd_3 as NUMERIC), CAST(ND_9 AS CHARACTER VARYING(60)) from test)
PG: PQexec(DECLARE OGRPGResultLayerReader CURSOR for select CAST(nd_1 AS NUMERIC), CAST(nd_2 AS NUMERIC), CAST(nd_3 as NUMERIC), CAST(ND_9 AS CHARACTER VARYING(60)) from test)
PG: 2 features read on layer 'sql_statement'.
PG: PQexec(DECLARE executeSQLCursor CURSOR for select * from test)
PG: PQexec(DECLARE OGRPGResultLayerReader CURSOR for select * from test)
PG: PQexec(DECLARE OGRPGResultLayerReader CURSOR for select * from test)
PG: 2 features read on layer 'sql_statement'.
I also tried -dsco precision=no
but that did not help either.
Maybe dbview
is pulling my leg, but it showed the header of the source .dbf of the Shapefile set correctly.
Anyone any ideas?
EJ
UpDate: hi mloskot,
thanks for your reply and the reformatting, much appreciated!
i'm on GDAL 1.6.3.
i tried specifying the width, but it does not make a difference.
ogr2ogr test.shp "PG: [...] " -sql 'select CAST(nd_1 AS INTEGER), CAST(nd_2 AS numeric(2,0)), CAST(nd_3 as NUMERIC(2,0)) from test' ;
gives:
Field Name Type Length Decimal Pos
nd 1 N 11 0
nd 2 N 24 15
nd 3 N 24 15
yes i tried specifying the fields without casting, like this:
ogr2ogr air5000.shp "PG: [...] "-sql 'select nd_1, nd_2, nd_3, ND_9 from test'
which gives:
Field Name Type Length Decimal Pos
nd 1 N 24 15
nd 2 N 24 15
nd 3 N 24 15
nd 9 C 80 0
trying
integer(field_length)
like
CAST(nd_2 AS INTEGER(2))
does not work, gives error
ERROR 1: ERROR: current transaction is aborted, commands ignored until end of transaction block
maybe my Postgresql and PostGIS version are dated? Postgres = 8.3.5, PostGIS = 1.3.3
i will check Shapelib, i installed it just now.