Exemplo n.º 1
0
    def allocate_workslice_features(self, workslice, extent, feature_filter):
        layer_in_dataset = workslice.layer_in_dataset
        covered_fids = osm.get_layer_feature_ids(layer_in_dataset, extent)
        if covered_fids is None:
            raise WorksliceTooFeaturefulError
        if len(covered_fids) == 0:
            raise WorksliceInsufficientlyFeaturefulError

        ws_feats = [
            WorksliceFeature(
                workslice=workslice,
                layer_in_dataset=layer_in_dataset,
                feature_id=fid
                ) for fid
            in covered_fids
            if not self.filter(
                layer_in_dataset=layer_in_dataset,
                feature_id=fid,
                dirty=0
                ).exists()
            ]
        if len(ws_feats) > layer_in_dataset.layer.feature_limit:
            raise WorksliceTooFeaturefulError
        if len(ws_feats) == 0:
            raise WorksliceInsufficientlyFeaturefulError
        ws_feats = feature_filter.apply(ws_feats)
        self.bulk_create(ws_feats)
        workslice.feature_count = len(ws_feats)
        workslice.save()
Exemplo n.º 2
0
def workslice_info(request, layer_in_dataset_id):
    layer_in_dataset = get_object_or_404(LayerInDataset, pk=layer_in_dataset_id)
    layer = layer_in_dataset.layer
    dataset = layer_in_dataset.dataset

    ctx = {}

    form = WorksliceForm(request.POST, error_class=BootstrapErrorList, hide_filters=layer_in_dataset.hide_filters)
    if form.is_valid():
        feature_count = osm.get_layer_feature_count(dataset.name, layer, form.extent)
        if feature_count > layer.feature_limit:
            ctx['info'] = "Too many features (over %d): please reduce selection." % layer.feature_limit
            ctx['osm_conflict_info'] = ""
        else:
            if feature_count > 1:
                ctx['info'] = "%d features in selection." % feature_count
            elif feature_count == 1:
                ctx['info'] = "1 feature in selection."
            elif feature_count == 0:
                ctx['info'] = "No features in selection."
            else:
                ctx['info'] = "Serious error calculating features."

            if form.cleaned_data['show_conflicting_features'] in ['yes', 'count'] or form.cleaned_data['show_features_in_selection'] in ['yes', 'centroids']:
                feature_ids = osm.get_layer_feature_ids(layer_in_dataset, form.extent)
                workslice_features = [WorksliceFeature(feature_id=feat_id, layer_in_dataset=layer_in_dataset) for feat_id in feature_ids]

            if form.cleaned_data['show_conflicting_features'] in ['yes', 'count']:
                if layer.tags_ql:
                    osm_conflicts = overpass.osm_conflicts_json(workslice_features, layer.tags_ql)['elements'] # FIXME: tagging

                    nodes = dict([(n['id'], n) for n in osm_conflicts if n['type'] == 'node'])
                    ways = dict([(n['id'], n) for n in osm_conflicts if n['type'] == 'way'])
                    rels = dict([(n['id'], n) for n in osm_conflicts if n['type'] == 'rel'])

                    if layer.geometry_type == 'POINT':
                        conflict_count = len(nodes)
                        if form.cleaned_data['show_conflicting_features'] == 'yes':
                            ctx['osm_conflict_geometry'] = overpass.osm_geojson(nodes.values())
                    elif layer.geometry_type == 'LINESTRING':
                        conflict_count = len(ways)
                        if form.cleaned_data['show_conflicting_features'] == 'yes':
                            ctx['osm_conflict_geometry'] = overpass.osm_geojson(ways.values(), nodes)
                    elif layer.geometry_type == 'POLYGON':
                        conflict_count = len(ways) # FIXME: count rels too.
                        if form.cleaned_data['show_conflicting_features'] == 'yes':
                            ctx['osm_conflict_geometry'] = overpass.osm_geojson(rels.values(), nodes, ways)
                    elif layer.geometry_type == 'RELATION':
                        conflict_count = len(rels)
                        if form.cleaned_data['show_conflicting_features'] == 'yes':
                            ctx['osm_conflict_geometry'] = overpass.osm_geojson(rels.values(), nodes, ways)

                    if conflict_count > 1:
                        ctx['osm_conflict_info'] = "%d nearby features of this type in OSM." % conflict_count
                    elif conflict_count == 1:
                        ctx['osm_conflict_info'] = "1 nearby feature of this type in OSM."
                    elif conflict_count == 0:
                        ctx['osm_conflict_info'] = "No nearby features of this type in OSM."
                    else:
                        ctx['osm_conflict_info'] = "Error querying OSM."
                else:
                    ctx["osm_conflict_info"] = "Could not check for conflicting features - no OSM query set for this layer."
            else:
                ctx['osm_conflict_info'] = ""

            if form.cleaned_data['show_features_in_selection'] == 'yes':
                ctx['feature_selection_geometry'] = osm.feature_selection_geojson(workslice_features)
            elif form.cleaned_data['show_features_in_selection'] == 'centroids':
                ctx['feature_selection_geometry'] = osm.feature_selection_geojson(workslice_features, True)

    else:
        ctx['info'] = " ".join(form.errors['__all__'])
    # ctx['queries'] = django.db.connections[layer_in_dataset.dataset.name].queries

    return HttpResponse(json.dumps(ctx), content_type='application/json')