def processShapefile(self, tfrShapefile): """This method receives a shapefile and processes it to extract attribute/spatial information""" sf = shapefile.Reader(shp=tfrShapefile.shp, dbf=tfrShapefile.dbf, prj=tfrShapefile.prj, shx=tfrShapefile.shx) #below is the list of fields found in the FAA data. field_list = [ "NOTAM_ID", "TFR_TYPE", "TFR_AREA", "LOWER_ALT", "LALT_TYPE", "UPPER_ALT", "UALT_TYPE", "EFFECTIVE", "SHAPE@" ] shapeRecs = sf.shapeRecords() recCount = len(shapeRecs) fields = sf.fields for i in range(0, recCount): value_dict = {} value_dict['attributes'] = {} value_dict['geometry'] = {} value_dict['geometry']['rings'] = [] prow = [] recs = shapeRecs[0].record zcount = 0 for r in recs: value_dict['attributes'][field_list[zcount]] = r zcount += 1 notam = value_dict['attributes']['NOTAM_ID'] webu = "http://tfr.faa.gov/save_pages/detail_" + notam.replace( '/', '_') + ".html" value_dict['attributes']['WebLink'] = webu coords = shapeRecs[i].shape.points for pt in coords: mycoords = self.convertCoordinates(pt[0], pt[1], tfrShapefile.prj_string, 4269) lon, lat = mycoords[0], mycoords[1] prow.append([lon, lat]) value_dict['geometry']['rings'].append(prow) value_dict['geometry']['spatialReference'] = {'wkid': 4269} self.tfr_shapefile_list.append(value_dict)
def update(): src = src_root + filename dst = dst_root + filename r = shapefile.Reader(src, encoding=encoding) w = shapefile.Writer(dst, encoding=encoding) w.fields = r.fields[1:] # skip first deletion field for shaperec in r.iterShapeRecords(): # print(shaperec.shape.shapeType == shapefile.POLYGON) poly = [] for p in shaperec.shape.points: x, y = convert_point(p[0], p[1]) poly.append([x, y]) w.poly([poly]) w.record(*shaperec.record) w.close() # 保存
def update(filename, shapeType=None): src = src_root + filename filename = filename.replace('XNGIS_', '') dst = dst_root + filename r = shapefile.Reader(src, encoding=encoding) w = shapefile.Writer(dst, encoding=encoding) w.fields = r.fields[1:] # skip first deletion field if filename == 'SUBSTATION': # 增加属性字段 w.field('VOLTAGE') for shaperec in r.iterShapeRecords(): # 几何坐标转换 poly = [] for p in shaperec.shape.points: x, y = convert_point(p[0], p[1]) poly.append([x, y]) w.poly([poly]) # 转为polygon # 写属性字段 voltage_le = shaperec.record['VOLTAGE_LE'] voltage = '' if voltage_le == 17: voltage = "1000kV" elif voltage_le == 8: voltage = "unknown" elif voltage_le == 7: voltage = "500kV" elif voltage_le == 6: voltage = "220kV" elif voltage_le == 5: voltage = "110kV" w.record(*shaperec.record, voltage) elif filename == 'CABLE_SECT': w.field('CABLE_NAME') w.field('PHASE') # w.field('LABEL_0') # 名称 w.field('LABEL_1') # 名称 + 段编号 w.field('LABEL_2') # 名称 + 相位 + 段编号 for shaperec in r.iterShapeRecords(): line = [] for p in shaperec.shape.points: x, y = convert_point(p[0], p[1]) line.append([x, y]) w.line([line]) gisdesc = shaperec.record['GISDESC'] cable_name = gisdesc.replace("黄相", "").replace("绿相", "").replace("红相", "") \ .upper() \ .replace("A相", "").replace("B相", "").replace("C相", "") \ .replace('(', '(').replace(')', ')')\ .replace(' ', '') phase = '' if "黄相" in gisdesc or "A相" in gisdesc: phase = "黄相" elif "绿相" in gisdesc or "B相" in gisdesc: phase = "绿相" elif "红相" in gisdesc or "C相" in gisdesc: phase = "红相" label_1 = cable_name + ' ' + shaperec.record['NAME'] label_2 = cable_name + phase + shaperec.record['NAME'] w.record(*shaperec.record, cable_name, phase, label_1, label_2) elif filename == 'CABLE_JOINT_ONLINE': # 统计 'CABLE_SECT' 的所有电缆PID+对应名称+相位 src_cable_sect = dst_root + 'CABLE_SECT' r0 = shapefile.Reader(src_cable_sect, encoding=encoding) arr_pid = [] for shaperec in r0.iterShapeRecords(): exists = False for a in arr_pid: if shaperec.record['PID'] == a["PID"]: exists = True break if not exists: a = { "PID": shaperec.record['PID'], "CABLE_NAME": shaperec.record['CABLE_NAME'], "PHASE": shaperec.record['PHASE'] } arr_pid.append(a) w.field('CABLE_NAME') w.field('PHASE') # w.field('LABEL_0') # 名称 w.field('LABEL_1') # 名称 + 接头编号 w.field('LABEL_2') # 名称 + 相位 + 接头编号 for shaperec in r.iterShapeRecords(): p = shaperec.shape.points[0] x, y = convert_point(p[0], p[1]) w.point(x, y) name = shaperec.record['NAME'].replace( " ", "") # 此字段为接头编号, 为空时使用GISDESC字段 if name == "": shaperec.record['NAME'] = shaperec.record['GISDESC'] cable_name = '' phase = '' for a in arr_pid: if shaperec.record['PID'] == a["PID"]: cable_name = a["CABLE_NAME"] phase = a["PHASE"] break label_1 = cable_name + '#' + shaperec.record['NAME'] label_2 = cable_name + phase + '#' + shaperec.record['NAME'] w.record(*shaperec.record, cable_name, phase, label_1, label_2) elif filename == 'CABLE_SILO' or \ filename == 'CABLE_BRIDGE' or \ filename == 'CABLE_CHANGEPOS_BOX': for shaperec in r.iterShapeRecords(): poly = [] for p in shaperec.shape.points: x, y = convert_point(p[0], p[1]) poly.append([x, y]) w.poly([poly]) # 转为polygon w.record(*shaperec.record) else: if shapeType == 'Polyline': for shaperec in r.iterShapeRecords(): line = [] for p in shaperec.shape.points: x, y = convert_point(p[0], p[1]) line.append([x, y]) w.line([line]) w.record(*shaperec.record) elif shapeType == 'Point': for shaperec in r.iterShapeRecords(): p = shaperec.shape.points[0] x, y = convert_point(p[0], p[1]) w.point(x, y) w.record(*shaperec.record) w.close() # 保存
ax.patches.remove(patch) patch = patches.FancyArrowPatch((9, 56), (9, 52), mutation_scale=100 * temp) ax.add_patch(patch) time_text.set_text(time_template % (t * dt)) return patch, time_text for ii in states_list: target = folder + 'gadm36_' + ii + '_0.shp' print(target) sf = shapefile.Reader(target) shape = sf.shape() # whichever shape points = np.array(shape.points) intervals = list(shape.parts) + [len(shape.points)] for (i, j) in zip(intervals[:-1], intervals[1:]): if ii == main: ax.fill(*zip(*points[i:j]), color='blue') ax.plot(*zip(*points[i:j]), color='black') else: ax.fill(*zip(*points[i:j]), color='red') ax.plot(*zip(*points[i:j]), color='black') # import den
from pyshp import shapefile from slugify import slugify import math import PIL.ImageDraw as ImageDraw import PIL.Image as Image import random import io import csv import pandas import folium import requests import urllib import json sf = shapefile.Reader("data/QP_METROPOLEOUTREMER_WGS84_EPSG4326", encoding='latin1') QPV = sf.shapeRecords() # determine if a point is inside a given polygon or not # Polygon is a list of (x,y) pairs. def point_inside_polygon(x, y, poly): n = len(poly) inside = False p1x, p1y = poly[0] for i in range(n + 1): p2x, p2y = poly[i % n] if y > min(p1y, p2y):