LoginSignup
0
0

[ArcGIS]週の活動レポート作成(arcgis API for python)

Posted at

はじめに

ArcGIS onlineの週活動のレポートの自動作成をして、デスクトップPCにダウンロードする。

前提条件

  • M1 MAC
  • Arcgis API for pythonの2.3.0.1を使用
  • ユーザーは作成して、権限を付与されているものとする。

手順

ユーザーログイン

# arcgisのライブラリをインポートする。
from arcgis.gis import *
from arcgis.mapping import WebMap

# ArcGIS onlineのユーザーでログインする。
user_name = "username"
pass_word = "password"
my_gis = GIS("https://www.arcgis.com", username = user_name, password = pass_word, access = "private")
print(str(my_gis.properties.user.username) + "としてログインしました。")

datetimeライブラリのインポート

日付を扱うので、datetimeライブラリをインポートする。

import datetime as dt

前の週の日曜日の値を求める。

ハマったところとして、年月日までの値だとException: Invalid startTime. Weekly report must start from Sunday or Monday. (Error Code: 400)というエラーが出る。時と分までを入力する必要がある。

# 現在の日時
now = dt.datetime.now()
# 現在の日付での曜日(日曜日は0)
today_weekday = now.weekday()
# 現在の週の前の週の日曜日の0時を計算
days_to_last_sunday = today_weekday + 1
last_sunday = now - dt.timedelta(days=days_to_last_sunday)
# 前週の日曜日の0時に調整
sunday_last_week = dt.datetime(last_sunday.year, last_sunday.month, last_sunday.day, 0, 0)- dt.timedelta(weeks=1)
print(sunday_last_week)

#> datetime.datetime(2024, 4, 28, 0, 0)

レポート作成

def makeReport():
    global item
    user = my_gis.users.me
    #毎週月曜日に実行する。
    item = user.report("activity",
                       start_time=sunday_last_week,
                   duration="weekly")
    return item

#レポート作成関数の実行

makeReport()

ファイルのダウンロード

#アイテムをダウンロードする関数
def itemDownload(itemId):
    csv_item = my_gis.content.get(itemId)
    downloadPath = csv_item.download("/Users/username/Downloads/")
    
    return downloadPath

#アイテムIDを変数に格納する。
downloadPath = item.id
#ダウンロードの実行
itemDownload(downloadPath)

結果

ダウンロード成功。

雑感

週のレポート作成の値に時分が必要というところにハマりました。バージョンが新しくなって、対応されただけかもしれないですが。arcgis API for python自体には小さなバグがあるので、バージョンを変えてテストすると、修正されているところもある。githubでissueをユーザーが挙げていかないと行けないのだなあ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0