2017-06-29 69 views
0

我有導入CSV數據到Postgres數據庫與地理數據/ PostGIS的通過以下命令在我的數據庫「地標」啓用一個提交給Postgres:導入CSV新列

CREATE EXTENSION postgis; 

所以....故事如下:

我跟隨此tutorial

我試圖導入CSV這些列

name conf capital venture latitude longitude 

第一線,爲數據的一個例子是:

example, 1, 1, 1, 51.51923, -0.12205 

我已經設置了表了教程以下除了在他的數據(地址,date_built,建築,地標)增加的conf,資金和風險,而不是列。即:

CREATE TABLE landmarks 
(
    gid serial NOT NULL, 
    name character varying(50), 
    conf character varying(10), 
    capital character varying(10), 
    venture character varying(10), 
    the_geom geometry, 
    CONSTRAINT landmarks_pkey PRIMARY KEY (gid), 
    CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2), 
    CONSTRAINT enforce_geotype_geom CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL), 
    CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326) 
); 

然後

CREATE INDEX landmarks_the_geom_gist 
    ON landmarks 
    USING gist 
(the_geom); 

的數據基本上是相同的,否則他的example

我已經正確設置了表格並啓用了postgis擴展來處理好geom數據。

然而,問題是當我試圖導入我的CSV:

landmarks=# \copy landmarks(name,conf,capital,venture,latitude,longitude) FROM '../../../../../var/tmp/map2.csv' DELIMITERS ',' CSV HEADER; 
ERROR: column "latitude" of relation "landmarks" does not exist 

現在,我注意到,當他創造了桌上,他不添加經緯度列...所以我想知道如果是這樣的問題,並試圖建立與這些列以及整數另一個表,但只是給我這個錯誤:

ptmap3=# \copy landmarks(name,conf,capital,venture,latitude,longitude) FROM '../../../../../var/tmp/map2.csv' DELIMITERS ',' CSV HEADER; 
ERROR: invalid input syntax for integer: "51.51923" 
CONTEXT: COPY landmarks, line 2, column latitude: "51.51923" 

所以......看來,如果我添加了緯度列,然後它的工作原理,但數據失敗?使用這種

od -c map2.csv 

檢查錯誤的CSV之後...有什麼錯我的CSV(沒有隱藏字符或錯誤)......所以這是怎麼回事?

如果任何人都可以幫我導入我的csv到這個數據庫,我將非常感激!

+0

錯誤:整數無效輸入語法:「51.51923」分配一個字符串值的整數列時將發生該錯誤。你可以檢查數據是如何在csv文件中獲得經緯度的 –

+0

在命令行中使用od讀取,該值以character-for-character的形式存儲爲5 1。 5 1 9 2 3, –

回答

0

您需要分兩步處理,將您的(latitude,longitude)數據導入數據庫的幾何列。

1。將數據導入兩條浮法列latitudelongitude

從原始表的地標:

CREATE TABLE landmarks 
(
    gid serial NOT NULL, 
    name character varying(50), 
    conf character varying(10), 
    capital character varying(10), 
    venture character varying(10), 
    the_geom geometry, 
    CONSTRAINT landmarks_pkey PRIMARY KEY (gid), 
    CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2), 
    CONSTRAINT enforce_geotype_geom CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL), 
    CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326) 
); 

CREATE INDEX landmarks_the_geom_gist 
    ON landmarks 
    USING gist 
(the_geom); 

添加臨時兩列latitudelongitude

ALTER TABLE landmarks ADD COLUMN latitude double precision; 
ALTER TABLE landmarks ADD COLUMN longitude double precision; 

然後導入您的數據:

\copy landmarks(name,conf,capital,venture,latitude,longitude) FROM '../../../../../var/tmp/map2.csv' DELIMITERS ',' CSV HEADER; 

2.通過從latitudelongitude創建點幾何填充幾何列:

UPDATE landmarks SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326); 

最後,刪除臨時列:

ALTER TABLE landmarks DROP COLUMN latitude; 
ALTER TABLE landmarks DROP COLUMN longitude; 
0

有一個替代方法。

  1. Google pgfutter並下載相應版本的可執行文件。(https://github.com/lukasmartinelli/pgfutter/releases/download/v1.1/pgfutter_windows_amd64.exe)鏈接的窗口,但其他版本也可用。
  2. 確保pgfutter文件和yourdata.csv文件位於相同的目錄中。
  3. 在終端中設置目錄並運行以下代碼。
  4. 這也會創建一個新表格,只需輸入一個新的表格名稱即可。

    pgfutter.exe --host "localhost" --port "5432" --db "YourDatabaseName" --schema "public" --table "TableName" --user "postgres" --pw "YourPassword" csv YourData.csv