Example #1
0
    def flatten_tiles(self,
                      tiles,
                      datatype_factory,
                      compact=True,
                      use_fieldname=False):
        feature_collections = {}
        compacted_data = {}
        lookup = {}
        has_geometry = False

        for tile in tiles:  # normalize tile.data to use labels instead of node ids
            compacted_data["resourceid"] = tile["resourceinstance_id"]
            for nodeid, value in tile["data"].items():
                node = self.get_node(nodeid)
                if node.exportable:
                    datatype = datatype_factory.get_instance(node.datatype)
                    node_value = datatype.get_display_value(tile, node)
                    label = node.fieldname if use_fieldname is True else node.name

                    if compact:
                        if node.datatype == "geojson-feature-collection":
                            has_geometry = True
                            feature_collections = self.get_feature_collections(
                                tile, node, feature_collections, label,
                                datatype)
                        else:
                            try:
                                compacted_data[label] += ", " + str(node_value)
                            except KeyError:
                                compacted_data[label] = str(node_value)
                    else:
                        data[label] = str(node_value)

            if not compact:  # add on the cardinality and card_names to the tile for use later on
                tile["data"] = data
                card = models.CardModel.objects.get(
                    nodegroup=tile["nodegroup_id"])
                tile["card_name"] = card.name
                tile["cardinality"] = node.nodegroup.cardinality
                tile[card.name] = tile["data"]
                lookup[tile["tileid"]] = tile

        if compact:
            for key, value in feature_collections.items():
                compacted_data[key] = value[
                    "datatype"].transform_export_values(value)
            compacted_data["has_geometry"] = has_geometry
            return compacted_data

        resource_json = self.create_resource_json(tiles)
        return flatten_dict(resource_json)
Example #2
0
    def flatten_tiles(self, tiles, datatype_factory, compact=True):
        compacted_data = {}
        lookup = {}
        feature_collections = {}

        # first let's normalize tile.data to use labels instead of node ids
        for tile in tiles:
            data = {}
            for nodeid, value in tile["data"].items():
                node = self.get_node(nodeid)
                if node.exportable:
                    datatype = datatype_factory.get_instance(node.datatype)
                    # node_value = datatype.transform_export_values(tile['data'][str(node.nodeid)])
                    node_value = datatype.get_display_value(tile, node)
                    label = node.fieldname

                    if compact:
                        if node.datatype == "geojson-feature-collection":
                            node_value = tile["data"][str(node.nodeid)]
                            for feature_index, feature in enumerate(
                                    node_value["features"]):
                                feature["geometry"][
                                    "coordinates"] = self.set_precision(
                                        feature["geometry"]["coordinates"],
                                        self.precision)
                                try:
                                    feature_collections[label][
                                        "features"].append(feature)
                                except:
                                    feature_collections[label] = {
                                        "datatype": datatype,
                                        "features": [feature]
                                    }
                        else:
                            try:
                                compacted_data[label] += ", " + str(node_value)
                            except:
                                compacted_data[label] = str(node_value)
                    else:
                        data[label] = str(node_value)

            if not compact:
                # add on the cardinality and card_names to the tile for use later on
                tile["data"] = data
                card = models.CardModel.objects.get(
                    nodegroup=tile["nodegroup_id"])
                tile["card_name"] = card.name
                tile["cardinality"] = node.nodegroup.cardinality
                tile[card.name] = tile["data"]
                lookup[tile["tileid"]] = tile

        if compact:
            for key, value in feature_collections.items():
                compacted_data[key] = value[
                    "datatype"].transform_export_values(value)
            return compacted_data

        # print(JSONSerializer().serialize(tiles, indent=4))

        resource_json = {}
        # ret = []
        # aggregate tiles into single resource instance objects rolling up tile data in the process
        # print out "ret" to understand the intermediate structure
        for tile in tiles:
            if tile["parenttile_id"] is not None:
                parentTile = lookup[str(tile["parenttile_id"])]
                if tile["cardinality"] == "n":
                    try:
                        parentTile[parentTile["card_name"]][
                            tile["card_name"]].append(tile[tile["card_name"]])
                    except KeyError:
                        parentTile[parentTile["card_name"]][
                            tile["card_name"]] = [tile[tile["card_name"]]]
                else:
                    parentTile[parentTile["card_name"]][
                        tile["card_name"]] = tile[tile["card_name"]]
            else:
                # print the following out to understand the intermediate structure
                # ret.append(tile)
                # ret.append({tile['card_name']: tile[tile['card_name']]})
                if tile["cardinality"] == "n":
                    try:
                        resource_json[tile["card_name"]].append(
                            tile[tile["card_name"]])
                    except KeyError:
                        resource_json[tile["card_name"]] = [
                            tile[tile["card_name"]]
                        ]
                else:
                    resource_json[tile["card_name"]] = tile[tile["card_name"]]

        return flatten_dict(resource_json)