简体   繁体   中英

How to rasterize (in a jupyter notebook) using GDAL/OGR , crs of vector file in EPSG: 4326

I was trying to rasterize the vector file in python.

I used this code:

gdal.Grid("trial2.tif","merged2018-19.gpkg",zfield="h_li",algorithm="nearest")

(the crs of vector data is in EPSG: 4326 )

  1. On plotting the vector file in jupyter it showed the coordinates X axis: 74 to 78 (longitude value) Y axis: 35 to 38 (latitude value)

After rasterizing using this code X Axis: 0 to 250 Y Axis: 250 to 0

ie its crs value is lost (I guess)

  1. it created a raster file but with pixel resolution (0.015625 degree, 0.01171875 degree) which is quite large enough.

How can I regulate the pixel size here.

You can use the outBounds and width/height keywords to control the output resolution. For example, if you want the output resolution to be 0.005 degrees, you can define these as:

gdal.Grid(
    "trial2.tif","merged2018-19.gpkg", zfield="h_li",algorithm="nearest",
    width=600, # --- width of the output raster in pixel
    height=800, # --- height of the output raster in pixel
    outputBounds=[74, 38, 78, 35], # --- assigned output bounds: [ulx, uly, lrx, lry]
)

You get the width/height in pixels by dividing the width/height in degrees by the resolution.

600 = 3/0.005
800 = 4/0.005

You can consider also adding outputSRS="EPSG:4326" if the output CRS is not set correctly.

Btw, the term "rasterize" is normally used for converting vector features to a raster representation ("pixelized"). This is done with gdal.Rasterize . Using gdal.Grid would be interpolating the data (also create output values in between the features).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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