1. 程式人生 > >百度地圖API繪製計程車流向地圖(一):將起始GPS點對映到地圖上

百度地圖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')

熱力圖效果如下

在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

參考部落格:
使用python獲取csv文字的某行或某列資料
Python:CSV檔案和JSON檔案相互轉換