2014-01-16 5 views
0
"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 

Как я могу получить что-то, как показано ниже в файле CSV:Python преобразовать WKT многоугольник грести мудрые точки

UID (например, 101,102 и т.д.) представляет собой уникальный идентификатор для каждого полигона.

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 
+0

В своем первом ряду, это "что-то" округ и "100000" население? – senshin

ответ

1

Решение проблемы с использованием pyparsing. Дайте мне знать, если это не сработает для вас - не должно быть слишком сложно придумать что-то, что использует только стандартную библиотеку (например, re и т. Д.), Но это определенно будет уродливее.

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 

Результат:

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 
+0

Привет, Спасибо, что ответили. Я получаю ниже ошибки, Traceback (последний последний звонок): Файл «C: \ Users \ DURGA \ Desktop \ ttt.py», строка 45, в parse_lines (data, r'd : \ out.txt ') Файл «C: \ Users \ DURGA \ Desktop \ ttt.py», строка 34, в parse_lines writer = csv.writer (outfile) TypeError: аргумент 1 должен иметь метод «write» – addcolor

+0

О, извините, я испортил. Позвольте мне исправить это, всего минуту. – senshin

+0

@DurgaPrasadDhulipudi Там мы идем. Теперь он должен работать. Обязательно измените 'r'd: \ out.txt'', где бы вы не захотели сохранить результат. Дайте мне знать, если это сработает. – senshin

1

Спасибо senshin за solution.That была моя первая попытка Python.I попробовал ваш suggestion.Did попытаться выработать альтернативу и получили хороший результат.

геометрия, зона, тип, UID
"ПОЛИГОН (x1 y1, x2 y2, x3 у3, х4 у4)", имя1, ABC, 100
«ПОЛИГОН (x1 y1, x2 y2, x3 у3 , x4 y4, x5 у5, x6 Y6)», name2, PQR, 101

import csv 
import re 
import sys 


l_InputFileName ='D:/Example1.txt'    # make changes here.. 
l_OutputFileName ='D:/Example1_o.txt'   # make changes here.. 

fo = open(l_OutputFileName, "a+") 
with open(l_InputFileName, 'r') as csvfile: 

csvR = csv.reader(csvfile, delimiter=',', quotechar='"') 
#Get first row which has column names 
header = csvR.next() 
#Convert first row which is a list into a string 
print ','.join(header[0:]) 
#Write the header to output csv file,index starts at 1 here(splitting point into lat and  long) 
fo.write('longitude,latitude,') 
fo.write(','.join(header[1:])+'\n') 

for row in csvR: 

    #Remove the string POLYGON and brackets from first column 
    coodlist=row[0].strip('POLYGON').strip('()').split(',') 
    #Get the number of columns 
    #print "\nNo of Columns="+str(len(row)) 
    #Get remaining columns 
    strRemainingCols =','.join(row[1:]) 
    #Print each lat,long in a seperate row..reamining columns will not change 
    #Seperate latitude and longitude using comma 
    for i in range(len(coodlist)): 
     print coodlist[i].replace(' ',',')+','+strRemainingCols 
     fo.write(coodlist[i].replace(' ',',')+','+strRemainingCols+'\n') 

csvfile.close() 
fo.close() 
Смежные вопросы