python - 将数据转换为 GeoJson

标签 python geojson

我有一个文本文件,想将数据格式更改为 GeoJson 格式。首先,我想将它解析为单独的部分。我无法使用 python 编写代码来做到这一点。你能帮我解析数据并制作 GeoJson 格式吗?初始数据格式如下。

.A DAQT2 170314 C DH124045 /HGIRS 479.7:

.A DAMT2 170314 C DH115756 /HGIRS 425.1:

.A DBQT2 170314 C DH123840 /HGIRS 436.6:

.A DBDT2 170314 C DH120419 /HGIRS 472.3:

.A DDDT2 170314 C DH120204 /HGIRS 400.6:

.A DCNT2 170314 C DH120806 /HGIRS 412.9:

.A DBUT2 170314 C DH124633 /HGIRS 404.1:

.A DBBT2 170314 C DH121511 /HGIRS 453.4:

.A DDNT2 170314 C DH123336 /HGIRS 0.2:

.A DAXT2 170314 C DH123031 /HGIRS 438.1:

到目前为止,我生成了以下代码来首先分离数据(解析它们),如下所示,但它返回一行数据。我希望逐行输出。

file = open('test1.txt', 'r')
lines = file.readlines()
length=lines[0].split('\n')
number_of_lines=len(length)

lines=str(lines)
data=lines.split(' ')
data=str(data)
data=data.split('\n')

file.close()
file = open('test1.txt', 'w')

file.write(str(data))
file.write('\n')
file.close()

file = open('test1.txt', 'w')
file.write(str(data))
file.write('\n')
file.close()

输出如下:

['["[\':", \'1117\', \'AM\', \'CDT\', \'TUE\', \'MAR\', \'14\', 
"2017\\\\n\',", "\':\\\\n\',", "\':", \'HOURLY\', \'ACCUMULATOR\', 
\'INFORMATION\', "TABLE\\\\n\',", "\':\\\\n\',", "\':", \'NOTE:\', \'\', 
\'ERRONEOUS\', \'REPORTS\', \'MAY\', \'BE\', \'RECEIVED\', \'UNDER\', 
"CERTAIN\\\\n\',", "\':", \'\', \'\', \'\', \'\', \'\', \'\', \'\', 
\'WEATHER\', "CONDITIONS\\\\n\',", "\':\\\\n\',", "\':", 
"**********************************************************\\\\n\',", "\':", 
\'ID\', \'\', \'\', \'\', \'LOCATION\', \'\', \'\', \'\', \'\', \'\', \'\', 
\'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', 
\'ACCUMULATOR\', "VALUE\\\\n\',", "\':", 
"**********************************************************\\\\n\',", 
"\':CITY", \'OF\', \'DALLAS\', \'ALERT\', "SYSTEM\\\\n\',", "\'.A", 
\'DDFT2\', \'170314\', \'C\', \'DH103231\', \'/HGIRS\', "516.8:\\\\n\',", 
"\'.A", \'DCVT2\', \'170314\', \'C\', \'DH110026\', \'/HGIRS\', 
"536.0:\\\\n\',", "\'.A", \'DDJT2\', \'170314\', \'C\', \'DH102056\', 
\'/HGIRS\', "0.0:\\\\n\',", "\'.A", \'DDUT2\', \'170314\', \'C\', 
\'DH100503\', \'/HGIRS\', "3.6:\\\\n\',", "\'.A", \'DDUT2\', \'170314\', 
\'C\', \'DH100533\', \'/HGIRS\', "3.9:\\\\n\',", "\'.A", \'DDUT2\', 
\'170314\', \'C\', \'DH100603\', \'/HGIRS\', "4.0:\\\\n\',", "\'.A", 
\'DDUT2\', \'170314\', \'C\', \'DH100703\', \'/HGIRS\', "4.2:\\\\n\',", 
"\'.A", \'DDUT2\', \'170314\', \'C\', \'DH101603\', \'/HGIRS\', 
"4.5:\\\\n\',", "\'.A", \'DDUT2\', \'170314\', \'C\', \'DH103404\', 
\'/HGIRS\', "4.7:\\\\n\',", "\'.A", \'DDWT2\', \'170314\', \'C\', 
\'DH100740\', \'/HGIRS\', "0.0:\\\\n\',", "\'.A", \'DCXT2\', \'170314\', 
\'C\', \'DH105825\', \'/HGIRS\', "614.1:\\\\n\',", "\'.A", \'DCRT2\', 
\'170314\', \'C\', \'DH101815\', \'/HGIRS\', "381.1:\\\\n\',", "\'.A", 
\'DBGT2\', \'170314\', \'C\', \'DH103921\', \'/HGIRS\', "394.2:\\\\n\',", 
"\'.A", \'DBUT2\', \'170314\', \'C\', \'DH105533\', \'/HGIRS\', 
"404.1:\\\\n\',", "\'.A", \'DBRT2\', \'170314\', \'C\', \'DH101936\', 
\'/HGIRS\', "447.6:\\\\n\',", "\'.A", \'DBRT2\', \'170314\', \'C\', 
\'DH103306\', \'/HGIRS\', "447.6:\\\\n\',", "\'.A", \'DBZT2\', \'170314\', 
\'C\', \'DH102317\', \'/HGIRS\', "397.1:\\\\n\',", "\'.A", \'DATT2\', 
\'170314\', \'C\', \'DH102058\', \'/HGIRS\', "448.6:\\\\n\']"]']

最佳答案

如果你拆分(' ') "Hello world/n Hi someone/n"输出将是

['Hello','world/n','Hi','someone/n']

然后如果你使用 str 转换结果将是

"['Hello','world/n','Hi','someone/n']"

解决方案是在第一个数组的每个元素中执行 for

data = lines.split('/n')
data[:] = [x.split(' ') for x in data]

这样数据的输出将是:

[['Hello', 'world'], ['Hi','someone']]

然后您可以使用for 逐行写入该数据。

对于这个结果:

Hello
world
Hi
someone

只是:

for line in data:
   for word in line:
      file.write("%s/n" % word)

如果你想要的输出是:

Hello world
Hi someone

然后:

for line in data:
   for word in line:
      file.write(word)
   file.write('/n')

提示, 您还可以将数据加入数组中:

>>>a = ["This","is","a","sentence"]
>>>"/n".join(a)
This
is 
a 
sentence

但是如果你尝试这样做,它会给你一个错误:

>>>a = [["This","is","a","sentence"],["An","Other","sentence"]]
>>>"/n".join(a)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, list found

但是你可以试试:

>>>a = [["This","is","a","sentence"],["An","Other","sentence"]]
>>>a[:] = [' '.join(x) for x in a]
>>>a
['This is a sentence', 'An Other sentence']

关于python - 将数据转换为 GeoJson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42793580/

相关文章:

python - sqlalchemy.exc.InvalidRequestError : One or more mappers failed to initialize - can't proceed with initialization of other mappers 错误

python - 生成器表达式中的多个产量? Python

python - Web2py:运行时出现内部错误

iOS:geojson iOS 访问

php - 将 GeoPHP getArea() 值转换为 km2 或 Acres

python - 检查一个列表是否存在于另一个列表中?

python - Struct.unpack 和字节对象的长度

javascript - D3 - 大型 GeoJSON 文件未使用投影正确显示绘制 map

django - 如何使用 View 从 PostGIS 中提取几何图形,然后使用 Django 将其添加到模板中的传单 map

javascript - 无法循环 javascript 对象