简体   繁体   English

QGIS python - 获取栅格单位

[英]QGIS python - get units of the raster

In QGIS with pyQgis I would like to determine if input raster has units in degrees or metres.在带有 pyQgis 的 QGIS 中,我想确定输入栅格的单位是度还是米。 Here I learned I can get projection from raster using GDAL:在这里,我了解到我可以使用 GDAL 从光栅中获取投影:

inRaster = gdal.Open(rasterInPath,GA_ReadOnly)
projRef = inRaster.GetProjection()
print(projRef)

what's gives me string for raster with EPSG 4326:什么给了我带有 EPSG 4326 的栅格字符串:

GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]

and for raster with EPSG 3857:对于 EPSG 3857 的光栅:

PROJCS["WGS 84 / Pseudo-Mercator",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Mercator_1SP"],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",EAST],AXIS["Y",NORTH],EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"],AUTHORITY["EPSG","3857"]]

I guess I can do:我想我可以做到:

isInMetres = "PROJCS" in projRef

But is there any other solution?但是还有其他解决方案吗? I don't consider this very readable and not sure if I can cover all corner cases this way (eg missing projRef).我不认为这是非常可读的,并且不确定我是否可以通过这种方式涵盖所有极端情况(例如缺少 projRef)。 Dont need to be necessary with GDAL.不需要GDAL。

Using PyQgis API you can use the mapUnits method of QgsCoordinateReferenceSystem objects.使用PyQgis API您可以使用QgsCoordinateReferenceSystem对象的mapUnits方法。 It returns a QgsUnitTypes.DistanceUnit value, so you can use it like that for example:它返回一个QgsUnitTypes.DistanceUnit值,因此您可以像这样使用它,例如:

# lyr is a QgsRasterLayer
crs = lyr.crs()
# crs is a QgsCoordinateReferenceSystem 
unit = crs.mapUnits()
print('Unit is {}'.format(QgsUnitTypes.toString(unit)))
# 'Unit is mètres' (i'm using french language)

Reusing your code, you can write:重用您的代码,您可以编写:

isInMetres = crs.mapUnits() == 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM