zoukankan      html  css  js  c++  java
  • ”城市公交线路站点数据爬取 + csv站点数据转ShapeFile矢量数据“(三)点csv转shp

    这里用pyshp完成转换。

    坐标纠偏(gcj02转wgs84)和定义投影(proj.4)酌情操作。

    #-*-coding:utf-8-*-
    import shapefile as shp
    import csv
    import codecs
    import os
    
    def trans_point(folder, fn, delimiter=','):
        '''transfer a csv file to shapefile'''
        # create a point shapefile
        output_shp = shp.Writer(folder + "%s.shp"%fn.split('.')[0], shp.POINT)
        # for every record there must be a corresponding geometry.
        output_shp.autoBalance = 1
        # create the field names and data type for each you can omit fields here
        # 顺序一定要与下面的保持一致
        #关键步
        output_shp.field('PointUid', 'C', 80) # string, max-length
        #output_shp.field('KeyName', 'C', 20)
        output_shp.field('BusName', 'C', 70)
        output_shp.field('StationName', 'C', 50)
        output_shp.field('LON', 'C', 15)
        output_shp.field('LAT', 'C', 15)
        counter = 1 # count the features
        # access the CSV file
        with codecs.open(folder + fn, 'rb', 'utf-8') as csvfile:
            reader = csv.reader(csvfile, delimiter=delimiter)
            next(reader, None) # skip the header
            #loop through each of the rows and assign the attributes to variables
            for row in reader:
                try:
                    ###只有上海数据有KeyName
                    #关键步
                    PointUid = row[0]
                    #KeyName= row[1]
                    BusName = row[1]
                    StationName = row[2]
                    LON = row[3]
                    LAT = row[4]
                    output_shp.point(float(LON), float(LAT)) # create the point geometry
                    output_shp.record(PointUid,BusName,StationName,LON,LAT) # 关键步add attribute data
                    if counter % 10000 == 0:
                        print("Feature " + str(counter) + " added to Shapefile.")
                    counter = counter + 1
                except Exception as e:
                    print(e)
                    print(row)
    
    #listJS = ["changzhou","huaian","lianyungang","nanjing","nantong","suqian","suzhou","taizhou","wuxi","xuzhou","yancheng","yangzhou","zhenjiang"]
    for i in listJS:
        trans_point("E:\公交站点csv转shp\","{}点总量.csv".format(i))

    最后结果:

     厌世写手不想再写py爬虫了,立个flag,这个号不会再更新爬虫了。

  • 相关阅读:
    Unity3D 开发之shader教程(浅谈光照之漫反射diffuse)
    Unity3D 开发之shader教程(实现圆角矩形)
    Unity3D 开发之shader教程(实时swirl 扭曲漩涡效果)
    lerp function(线性插值计算)
    关于Lua以及UniLua的学习与笔记(随时更新)
    Android SDK Manager 更新
    Mac OS X 安装protobuf 2.6.1
    IDFactory int类型ID生成器
    Android TextView 添加部分文字高亮,点击功能
    Android MonkeyRunner 功能测试 自动执行 挂机脚本
  • 原文地址:https://www.cnblogs.com/hsh17/p/12572201.html
Copyright © 2011-2022 走看看