[英]csv to raster python
3 column csv (Lon, Lat, Ref) (63000 rows) and I would like to convert the "Ref" to raster. 3 列 csv(Lon、Lat、Ref)(63000 行),我想将“Ref”转换为栅格。 points (x,y) are being plotted.
正在绘制点 (x,y)。 I want to plot the "Ref" column and add contour and color-fill it.
我想绘制“Ref”列并添加轮廓和颜色填充它。 Thanks
谢谢
Data:数据:
Lon,Lat, Ref
-115.0377,51.9147,0
-115.0679,51.9237,0
-115.0528,51.9237,0
-115.0377,51.9237,0
-115.1134,51.9416,0
-115.0982,51.9416,0
-115.0831,51.9416,0
-115.1437,51.9596,6
-115.1285,51.9596,6
-115.1588,51.9686,6
-115.1437,51.9686,10.5
-115.1285,51.9686,10.5
-115.1134,51.9686,8
-115.1891,51.9776,7.5
-115.174,51.9776,7.5
-115.1588,51.9776,7.5
-115.1437,51.9776,8
-115.1285,51.9776,8
-115.1134,51.9776,8
-115.1891,51.9866,7
-115.174,51.9866,7
-115.1588,51.9866,7
-115.1437,51.9866,0
-115.1285,51.9866,0
-115.1134,51.9866,0
-115.1891,51.9956,7
-113.1143,52.2385,3.5
-113.0992,52.2475,3.5
-113.084,52.2475,3.5
-113.0689,52.2475,5.5
-113.0537,52.2475,5.5
Code:代码:
import pandas as pd
import geopandas
from shapely.geometry import Point
import fiona
import matplotlib.pyplot as plt
df=pd.read_csv('name.csv')
df1=df.interpolate()
geometry=[Point(xyz) for xyz in zip(df1.ix[:,0], df1.ix[:,1], df1.ix[:,2])]
df3=geopandas.GeoDataFrame(df1, geometry=geometry)
df3.plot()
plt.savefig('raster.tiff')
If you want to plot points from GeoPandas based on the "Ref" column, you don't need it as az coordinate.如果您想根据“参考”列绘制来自 GeoPandas 的点,则不需要将其作为 az 坐标。
import pandas as pd
import geopandas
from shapely.geometry import Point
import matplotlib.pyplot as plt
df = pd.read_csv('name.csv')
geometry = [Point(xy) for xy in zip(df.iloc[:, 0], df.iloc[:, 1])]
gdf = geopandas.GeoDataFrame(df, geometry=geometry)
gdf.plot(column=' Ref')
plt.savefig('raster.tiff')
You don't even need interpolate()
.你甚至不需要
interpolate()
。 However, if you want to convert your vector point dataset to raster geoTIFF, plot()
is not the right way to do it.但是,如果要将矢量点数据集转换为栅格 geoTIFF,
plot()
不是正确的方法。 I would go for gdal.Grid()
as explained here.我会去
gdal.Grid()
解释here。 - [Python - gdal.Grid() correct use][1] - [Python - gdal.Grid() 正确使用][1]
EDIT Using gdal.Grid()
like this I am able to generate tif based on the sample of data you provided.编辑像这样使用
gdal.Grid()
我能够根据您提供的数据样本生成 tif。
import os
import gdal
dir_with_csvs = r"/home/panda"
os.chdir(dir_with_csvs)
def find_csv_filenames(path_to_dir, suffix=".csv"):
filenames = os.listdir(path_to_dir)
return [ filename for filename in filenames if filename.endswith(suffix) ]
csvfiles = find_csv_filenames(dir_with_csvs)
for fn in csvfiles:
vrt_fn = fn.replace(".csv", ".vrt")
lyr_name = fn.replace('.csv', '')
out_tif = fn.replace('.csv', '.tiff')
with open(vrt_fn, 'w') as fn_vrt:
fn_vrt.write('<OGRVRTDataSource>\n')
fn_vrt.write('\t<OGRVRTLayer name="%s">\n' % lyr_name)
fn_vrt.write('\t\t<SrcDataSource>%s</SrcDataSource>\n' % fn)
fn_vrt.write('\t\t<GeometryType>wkbPoint</GeometryType>\n')
fn_vrt.write('\t\t<GeometryField encoding="PointFromColumns" x="Lon" y="Lat" z="Ref"/>\n')
fn_vrt.write('\t</OGRVRTLayer>\n')
fn_vrt.write('</OGRVRTDataSource>\n')
output = gdal.Grid('outcome.tif','name.vrt')
# below using your settings - I don't have sample large enough to properly test it, but it is generating file as well
output2 = gdal.Grid('outcome2.tif','name.vrt', algorithm='invdist:power=2.0:smoothing=1.0')
Do you have any particular reason to use gdal via shell?你有什么特别的理由通过 shell 使用 gdal 吗? [1]: https://gis.stackexchange.com/questions/254330/python-gdal-grid-correct-use
[1]: https : //gis.stackexchange.com/questions/254330/python-gdal-grid-correct-use
@ctvtkar, I am attaching a code here using gdal. @ctvtkar,我在这里使用 gdal 附加了一个代码。 When I run it, the file.vrt gets created, but not the .tif file.
当我运行它时,会创建 file.vrt,但不会创建 .tif 文件。 The erroe i get is: gdal_grid: not found.
我得到的错误是:gdal_grid:未找到。 gdal is instaled
gdal 已安装
Code:代码:
import subprocess
import os
dir_with_csvs = r"/home/panda"
os.chdir(dir_with_csvs)
def find_csv_filenames(path_to_dir, suffix=".csv"):
filenames = os.listdir(path_to_dir)
return [ filename for filename in filenames if filename.endswith(suffix) ]
csvfiles = find_csv_filenames(dir_with_csvs)
for fn in csvfiles:
vrt_fn = fn.replace(".csv", ".vrt")
lyr_name = fn.replace('.csv', '')
out_tif = fn.replace('.csv', '.tiff')
with open(vrt_fn, 'w') as fn_vrt:
fn_vrt.write('<OGRVRTDataSource>\n')
fn_vrt.write('\t<OGRVRTLayer name="%s">\n' % lyr_name)
fn_vrt.write('\t\t<SrcDataSource>%s</SrcDataSource>\n' % fn)
fn_vrt.write('\t\t<GeometryType>wkbPoint</GeometryType>\n')
fn_vrt.write('\t\t<GeometryField encoding="PointFromColumns" x="Lon" y="Lat" z="Ref"/>\n')
fn_vrt.write('\t</OGRVRTLayer>\n')
fn_vrt.write('</OGRVRTDataSource>\n')
gdal_cmd = 'gdal_grid -a invdist:power=2.0:smoothing=1.0 -zfield "Ref" -of GTiff -ot Float64 -l %s %s %s' % (lyr_name, vrt_fn, out_tif)
subprocess.call(gdal_cmd, shell=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.