百度地图API绘制出租车流向地图(一):将起始GPS点映射到地图上

打算在年前实现用百度地图API绘制NYC出租车流向地图。目前实现了将出租车的起始点的GPS点映射到地图上,绘制了GPS点的热力图。记录在此。
所用数据:NYC绿色出租车数据
时间:2016.01.01 0:00-24:00 共24小时
GPS点个数:64398
绘图方式:热力图

百度地图API提供自定义热力图(Heatmap)功能,网站上提供示例代码。
将经纬度数据喂进去即可。

记录一下csv文件转json文件:

绿色出租车csv数据包括上下车时间、上下车的经纬度,载客人数、各种收费计算,只读其中的上车点经纬度数据。

1.使用reader函数,返回一个生成器,类型为列表:

import csv
with open('example.csv','rb') as csvfile:
    reader = csv.reader(csvfile)
    rows= [row for row in reader]
  1. 读取其中的一列:
import csv
with open('A.csv','rb') as csvfile:
    reader = csv.reader(csvfile)
    column = [row[2] for row in reader]
    #读其中的第3列
  1. 读取其中的一行:
    下面代码打印出row[2]代表的值,也就是第三行
with open(csv_file, "r") as f:
    reader = csv.reader(f)
    row1 = [row for row in reader]
print row1[2]  #row1[i]就代表是第几行
  1. 读取指定的某行某列
    如果想打印出第一列第一行的值,直接
print column1[0]

2.使用DictReader

从csv读出的都是str类型。这种方法要事先知道列的序号,比如longitude在第5列,而不能根据’longtitude’这个标题查询。这时可以采用DictRead函数
如果我们想用DictReader读取csv的某一列,就可以用列的标题查询:

#直接用列标题 Pickup_longitude 来读
with open(file1,"r") as csvfile1:
        reader = csv.DictReader(csvfile1)
        Pickup_longitude = [row['Pickup_longitude'] for row in reader]

3. 将读取的某几列写入到新的csv文件:

with open(file2,"w") as csvfile2:
        print(len(Pickup_longitude), len(Pickup_latitude))
        writer = csv.writer(csvfile2)
        writer.writerow(['lng','lat','count'])
        #for index in range(len(Pickup_latitude)):
        for index in range(num):
            writer.writerow([float(Pickup_longitude[index]),float(Pickup_latitude[index]),float(1)])

将处理好的csv文件转json文件

def read_csv(file):
    csv_rows = []
    with open(file) as csvfile:
        reader = csv.DictReader(csvfile)
        title = reader.fieldnames
        for row in reader:
            csv_rows.extend([{title[i]:float(row[title[i]]) for i in range(len(title))}])
        return csv_rows

# 写json文件
def write_json(data, json_file, format=None):
    with open(json_file, "w") as f:
        if format == "good":
            f.write(json.dumps(data, sort_keys=False, indent=4, separators=(',', ': '),encoding="utf-8",ensure_ascii=False))
        else:
            f.write(json.dumps(data))

write_json(read_csv('G:/nyc/data.csv'), 'data.json')

附完整代码:

import csv
import json 
def csv2csv(file1,file2):
    Pickup_longitude = []
    Pickup_latitude = []
    with open(file1,"r") as csvfile1:
        reader = csv.DictReader(csvfile1)
        Pickup_longitude = [row['Pickup_longitude'] for row in reader]
        # Pickup_latitude = [row2['Pickup_latitude'] for row2 in reader]
    with open(file1, "r") as csvfile1:
        reader = csv.DictReader(csvfile1)
        Pickup_latitude = [row['Pickup_latitude'] for row in reader]

    with open(file2,"w") as csvfile2:
        print(len(Pickup_longitude), len(Pickup_latitude))
        writer = csv.writer(csvfile2)
        writer.writerow(['lng','lat','count'])
        for index in range(len(Pickup_latitude)):            writer.writerow([float(Pickup_longitude[index]),float(Pickup_latitude[index]),float(1)])

csv2csv('G:/nyc/green_tripdata_2016-01.csv','G:/nyc/data.csv')

def read_csv(file):
    csv_rows = []
    with open(file) as csvfile:
        reader = csv.DictReader(csvfile)
        title = reader.fieldnames
        for row in reader:
            csv_rows.extend([{title[i]:float(row[title[i]]) for i in range(len(title))}])
        return csv_rows

# 写json文件
def write_json(data, json_file, format=None):
    with open(json_file, "w") as f:
        if format == "good":
            f.write(json.dumps(data, sort_keys=False, indent=4, separators=(',', ': '),encoding="utf-8",ensure_ascii=False))
        else:
            f.write(json.dumps(data))

write_json(read_csv('G:/nyc/data.csv'), 'data.json')

热力图效果如下

百度地图API绘制出租车流向地图(一):将起始GPS点映射到地图上
百度地图API绘制出租车流向地图(一):将起始GPS点映射到地图上
百度地图API绘制出租车流向地图(一):将起始GPS点映射到地图上

参考博客:
使用python获取csv文本的某行或某列数据
Python:CSV文件和JSON文件相互转换