2015-07-01 91 views
1

我使用gdal_polygonize python腳本來多邊形化柵格圖像並將多邊形存儲在postgres數據庫中。到目前爲止一切正常。Python gdal投影信息

gdal_polygonize.py abia.tif -f PostgreSQL PG:"dbname='abiaDB' host='127.0.0.1' port='5434' user='postgres' password='****'" mylayer 

存儲在我從那裏出口它作爲一個GeoJSON的文件,我想Leaflat顯示數據庫中的數據之後。因此Leaflat只適用於投影EPSG:4326。因此,我使用了proj4.js插件,該插件從Gauss-Kruger 4區EPSG:31468投影中進行轉換。所以我有這樣的代碼來定義原始投影manualy:

proj4.defs('EPSG:31468', '+proj=tmerc +lat_0=0 +lon_0=12 +k=1 +x_0=4500000 +y_0=0 +ellps=bessel +towgs84=598.1,73.7,418.2,0.202,0.045,-2.455,6.7 +units=m +no_defs'); 

L.Proj.geoJson(data, { 
    'pointToLayer': function(feature, latlng) { 
    return L.marker(latlng).bindPopup(feature.properties.name); 
    } 
}).addTo(map); 

有沒有一種方法,我可以說,它也應該存儲在數據庫中的投影信息的python腳本。我的目標是可視化更加自動化,因此當有其他圖像時,使用其他投影應該從數據庫獲取投影信息。有沒有辦法,我可以說gdal_polygonize函數,它應該將信息存儲在一個額外的行或類似的東西?

多邊形化的Java

gdal.AllRegister(); 
ogr.RegisterAll(); 
args = gdal.GeneralCmdLineProcessor(args); 

//Open source file 
Dataset hDataset = gdal.Open(args[0], gdalconstConstants.GA_ReadOnly);   
Band rasterBand = hDataset.GetRasterBand(1); 
Band maskBand = rasterBand.GetMaskBand(); 

Driver driver = ogr.GetDriverByName("Memory"); 
DataSource dataSource = driver.CreateDataSource("mem"); 

SpatialReference srs = null; 
if(!hDataset.GetProjectionRef().isEmpty()) 
    srs = new SpatialReference(hDataset.GetProjectionRef()); 

Layer outputLayer = dataSource.CreateLayer("mylayer", srs); 
FieldDefn field_def = new FieldDefn("DN",ogr.OFTInteger); 
outputLayer.CreateField(field_def); 
gdal.Polygonize(rasterBand, maskBand, outputLayer, 0, new Vector<>(), new TermProgressCallback()); 

//Transformation  
DataSource dataDest = driver.CreateDataSource("mem2"); 
//Create destination projection 
SpatialReference dst = new SpatialReference(); 
dst.ImportFromEPSG(4326); 

CoordinateTransformation ct = CoordinateTransformation.CreateCoordinateTransformation(srs, dst); 

//Write data to database 
DataSource dataSourceDb = ogr.Open("PG:dbname='abiaDB' host='127.0.0.1' port='5434' user='postgres' password='****'", 1); 
dataSourceDb.CopyLayer(outputLayer, "mylayer"); 

回答

1

一個想法是,你可以修改gdal_polygonize.py重新計劃的結果。使用MEM驅動程序存儲來自dst_layer的臨時polygonised結果,然後使用osr模塊重新投影。

另一個想法是簡單得多的是使數據庫視圖與SRID = 4326使用項目表的幾何列,即

CREATE VIEW mytable_latlong AS 
    SELECT gid, ST_Transform(geom, 4326) AS geom, ... 
    FROM mytable 
+0

您好,感謝您的回覆。我是用Java做的,添加了上面的代碼。在這個例子中,我將結果多邊形化到數據庫後寫入。在寫入數據庫之前,您是否有過如何進行轉換的示例? – user2644964

+0

好吧,我把結果存儲到mem,然後寫入數據庫工作。我只在轉換過程中遇到問題。 – user2644964