我有20多张来自3颗不同卫星的农田卫星图像。每个图像名称都包含数据收集数据及其中的卫星名称。文件名的前两位数是月份,下两位数字是日期,最后一部分是卫星名。假设这段代码将使用六个图像。
每个图像都经过一个循环,在循环中被处理成numpy数组。密码是-
image_list = ["D:/6.10.SkySat.tif", "D:/06.30.SkySat.tif", "D:/06.06.RapidEye.tif",
"D:/06.16.RapidEye.tif", "D:/06.26.PlanetScope.tif", "D:/06.30.PlanetScope.tif"]
for image in image_list:
#converting raster image to numpy array
array = arcpy.RasterToNumPyArray(image, nodata_to_value=9999)
#masking out the no data value and converting into one dimentional array
marray = numpy.ma.masked_values(array,9999)
new_array = marray.flatten()
#extracting the date and satellite name
date = image[3:8]
satellite = image[9:-4]这里我得到一个一维数组,一个日期和一个字符串(卫星名称).为了进一步使用,我希望它们的格式如下所示。数据将有三列。一个将有来自数组的所有像素值,下一个将包含日期,最后将有卫星名称。
Value Date Satellite
0.05825 6/15/2018 SkySat
0.07967976 6/15/2018 SkySat
0.09638854 6/15/2018 SkySat
0.12477265 6/15/2018 SkySat
0.13941683 6/15/2018 SkySat
0.13072205 6/15/2018 SkySat
0.12254229 6/15/2018 SkySat
0.13378483 6/15/2018 SkySat
0.13875392 6/15/2018 SkySat
0.14010076 6/10/2018 PlanetScope
0.1371166 6/10/2018 PlanetScope
0.13878246 6/10/2018 PlanetScope
0.1351179 6/10/2018 PlanetScope
0.16816537 6/10/2018 PlanetScope
0.16348109 6/10/2018 PlanetScope
0.15997969 6/10/2018 PlanetScope
0.16568226 6/10/2018 PlanetScope
0.190534599 6/12/2018 RapidEye
0.219114789 6/12/2018 RapidEye
0.251982007 6/12/2018 RapidEye
0.289779308 6/12/2018 RapidEye
0.333246204 6/12/2018 RapidEye有没有办法安排这种格式的数据,然后将其写入CSV或文本文件?
发布于 2018-11-21 06:15:02
创建一个带有pandas.DataFrame和columns=['Value', 'Date', 'Satellite']的熊猫,并通过将当前的数据和图像中的新数据连接起来,为每个图像附加新的数据。对于在每幅图像上生成的数据,您需要重复日期和卫星信息。您也可以通过pd.to_datetime将日期转换为熊猫日期格式。它应该是这样的:
import pandas as pd
import numpy
image_list = ["D:/6.10.SkySat.tif", "D:/06.30.SkySat.tif", "D:/06.06.RapidEye.tif",
"D:/06.16.RapidEye.tif", "D:/06.26.PlanetScope.tif", "D:/06.30.PlanetScope.tif"]
df = pd.DataFrame(columns=['Value', 'Date', 'Satellite'])
for image in image_list:
#converting raster image to numpy array
array = arcpy.RasterToNumPyArray(image, nodata_to_value=9999)
#masking out the no data value and converting into one dimentional array
marray = numpy.ma.masked_values(array,9999)
new_array = marray.flatten()
#extracting the date and satellite name
date = pd.datetime(image[3:8], ignore_errors=True)
satellite = image[9:-4]
df2 = pd.DataFrame({'Value': new_array, 'Date':[date]*new_array.size, 'Satellite':[satellite]*new_array.size})
df = pd.concat([df,df2], ignore_index=True)
print(df) # Should output your expected columns发布于 2018-11-21 05:55:36
欢迎来到斯塔克沃夫索拉夫!
我认为您的问题是,您只想为相应的一维数组“value”重复“日期”和“卫星名称”的值。
考虑到以下例子:
value1 = [1,2,3]
date1 = '1 sep'
satellite_name1 = 'sauravyan'您可以使用numpy的“重复”功能:
date1 = np.repeat(date1,len(value1))
satellite_name1 = np.repeat(satellite_name1, len(value_1))使日期数组重复多次。在您的情况下,值数组的长度。
要最终将所有内容转换为csv,我认为最好的方法是
(1)把所有东西都推到辞典处:
d['values'].extend(value_1)
d['dates'].extend(date_1)
d['satellites'].extend(s_1)*记得在“for”循环之前创建以“值”、“日期”和“卫星”作为键的字典。
(2)将你的辞典D‘转换成数据集:
data = pd.DataFrame(d)(3)并最终将您的数据转换为csv:
data.to_csv(<filepath/filename.csv>)看到你的代码:
只需更改“for”循环中的行即可
date = np.repeat(image[3:8], len(new_array))
#similarly for the satellite name把这三瓶酒都推到字典里去
在for循环结束后,将您的字典转换为dataframe,然后转换为csv next。
如有任何疑问,请发表评论。
希望能帮上忙。
https://stackoverflow.com/questions/53405438
复制相似问题