def place(place_dcid=None): dcid = flask.request.args.get('dcid', None) topic = flask.request.args.get('topic', None) if dcid: url = flask.url_for('place.place', place_dcid=dcid, topic=topic, _external=True, _scheme="https") return flask.redirect(url) if not place_dcid: return flask.render_template( 'place_landing.html', maps_api_key=current_app.config['MAPS_API_KEY']) place_type = place_api.get_place_type(place_dcid) place_names = place_api.get_property_value(place_dcid, 'name') if place_names: place_name = place_names[0] else: place_name = place_dcid return flask.render_template( 'place.html', place_type=place_type, place_name=place_name, place_dcid=place_dcid, topic=topic if topic else '', maps_api_key=current_app.config['MAPS_API_KEY'])
def get_choropleth_display_level(geoDcid): """ Get the display level of places to show on a choropleth chart for a given place. Args: geoDcid: dcid of the place of interest Returns: tuple consisting of: dcid of the enclosing place to use (if the display level and the place type of the geoDcid arg are the same, this would be the parent place dcid) display level (AdministrativeArea1 or AdministrativeArea2) """ if geoDcid in SPECIAL_CHOROPLETH_DISPLAY_LEVEL_MAP: display_level = SPECIAL_CHOROPLETH_DISPLAY_LEVEL_MAP[geoDcid] return geoDcid, display_level place_type = place_api.get_place_type(geoDcid) display_level = None if place_type in CHOROPLETH_DISPLAY_LEVEL_MAP: display_level = CHOROPLETH_DISPLAY_LEVEL_MAP[place_type] elif place_type in EQUIVALENT_PLACE_TYPES and EQUIVALENT_PLACE_TYPES[ place_type] in CHOROPLETH_DISPLAY_LEVEL_MAP: place_type = EQUIVALENT_PLACE_TYPES[place_type] display_level = CHOROPLETH_DISPLAY_LEVEL_MAP[place_type] else: return None, None if place_type == display_level: parents_places = place_api.parent_places(geoDcid) # Multiple place types can be equivalent (eg. County and AA2) and we # want to find the parent who's display level is equivalent to the # geoDcid display_level target_display_levels = set([display_level]) if display_level in EQUIVALENT_PLACE_TYPES: target_display_levels.add(EQUIVALENT_PLACE_TYPES[display_level]) for parent in parents_places.get(geoDcid, []): parent_dcid = parent.get('dcid', None) if not parent_dcid: continue parent_place_types = parent.get('types', []) for parent_place_type in parent_place_types: parent_display_level = CHOROPLETH_DISPLAY_LEVEL_MAP.get( parent_place_type, None) if not parent_display_level: parent_display_level = CHOROPLETH_DISPLAY_LEVEL_MAP.get( EQUIVALENT_PLACE_TYPES.get(parent_place_type, '')) if parent_display_level in target_display_levels: return parent_dcid, display_level return None, None else: return geoDcid, display_level
def place(place_dcid=None): redirect_args = dict(flask.request.args) should_redirect = False if 'topic' in flask.request.args: redirect_args['category'] = flask.request.args.get('topic', '') del redirect_args['topic'] should_redirect = True category = redirect_args.get('category', None) if category in CATEGORY_REDIRECTS: redirect_args['category'] = CATEGORY_REDIRECTS[category] should_redirect = True if should_redirect: redirect_args['place_dcid'] = place_dcid return flask.redirect(flask.url_for('place.place', **redirect_args)) dcid = flask.request.args.get('dcid', None) if dcid: # Traffic from "explore more" in Search. Forward along all parameters, # except for dcid, to the new URL format. redirect_args = dict(flask.request.args) redirect_args['place_dcid'] = dcid del redirect_args['dcid'] redirect_args['category'] = category url = flask.url_for('place.place', **redirect_args, _external=True, _scheme=current_app.config.get('SCHEME', 'https')) return flask.redirect(url) if not place_dcid: # Use display names (including state, if applicable) for the static page place_names = place_api.get_display_name( '^'.join(_PLACE_LANDING_DCIDS), g.locale) return flask.render_template( 'place_landing.html', place_names=place_names, maps_api_key=current_app.config['MAPS_API_KEY']) place_type = place_api.get_place_type(place_dcid) place_names = place_api.get_i18n_name([place_dcid]) if place_names and place_names.get(place_dcid): place_name = place_names[place_dcid] else: place_name = place_dcid return flask.render_template( 'place.html', place_type=place_type, place_name=place_name, place_dcid=place_dcid, category=category if category else '', maps_api_key=current_app.config['MAPS_API_KEY'])
def get_choropleth_places(geoDcid): """ Get the list of places to show on a choropleth chart for a given place Args: geoDcid: dcid of the place of interest Returns: (list of dcids, property to use for fetching geo json) """ place_list = [] place_type = place_api.get_place_type(geoDcid) display_level = None if place_type in CHOROPLETH_DISPLAY_LEVEL_MAP: display_level = CHOROPLETH_DISPLAY_LEVEL_MAP[place_type] elif place_type in EQUIVALENT_PLACE_TYPES and EQUIVALENT_PLACE_TYPES[ place_type] in CHOROPLETH_DISPLAY_LEVEL_MAP: place_type = EQUIVALENT_PLACE_TYPES[place_type] display_level = CHOROPLETH_DISPLAY_LEVEL_MAP[place_type] else: return place_list if place_type == display_level: parents_places = place_api.parent_places(geoDcid) for parent in parents_places.get(geoDcid, []): parent_dcid = parent.get('dcid', None) if not parent_dcid: continue parent_place_types = parent.get('types', []) for parent_place_type in parent_place_types: parent_display_level = CHOROPLETH_DISPLAY_LEVEL_MAP.get( parent_place_type, None) if not parent_display_level: parent_display_level = CHOROPLETH_DISPLAY_LEVEL_MAP.get( EQUIVALENT_PLACE_TYPES.get(parent_place_type, '')) if parent_display_level == display_level: place_list = dc_service.get_places_in([parent_dcid], display_level).get( parent_dcid, []) geo_prop = CHOROPLETH_GEOJSON_PROPERTY_MAP[display_level] # Puerto Rico (geoId/72) requires higher resolution geoJson if parent_dcid == 'geoId/72': geo_prop = 'geoJsonCoordinatesDP1' return place_list, geo_prop return place_list else: place_list = dc_service.get_places_in([geoDcid], display_level).get(geoDcid, []) geo_prop = CHOROPLETH_GEOJSON_PROPERTY_MAP[display_level] # Puerto Rico (geoId/72) requires higher resolution geoJson if geoDcid == 'geoId/72': geo_prop = 'geoJsonCoordinatesDP1' return place_list, geo_prop
def topic_page(topic_id=None, place_dcid=None): topics_summary = json.dumps(current_app.config['TOPIC_PAGE_SUMMARY']) # Redirect to the landing page. if not place_dcid and not topic_id: return flask.render_template('topic_page_landing.html') all_configs = current_app.config['TOPIC_PAGE_CONFIG'] if os.environ.get('FLASK_ENV') == 'local': all_configs = libutil.get_topic_page_config() topic_configs = all_configs.get(topic_id, []) if len(topic_configs) < 1: return "Error: no config found" if not place_dcid: return flask.render_template( 'topic_page.html', place_type="", place_name="", place_dcid="", topic_id=topic_id, topic_name=topic_configs[0].metadata.topic_name or "", config={}, topics_summary=topics_summary) # Find the config for the topic & place. topic_place_config = None for config in topic_configs: if place_dcid in config.metadata.place_dcid: topic_place_config = config break if not topic_place_config: return "Error: no config found" # TODO: should use place metadata API to fetch these data in one call. place_type = place_api.get_place_type(place_dcid) place_names = place_api.get_i18n_name([place_dcid]) if place_names: place_name = place_names[place_dcid] else: place_name = place_dcid return flask.render_template( 'topic_page.html', place_type=place_type, place_name=place_name, place_dcid=place_dcid, topic_id=topic_id, topic_name=topic_place_config.metadata.topic_name or "", config=MessageToJson(topic_place_config), topics_summary=topics_summary)
def get_choropleth_display_level(geoDcid): """ Get the display level of places to show on a choropleth chart for a given place. Args: geoDcid: dcid of the place of interest Returns: tuple consisting of: dcid of the enclosing place to use (if the display level and the place type of the geoDcid arg are the same, this would be the parent place dcid) display level (AdministrativeArea1 or AdministrativeArea2) """ # Territories of the US, e.g. country/USA are also AA1. Restrict this view to only States. if geoDcid == 'country/USA': return geoDcid, 'State' place_type = place_api.get_place_type(geoDcid) display_level = None if place_type in CHOROPLETH_DISPLAY_LEVEL_MAP: display_level = CHOROPLETH_DISPLAY_LEVEL_MAP[place_type] elif place_type in EQUIVALENT_PLACE_TYPES and EQUIVALENT_PLACE_TYPES[ place_type] in CHOROPLETH_DISPLAY_LEVEL_MAP: place_type = EQUIVALENT_PLACE_TYPES[place_type] display_level = CHOROPLETH_DISPLAY_LEVEL_MAP[place_type] else: return None, None if place_type == display_level: parents_places = place_api.parent_places(geoDcid) for parent in parents_places.get(geoDcid, []): parent_dcid = parent.get('dcid', None) if not parent_dcid: continue parent_place_types = parent.get('types', []) for parent_place_type in parent_place_types: parent_display_level = CHOROPLETH_DISPLAY_LEVEL_MAP.get( parent_place_type, None) if not parent_display_level: parent_display_level = CHOROPLETH_DISPLAY_LEVEL_MAP.get( EQUIVALENT_PLACE_TYPES.get(parent_place_type, '')) if parent_display_level == display_level: return parent_dcid, display_level return None, None else: return geoDcid, display_level
def place(place_dcid=None): dcid = flask.request.args.get('dcid', None) topic = flask.request.args.get('topic', None) if dcid: # Traffic from "explore more" in Search. Forward along all parameters, # except for dcid, to the new URL format. redirect_args = dict(flask.request.args) redirect_args['place_dcid'] = dcid del redirect_args['dcid'] redirect_args['topic'] = topic url = flask.url_for('place.place', **redirect_args, _external=True, _scheme=current_app.config.get('SCHEME', 'https')) return flask.redirect(url) if not place_dcid: # Use display names (including state, if applicable) for the static page place_names = place_api.get_display_name( '^'.join(_PLACE_LANDING_DCIDS), g.locale) return flask.render_template( 'place_landing.html', place_names=place_names, maps_api_key=current_app.config['MAPS_API_KEY']) place_type = place_api.get_place_type(place_dcid) place_names = place_api.get_i18n_name([place_dcid]) if place_names: place_name = place_names[place_dcid] else: place_name = place_dcid return flask.render_template( 'place.html', place_type=place_type, place_name=place_name, place_dcid=place_dcid, topic=topic if topic else '', maps_api_key=current_app.config['MAPS_API_KEY'])