Python将WKT多边形转换为行点
如何在csv文件中获得以下内容: UID(如101102等)表示每个多边形的唯一标识符Python将WKT多边形转换为行点,python,latitude-longitude,points,polygons,wkt,Python,Latitude Longitude,Points,Polygons,Wkt,如何在csv文件中获得以下内容: UID(如101102等)表示每个多边形的唯一标识符 "POLYGON ((12 13,22 23,16 17,22 24))",101,Something,100000 "POLYGON ((10 12,40 42,46 34,16 24,88 22,33 24,18 20 ))",102,another,200000 下面是一个使用的解决方案。如果这对您不起作用,请告诉我-想出只使用标准库的东西应该不会太难(例如,re,等等),但肯定会更难看 UID#1,
"POLYGON ((12 13,22 23,16 17,22 24))",101,Something,100000
"POLYGON ((10 12,40 42,46 34,16 24,88 22,33 24,18 20 ))",102,another,200000
下面是一个使用的解决方案。如果这对您不起作用,请告诉我-想出只使用标准库的东西应该不会太难(例如,re
,等等),但肯定会更难看
UID#1,County,population,Point#1_Lat,Point#1_Long
UID#1,County,population,Point#2_Lat,Point#2_Long
UID#1,County,population,Point#3_Lat,Point#3_Long
UID#1,County,population,Point#n_Lat,Point#n_Long
UID#2,County,population,Point#1_Lat,Point#1_Long
UID#2,County,population,Point#2_Lat,Point#2_Long
UID#2,County,population,Point#3_Lat,Point#3_Long
UID#2,County,population,Point#n_Lat,Point#n_Long
结果:
import csv
from pyparsing import Group, Literal, OneOrMore, Optional, Word
from pyparsing import delimitedList
from pyparsing import alphas, nums
data = """
"POLYGON ((12 13,22 23,16 17,22 24))",101,Something,100000
"POLYGON ((10 12,40 42,46 34,16 24,88 22,33 24,18 20 ))",102,another,200000
"""
def parse_line(line):
latitude = Word(nums)
longitude = Word(nums)
point = Group(latitude + longitude)
point_sequence = delimitedList(point, delim=',')
name = Word("POLYGON").suppress()
paren_left = Literal("((").suppress()
paren_right = Literal("))").suppress()
quote = Literal('"').suppress()
polygon = Group(quote + name + paren_left + point_sequence + paren_right + quote)
uid = Word(nums)
county = Word(alphas)
population = Word(nums)
sep = Literal(",").suppress()
parser = polygon + sep + uid + sep + county + sep + population
result = parser.parseString(line)
return result
def parse_lines(data, outfile):
with open(outfile, 'w') as f:
writer = csv.writer(f, lineterminator='\n')
lines = data.split('\n')
for line in lines:
if not line:
continue
points, uid, county, population = parse_line(line)
for lat, long in points:
writer.writerow([uid, county, population, lat, long])
writer.writerow('')
parse_lines(data, r'd:\out.txt') # change the path to wherever you want output
下面是一个使用的解决方案。如果这对您不起作用,请告诉我-想出只使用标准库的东西应该不会太难(例如,re
,等等),但肯定会更难看
UID#1,County,population,Point#1_Lat,Point#1_Long
UID#1,County,population,Point#2_Lat,Point#2_Long
UID#1,County,population,Point#3_Lat,Point#3_Long
UID#1,County,population,Point#n_Lat,Point#n_Long
UID#2,County,population,Point#1_Lat,Point#1_Long
UID#2,County,population,Point#2_Lat,Point#2_Long
UID#2,County,population,Point#3_Lat,Point#3_Long
UID#2,County,population,Point#n_Lat,Point#n_Long
结果:
import csv
from pyparsing import Group, Literal, OneOrMore, Optional, Word
from pyparsing import delimitedList
from pyparsing import alphas, nums
data = """
"POLYGON ((12 13,22 23,16 17,22 24))",101,Something,100000
"POLYGON ((10 12,40 42,46 34,16 24,88 22,33 24,18 20 ))",102,another,200000
"""
def parse_line(line):
latitude = Word(nums)
longitude = Word(nums)
point = Group(latitude + longitude)
point_sequence = delimitedList(point, delim=',')
name = Word("POLYGON").suppress()
paren_left = Literal("((").suppress()
paren_right = Literal("))").suppress()
quote = Literal('"').suppress()
polygon = Group(quote + name + paren_left + point_sequence + paren_right + quote)
uid = Word(nums)
county = Word(alphas)
population = Word(nums)
sep = Literal(",").suppress()
parser = polygon + sep + uid + sep + county + sep + population
result = parser.parseString(line)
return result
def parse_lines(data, outfile):
with open(outfile, 'w') as f:
writer = csv.writer(f, lineterminator='\n')
lines = data.split('\n')
for line in lines:
if not line:
continue
points, uid, county, population = parse_line(line)
for lat, long in points:
writer.writerow([uid, county, population, lat, long])
writer.writerow('')
parse_lines(data, r'd:\out.txt') # change the path to wherever you want output
下面是一个使用的解决方案。如果这对您不起作用,请告诉我-想出只使用标准库的东西应该不会太难(例如,re
,等等),但肯定会更难看
UID#1,County,population,Point#1_Lat,Point#1_Long
UID#1,County,population,Point#2_Lat,Point#2_Long
UID#1,County,population,Point#3_Lat,Point#3_Long
UID#1,County,population,Point#n_Lat,Point#n_Long
UID#2,County,population,Point#1_Lat,Point#1_Long
UID#2,County,population,Point#2_Lat,Point#2_Long
UID#2,County,population,Point#3_Lat,Point#3_Long
UID#2,County,population,Point#n_Lat,Point#n_Long
结果:
import csv
from pyparsing import Group, Literal, OneOrMore, Optional, Word
from pyparsing import delimitedList
from pyparsing import alphas, nums
data = """
"POLYGON ((12 13,22 23,16 17,22 24))",101,Something,100000
"POLYGON ((10 12,40 42,46 34,16 24,88 22,33 24,18 20 ))",102,another,200000
"""
def parse_line(line):
latitude = Word(nums)
longitude = Word(nums)
point = Group(latitude + longitude)
point_sequence = delimitedList(point, delim=',')
name = Word("POLYGON").suppress()
paren_left = Literal("((").suppress()
paren_right = Literal("))").suppress()
quote = Literal('"').suppress()
polygon = Group(quote + name + paren_left + point_sequence + paren_right + quote)
uid = Word(nums)
county = Word(alphas)
population = Word(nums)
sep = Literal(",").suppress()
parser = polygon + sep + uid + sep + county + sep + population
result = parser.parseString(line)
return result
def parse_lines(data, outfile):
with open(outfile, 'w') as f:
writer = csv.writer(f, lineterminator='\n')
lines = data.split('\n')
for line in lines:
if not line:
continue
points, uid, county, population = parse_line(line)
for lat, long in points:
writer.writerow([uid, county, population, lat, long])
writer.writerow('')
parse_lines(data, r'd:\out.txt') # change the path to wherever you want output
下面是一个使用的解决方案。如果这对您不起作用,请告诉我-想出只使用标准库的东西应该不会太难(例如,re
,等等),但肯定会更难看
UID#1,County,population,Point#1_Lat,Point#1_Long
UID#1,County,population,Point#2_Lat,Point#2_Long
UID#1,County,population,Point#3_Lat,Point#3_Long
UID#1,County,population,Point#n_Lat,Point#n_Long
UID#2,County,population,Point#1_Lat,Point#1_Long
UID#2,County,population,Point#2_Lat,Point#2_Long
UID#2,County,population,Point#3_Lat,Point#3_Long
UID#2,County,population,Point#n_Lat,Point#n_Long
结果:
import csv
from pyparsing import Group, Literal, OneOrMore, Optional, Word
from pyparsing import delimitedList
from pyparsing import alphas, nums
data = """
"POLYGON ((12 13,22 23,16 17,22 24))",101,Something,100000
"POLYGON ((10 12,40 42,46 34,16 24,88 22,33 24,18 20 ))",102,another,200000
"""
def parse_line(line):
latitude = Word(nums)
longitude = Word(nums)
point = Group(latitude + longitude)
point_sequence = delimitedList(point, delim=',')
name = Word("POLYGON").suppress()
paren_left = Literal("((").suppress()
paren_right = Literal("))").suppress()
quote = Literal('"').suppress()
polygon = Group(quote + name + paren_left + point_sequence + paren_right + quote)
uid = Word(nums)
county = Word(alphas)
population = Word(nums)
sep = Literal(",").suppress()
parser = polygon + sep + uid + sep + county + sep + population
result = parser.parseString(line)
return result
def parse_lines(data, outfile):
with open(outfile, 'w') as f:
writer = csv.writer(f, lineterminator='\n')
lines = data.split('\n')
for line in lines:
if not line:
continue
points, uid, county, population = parse_line(line)
for lat, long in points:
writer.writerow([uid, county, population, lat, long])
writer.writerow('')
parse_lines(data, r'd:\out.txt') # change the path to wherever you want output
感谢senshin提供的解决方案。这是我第一次尝试Python。我尝试了你的建议。我尝试了解决方案,并取得了良好的效果 几何图形、分区、类型、UID
“多边形(x1-y1,x2-y2,x3-y3,x4-y4)”,名称1,abc,100
“多边形(x1 y1、x2 y2、x3 y3、x4 y4、x5 y5、x6 y6)”,名称2,pqr,101
101,Something,100000,12,13
101,Something,100000,22,23
101,Something,100000,16,17
101,Something,100000,22,24
102,another,200000,10,12
102,another,200000,40,42
102,another,200000,46,34
102,another,200000,16,24
102,another,200000,88,22
102,another,200000,33,24
102,another,200000,18,20
感谢senshin提供的解决方案。这是我第一次尝试Python。我尝试了你的建议。我尝试了解决方案,并取得了良好的效果 几何图形、分区、类型、UID
“多边形(x1-y1,x2-y2,x3-y3,x4-y4)”,名称1,abc,100
“多边形(x1 y1、x2 y2、x3 y3、x4 y4、x5 y5、x6 y6)”,名称2,pqr,101
101,Something,100000,12,13
101,Something,100000,22,23
101,Something,100000,16,17
101,Something,100000,22,24
102,another,200000,10,12
102,another,200000,40,42
102,another,200000,46,34
102,another,200000,16,24
102,another,200000,88,22
102,another,200000,33,24
102,another,200000,18,20
感谢senshin提供的解决方案。这是我第一次尝试Python。我尝试了你的建议。我尝试了解决方案,并取得了良好的效果 几何图形、分区、类型、UID
“多边形(x1-y1,x2-y2,x3-y3,x4-y4)”,名称1,abc,100
“多边形(x1 y1、x2 y2、x3 y3、x4 y4、x5 y5、x6 y6)”,名称2,pqr,101
101,Something,100000,12,13
101,Something,100000,22,23
101,Something,100000,16,17
101,Something,100000,22,24
102,another,200000,10,12
102,another,200000,40,42
102,another,200000,46,34
102,another,200000,16,24
102,another,200000,88,22
102,another,200000,33,24
102,another,200000,18,20
感谢senshin提供的解决方案。这是我第一次尝试Python。我尝试了你的建议。我尝试了解决方案,并取得了良好的效果 几何图形、分区、类型、UID
“多边形(x1-y1,x2-y2,x3-y3,x4-y4)”,名称1,abc,100
“多边形(x1 y1、x2 y2、x3 y3、x4 y4、x5 y5、x6 y6)”,名称2,pqr,101
101,Something,100000,12,13
101,Something,100000,22,23
101,Something,100000,16,17
101,Something,100000,22,24
102,another,200000,10,12
102,another,200000,40,42
102,another,200000,46,34
102,another,200000,16,24
102,another,200000,88,22
102,another,200000,33,24
102,another,200000,18,20
在你的第一排,“某物”是指县,而“100000”是指人口吗?在你的第一排,“某物”是指县,而“100000”是指人口吗?在你的第一排,“某物”是指县,而“100000”是指人口吗?嗨,谢谢你的回答。下面是错误,回溯(最后一次调用):parse_lines(数据,r'd:\out.txt)中的第45行文件“C:\Users\DURGA\Desktop\ttt.py”,parse_lines writer=csv.writer(outfile)中的第34行文件“C:\Users\DURGA\Desktop\ttt.py”类型错误:参数1必须有“write”方法哦,对不起,我弄糟了。让我来解决这个问题,请稍等。@DurgaPrasadDhulipudi我们开始了。它现在应该可以工作了。请确保将
r'd:\out.txt'
更改到您要保存输出的任何位置。让我知道这是否有效。感谢您的展示,除了正则表达式之外,它看起来非常有用。您好,感谢您的响应。我得到下面的错误,Traceback(上次调用):parse_lines(数据,r'd:\out.txt)文件“C:\Users\DURGA\Desktop\ttt.py”第45行的文件“C:\Users\DURGA\Desktop\ttt.py”,parse_lines writer=csv.writer(outfile)中的第34行。类型错误:参数1必须有“write”方法哦,对不起,我弄糟了。让我来解决这个问题,请稍等。@DurgaPrasadDhulipudi我们开始了。它现在应该可以工作了。请确保将r'd:\out.txt'
更改到您要保存输出的任何位置。让我知道这是否有效。感谢您的展示,除了正则表达式之外,它看起来非常有用。您好,感谢您的响应。我得到下面的错误,Traceback(上次调用):parse_lines(数据,r'd:\out.txt)文件“C:\Users\DURGA\Desktop\ttt.py”第45行的文件“C:\Users\DURGA\Desktop\ttt.py”,parse_lines writer=csv.writer(outfile)中的第34行。类型错误:参数1必须有“write”方法哦,对不起,我弄糟了。让我来解决这个问题,请稍等。@DurgaPrasadDhulipudi我们开始了。它现在应该可以工作了。请确保将r'd:\out.txt'
更改到您要保存输出的任何位置。让我知道这是否有效。感谢您的展示,除了正则表达式之外,它看起来非常有用。您好,感谢您的响应。我得到下面的错误,Traceback(上次调用):parse_lines(数据,r'd:\out.txt)文件“C:\Users\DURGA\Desktop\ttt.py”第45行的文件“C:\Users\DURGA\Desktop\ttt.py”,parse_lines writer=csv.writer(outfile)中的第34行。类型错误:参数1必须有“write”方法哦,对不起,我弄糟了。让我来解决这个问题,请稍等。@DurgaPrasadDhulipudi我们开始了。它现在应该可以工作了。请确保将r'd:\out.txt'
更改到您要保存输出的任何位置。如果可以,请告诉我。感谢您的展示,除了正则表达式之外,它看起来非常有用。