2010-02-02 2 views
2

UpDate :PG에서 SHP 로의 ogr2ogr이 .DBF의 데이터 유형을 변경합니다.

예, 그게 전부입니다!

mloskot처럼 전체 테이블을 덤프해야합니다. sql을 사용하여 필드를 선택하면 fuield 너비에 대한 정보가 손실됩니다.

감사합니다.

안녕 모두,

나는 ogr2ogr에 문제가 있습니다. PostgreSQL tabel을 Shapefile에 덤프하려고합니다. ogr2ogr은 정수 및 char과 같은 데이터 유형의 크기를 변경하는 것으로 보입니다.

다음은 Shape 파일 세트에 읽은 DB 구조의 일부입니다 :

Table "test" 
nd_1 - numeric(8,0) 
nd_2 - numeric(2,0) 
nd_3 - numeric(2,0) 
nd_9 - character varying(60) 

를하고 올바르게 보인다. 여기

는 재미가 시작되는 곳입니다 :

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 

납입 길이가가는 형태 8,2,211에 다음

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' ; 

, 문자 길이는 10에서 간다 .

폭 (길이)을 지정하지 않으면 기본값입니다.

너비는 어떻게 지정할 수 있습니까?

나는 이런 식으로, 또는 캐스팅없이 모든 콤보를 시도 :

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 

에는 주조 (select * from test)는 어디에도

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 

을 제공하지 : dbview에 제공하지 않습니다

select 
CAST(nd_1 AS NUMERIC), 
CAST(nd_2 AS NUMERIC), 
CAST(nd_3 as NUMERIC), 
CAST(ND_9 AS CHARACTER VARYING(60)) 

어떤 장면에서도 원본 근처. 나는 또한 -dsco precision=no을 시도했지만 그 역시 도움이되지 않았다

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'. 

:

debugfile [--debug on] 내가 시도 3 ogr2ogr 명령의 출력을 여기에, 아무것도 특별한 이럴을 제공합니다.

아마도 dbview이 내 다리를 당기고 있지만 셰이프 파일의 원본 .dbf 헤더가 올바르게 설정되어 있습니다.

누구나 아이디어가 있으십니까?

EJ

업데이트 : 안녕 mloskot, 회신 및 재 포맷에 대한

덕분에, 많은 감사!

나는 GDAL 1.6.3을 사용 중입니다.

너비를 지정하려고 시도했지만 차이가 없습니다.

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' ; 

을 제공합니다

ogr2ogr air5000.shp "PG: [...] "-sql 'select nd_1, nd_2, nd_3, ND_9 from test' 

주는 :

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 

을 시도

Field Name Type Length Decimal Pos 
nd 1   N 11  0 
nd 2   N 24  15 
nd 3   N 24  15 

예 내가 다음과 같이 캐스팅없이 필드를 지정 시도

같은

integer(field_length)

CAST(nd_2 AS INTEGER(2)) 

이 작동하지 않습니다 ,

ERROR 1: ERROR: current transaction is aborted, commands ignored until end of transaction block

오류

어쩌면 내 PostgreSQL을하고 PostGIS와 버전 날짜가 기입되어 있습니다? Postgres = 8.3.5, PostGIS = 1.3.3

나는 Shapelib을 확인하겠다. 나는 지금 설치했다.

답변

5

먼저 GDAL/OGR의 버전을 지정하지 않으므로 문제가있는 곳을 진단하기가 어려울 수 있습니다. 버전별로 버전이 향상되고 있습니다.

예, 당신은 당신이 행의 일부가 테이블 을 형성만을 가져 오는 SQL 쿼리를 사용하는 것을 기본 값이 OGR driver for ESRI Shapefile

문제에 의해 설정되어, 올바른 값 (11)와 각각 정수 폭과 문자 폭 80입니다 이로 인해 OGR PG driver 드라이버가 필드 너비를 보존하지 않습니다.

이 두 개의 출력 비교 : 당신이 볼 수 있듯이 전체 테이블이

$ ogrinfo PG:dbname=test test_ogr 
Layer name: test_ogr 
Geometry: Point 
Feature Count: 1 
Extent: (1.000000, 2.000000) - (1.000000, 2.000000) 
Layer SRS WKT: 
(unknown) 
FID Column = id 
Geometry Column = geom 
n3: String (60.0) 
n1: Integer (8.0) 
n2: Integer (2.0) 
OGRFeature(test_ogr):1 
    n3 (String) = abcdefg 
    n1 (Integer) = 12345678 
    n2 (Integer) = 12 
    POINT (1 2) 

같은 쿼리하지만 수동으로 지정된 SQL과

$ ogrinfo PG:dbname=test -sql "SELECT n1, n2, n3, geom FROM test_ogr" 
Layer name: sql_statement 
Geometry: Unknown (any) 
Feature Count: 1 
Extent: (1.000000, 2.000000) - (1.000000, 2.000000) 
Layer SRS WKT: 
(unknown) 
Geometry Column = geom 
n1: Real (0.0) 
n2: Real (0.0) 
n3: String (0.0) 
OGRFeature(sql_statement):0 
    n1 (Real) = 12345678 
    n2 (Real) = 12 
    n3 (String) = abcdefg 
    POINT (1 2) 

을 선택

쿼리를, 두 번째 옵션은 필드 속성을 푼다 완전하게 OGR이 작동하는 방법입니다.여기

두 출력 같은 예이지만, Shape 파일로 번역 :

$ ogr2ogr -f "ESRI Shapefile" test.shp PG:dbname=test -sql "SELECT n1, n2, n3, geom FROM test_ogr" 
$ dbfdump -h test.dbf 
Field 0: Type=N/Double, Title=`n1', Width=24, Decimals=15 
Field 1: Type=N/Double, Title=`n2', Width=24, Decimals=15 
Field 2: Type=C/String, Title=`n3', Width=80, Decimals=0 
         n1      n2 n3 
12345678.000000000000000  12.000000000000000 abcdefg 
:

-SQL 옵션 SELECT 쿼리의 사용과 test_ogr 테이블

$ ogr2ogr -f "ESRI Shapefile" test.shp PG:dbname=test test_ogr 
$ dbfdump -h test.dbf 
Field 0: Type=C/String, Title=`n3', Width=60, Decimals=0 
Field 1: Type=N/Integer, Title=`n1', Width=8, Decimals=0 
Field 2: Type=N/Integer, Title=`n2', Width=2, Decimals=0 
n3                 n1 n2 
abcdefg              12345678 12 

모든 데이터 덤프

따라서 모든 속성을 정확하게 유지하려면 SQL 쿼리를 지정하지 않고 모든 테이블을 덤프해야합니다.

그런데 의심 스럽다면 dbfview를보고 shpdump and dbfdump 유틸리티를 사용하여 출력 된 Shapefile 파일 (.shp, .dbf)을 Shapelib에서 검사 해보십시오. 이 소프트웨어는 GDAL/OGR에서 Shapefile 데이터를 처리하는 데 사용되는 것과 정확히 동일한 C 코드를 사용합니다.

관련 문제