问题描述
我正在尝试使用模块 pyproj 编写一个 python 函数,它将基于两个因素进行坐标转换 - 文件名的结尾和 2 行的名称。
例如:if self.file_crs == 'IG'
如果文件结尾是爱尔兰网格的 IG
和
for idx,el in enumerate(row):
if keys[idx].capitalize() in ['Easting','northing']:
如果两列分别称为东行和北行
然后运行
inProj = Proj(init='epsg:29903') # Irish Grid
outProj = Proj(init='epsg:4326') # wgs84
x1,y1 = row[1],row[2] # easting,northing
x2,y2 = transform(inProj,outProj,x1,y1)
row[1],row[2] = y2,x2
我如何将这些组合起来看起来像:
if self.file_crs == 'IG' and keys[idx].capitalize() in ['Easting','northing']:
inProj = Proj(init='epsg:29903') # Irish Grid
outProj = Proj(init='epsg:4326') # wgs84
x1,northing
x2,y1)
row[1],x2
我需要能够事先引用 idx 以便在我的“if”语句中识别它
编辑
键是正在解析的 csv 中的行名称。
if line_count == 0:
keys = row
各行如下
姓名 | 小编 | 北向 | 时间 |
---|---|---|---|
测试1 | 169973 | 77712 | 01/01/2020 09:51:03 AM |
解决方法
好的,我在临时测试工具中尝试了这个,并且运行了:
class TestPositions:
def __init__(self,crs):
self.file_crs = crs
def process_incoming_file(self,bucket,key,event):
if self.file_crs == 'BNG':
inProj = Proj(init="epsg:27700") # British National Grid
outProj = Proj(init="epsg:4326") # WGS84
else:
inProj = Proj(init='epsg:29903') # Irish Grid
outProj = Proj(init='epsg:4326') # WGS84
try:
decoded_content = ['Name|Easting|Northing|Time','Test1|169973|77712|01/01/2020 09:51:03 AM']
print('processing data')
rows = csv.reader(decoded_content,delimiter='|')
for line_count,row in enumerate(rows):
if line_count == 0:
keys = [title.lower() for title in row]
print('keys',keys)
isEasting = ('easting' in keys)
else:
json_doc = {}
for idx,el in enumerate(row):
if keys[idx] in ['time','date serviced','timestamp']:
timestamp = self.format_timestring(el)
else:
json_doc[keys[idx]] = el
if isEasting:
json_doc['easting'],json_doc['northing'] = transform(inProj,outProj,json_doc['easting'],json_doc['northing'])
json_doc['latitude'] = json_doc.pop('easting')
json_doc['longitude'] = json_doc.pop('northing')
geom = Point(json_doc['Longitude'],json_doc['Latitude'])
WKB_format = wkb.dumps(geom,hex=True,srid=4326)
fid = uuid.uuid4() # assign new UUID to each row
print(json_doc['name'])
print(fid)
print(timestamp)
print(WKB_format)
print(json_doc)
except Exception as e:
print(f'Exception: {e.__class__.__name__}({e})')
您可以看到我如何在方法的开头生成 inProj
和 outProj
,因此每次调用 process_incoming_file()
都会生成一次。
我已经硬编码了 decoded_content
。您将需要原件:
response = self.client.get_object(Bucket=bucket,Key=key)
decoded_content = response['Body'].read().decode('utf-8')
print(decoded_content)
rows = csv.reader(decoded_content.splitlines(),delimiter=',')
# no need to convert to a list
您会注意到,我检查了第一行的列名并抛出了异常,因为继续处理缺少的信息将毫无意义。
此外,一旦我将单元格复制到 json_doc
中,就无需在循环中再次引用 row
。
更新:
我添加了对 'easting'
的检查,作为将出现一组列名称的代理。因此,if isEasting:
使转换发生,否则假定不需要转换。
回复以上答案
def process_incoming_file(self,event):
if self.file_crs == 'BNG':
inProj = Proj(init="epsg:27700") # British National Grid
outProj = Proj(init="epsg:4326") # WGS84
else:
inProj = Proj(init='epsg:29903') # Irish Grid
outProj = Proj(init='epsg:4326') # WGS84
try:
response = self.client.get_object(Bucket=bucket,Key=key)
decoded_content = response['Body'].read().decode('utf-8')
print(decoded_content)
print('processing data')
rows = csv.reader(decoded_content.splitlines(),')
for line_count,json_doc['northing'])
json_doc['latitude'] = json_doc.pop('easting')
json_doc['longitude'] = json_doc.pop('northing')
geom = Point(json_doc['longitude'],json_doc['latitude'])
WKB_format = wkb.dumps(geom,srid=4326)
fid = uuid.uuid4() # assign new UUID to each row
print(json_doc['name'])
print(fid)
print(timestamp)
print(WKB_format)
print(json_doc)
except Exception as e:
print(f'Exception: {e.__class__.__name__}({e})')