Upload a shapefile to a given project, and display status when complete. Args: service: The service object built by the Google API Python client library. project_id: string, id of the GME project. shapefile_prefix: string, the shapefile without the .shp suffix. Returns:
(service, project_id, shapefile_prefix)
| 87 | |
| 88 | |
| 89 | def UploadShapefile(service, project_id, shapefile_prefix): |
| 90 | """Upload a shapefile to a given project, and display status when complete. |
| 91 | |
| 92 | Args: |
| 93 | service: The service object built by the Google API Python client library. |
| 94 | project_id: string, id of the GME project. |
| 95 | shapefile_prefix: string, the shapefile without the .shp suffix. |
| 96 | |
| 97 | Returns: |
| 98 | String id of the table asset. |
| 99 | """ |
| 100 | # A shapefile is actually a bunch of files; GME requires these four suffixes. |
| 101 | suffixes = ["shp", "dbf", "prj", "shx"] |
| 102 | files = [] |
| 103 | for suffix in suffixes: |
| 104 | files.append({"filename": "%s.%s" % (shapefile_prefix, suffix)}) |
| 105 | metadata = { |
| 106 | "projectId": project_id, |
| 107 | "name": shapefile_prefix, |
| 108 | "description": "polygons that were uploaded by a script", |
| 109 | "files": files, |
| 110 | # You need the string value of a valid shared and published ACL |
| 111 | # Check the "Access Lists" section of the Maps Engine UI for a list. |
| 112 | "draftAccessList": "Map Editors", |
| 113 | "tags": [shapefile_prefix, "auto_upload", "kittens"], |
| 114 | } |
| 115 | |
| 116 | logging.info("Uploading metadata for %s", shapefile_prefix) |
| 117 | response = service.tables().upload(body=metadata).execute() |
| 118 | # We have now created an empty asset. |
| 119 | table_id = response["id"] |
| 120 | |
| 121 | # And now upload each of the files individually, passing in the table id. |
| 122 | for suffix in suffixes: |
| 123 | shapefile = "%s.%s" % (shapefile_prefix, suffix) |
| 124 | media_body = MediaFileUpload(shapefile, mimetype="application/octet-stream") |
| 125 | logging.info("uploading %s", shapefile) |
| 126 | |
| 127 | response = ( |
| 128 | service.tables() |
| 129 | .files() |
| 130 | .insert(id=table_id, filename=shapefile, media_body=media_body) |
| 131 | .execute() |
| 132 | ) |
| 133 | |
| 134 | # With all files uploaded, check status of the asset to ensure it's processed. |
| 135 | CheckAssetStatus(service, "tables", table_id) |
| 136 | return table_id |
| 137 | |
| 138 | |
| 139 | def CheckAssetStatus(service, asset_type, asset_id): |
no test coverage detected
searching dependent graphs…