def main(): shapefileName = "shapefiles/路網數值圖103年_西屯區道路路段" sys.stdout.write("Start read shapefile {}\n".format(shapefileName)) sf = shapefile.Reader(shapefileName) m = tools.create_mapping(sf.fields) parser = argparse.ArgumentParser() parser.add_argument('server', help='server ip') parser.add_argument('username', help='user name') parser.add_argument('password', help='user password') parser.add_argument('database', help='database name') args = parser.parse_args() conn = pymssql.connect(args.server, args.username, args.password, args.database) sys.stdout.write("Start connect to server\n") cursor = conn.cursor() sys.stdout.write("Check table is existed\n") checkTable(conn, "tbRoadLink") sys.stdout.write("Create table\n") cursor.execute(CREATE_TABLE_ROADLINK) sys.stdout.write("Start insert RoadLink\n") insertRoadLink(conn, sf, m) sys.stdout.write("Finish all\n") conn.close()
def main(): sf = shapefile.Reader("shapefiles/路網數值圖103年_西屯區道路路段") # 確認 shapeType 種類 if sf.shapeType is 3: log.debug("PolyLine") root = ET.Element('ro:Road') m = tools.create_mapping(sf.fields) log.debug(m) index = 0 rp = None shapes = sf.shapes() for rec in sf.iterRecords(): if "台12" == rec[m['roadname']]: if rp is None: rp = setRefered(sf, m, root, rec) setLink(sf, m, root, rec, shapes[index].points, index) index = index + 1 log.debug(ET.dump(root)) tree = ET.ElementTree(root) with open ('台12motc.xml', 'w') as f: f.write('<?xml version="1.0" encoding="UTF-8"?>\n') tree.write(f, encoding="unicode")