Python gdal投影信息

标签 python postgresql leaflet gdal

我正在使用 gdal_polygonize python 脚本对光栅图像进行多边形化并将多边形存储在 postgres 数据库中。到目前为止一切正常。

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 zone 4 EPSG:31468 投影进行转换。所以我必须像这样在代码中手动定义原始投影:

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");   

最佳答案

一个想法是您可以修改 gdal_polygonize.py重新投影结果。使用 MEM 驱动程序存储来自 dst_layer 的临时多边形化结果,然后使用 osr 模块重新投影。

另一个更简单的想法是使用 创建一个数据库 VIEW,该数据库 View 使用 SRID=4326 投影表的几何列。 ,即

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

关于Python gdal投影信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31156375/

相关文章:

python - Pandas DataFrame 到 Excel 单元格对齐

python - 使用 psycopg2 缓存中间表

javascript - 如何使用 Shadow DOM 中的 div 作为 Leaflet map 容器?

leaflet - 使用传单拖动标记时使用折线标记

java - Google FooBar意外评估失败

python - 使用 asyncio 协同程序进行方法链接

python - 如何从 SQLAlchemy 模型中确定子对象的类型?

sql - 同一张表上的postgres递归查询

java - Hibernate序列生成器始终为0

javascript - 并非所有 d3 点都显示在传单 map 上的正确位置