Created
March 28, 2014 15:24
-
-
Save jlivni/9835361 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def CreateLayer(http, project_id, table_id, title, description, tags): | |
"""Create a styled polygon Layer from a Table; display a status when complete. | |
Args: | |
http: http, authenticated http object. | |
project_id: string, id of the GME project. | |
table_id: string, the id of the source table. | |
title: string, the title of the layer. | |
description: string, the description of the layer. | |
tags: list, list,of,tags. | |
Returns: | |
String id of the new layer asset. | |
""" | |
# TODO(jlivni): Update to v1 when launched. | |
# The process url param causes the Layer to auto process after create. | |
create_url = "https://www.googleapis.com/mapsengine/exp2/layers/?process=true" | |
headers = {"Content-Type": "application/json"} | |
# Create a simple polygon style | |
polygon_style = { | |
"type": "displayRule", | |
"displayRules": [{ | |
"name": "mystyle", | |
"zoomLevels": { | |
"min": 0, | |
"max": 24 | |
}, | |
"polygonStyle": { | |
"fillColor": { | |
"color": "#31a354", | |
"opacity": 0.5 | |
}, | |
"border": { | |
"color": { | |
"color": "#74c476", | |
"opacity": 0.3 | |
}, | |
"width": 1 | |
} | |
} | |
}] | |
} | |
metadata = { | |
"name": title, | |
"description": description, | |
"projectId": project_id, | |
"tags": tags, | |
# You need the string value of a valid shared and publiched ACL | |
# Check the "Access Lists" section of the Maps Engine UI for a list. | |
"draftAccessList": "Map Editors", | |
"publishedAccessList": "Map Viewers", | |
"datasourceType": "table", | |
"datasources": [{"id": table_id}] | |
} | |
metadata["vectorStyle"] = polygon_style | |
logging.info("metadata: %s", metadata) | |
body = json.dumps(metadata) | |
logging.info("Creating layer with the following metadata: %s", metadata) | |
resp, content = http.request(create_url, | |
method="POST", | |
headers=headers, | |
body=body) | |
RaiseBadResponse(create_url, resp, content) | |
logging.debug(content) | |
# We have now created a styled Layer. | |
result = json.loads(content) | |
layer_id = result["id"] | |
# Check everything completed. | |
CheckAssetStatus(http, "layers", layer_id) | |
return layer_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment