def create_spatial_unit(self, data, project, questionnaire, party=None): location_resources = [] location_objects = [] try: location_group = self._format_repeat(data, ['location']) for group in location_group: if 'location_geotrace' in group.keys(): location_geometry = group['location_geotrace'] elif 'location_geoshape' in group.keys(): location_geometry = group['location_geoshape'] else: location_geometry = group['location_geometry'] geom = odk_geom_to_wkt(location_geometry) location = SpatialUnit.objects.create( project=project, type=group['location_type'], geometry=geom, attributes=self._get_attributes(group, 'location')) location_resources.append( self._get_resource_names(group, location, 'location')) location_objects.append(location) except Exception as e: raise InvalidXMLSubmission(_('Location error: {}'.format(e))) return location_objects, location_resources
def _format_geometry(self, data): if 'location_geotrace' in data: geom = data['location_geotrace'] elif 'location_geoshape' in data: geom = data['location_geoshape'] else: geom = data['location_geometry'] return odk_geom_to_wkt(geom)
def validate_row(headers, row, config): party_name, party_type, geometry, tenure_type, location_type = (None, None, None, None, None) (party_name_field, party_type_field, location_type_field, type, geometry_field, tenure_type_field) = get_fields_from_config(config) if len(headers) != len(row): raise ValidationError(_("Number of headers and columns do not match.")) if party_name_field and party_type_field: try: party_name = row[headers.index(party_name_field)] except ValueError: raise ValidationError(_("No 'party_name' column found.")) try: party_type = row[headers.index(party_type_field)] except ValueError: raise ValidationError(_("No 'party_type' column found.")) if geometry_field: try: coords = row[headers.index(geometry_field)] except ValueError: raise ValidationError(_("No 'geometry_field' column found.")) try: geometry = GEOSGeometry(coords) except: pass # try ODK geom parser if not geometry: try: geometry = odk_geom_to_wkt(coords) except InvalidODKGeometryError: raise ValidationError(_("Invalid geometry.")) if location_type_field: try: location_type = row[headers.index(location_type_field)] type_choices = [choice[0] for choice in TYPE_CHOICES] if location_type and location_type not in type_choices: raise ValidationError( _("Invalid location_type: '%s'.") % location_type) except ValueError: raise ValidationError(_("No 'location_type' column found.")) if party_name_field and geometry_field: try: tenure_type = row[headers.index(tenure_type_field)] if tenure_type and not TenureRelationshipType.objects.filter( id=tenure_type).exists(): raise ValidationError( _("Invalid tenure_type: '%s'.") % tenure_type) except ValueError: raise ValidationError(_("No 'tenure_type' column found.")) return (party_name, party_type, geometry, location_type, tenure_type)
def validate_row(headers, row, config): party_name, party_type, geometry, tenure_type, location_type = (None, None, None, None, None) (party_name_field, party_type_field, location_type_field, type, geometry_field, tenure_type_field) = get_fields_from_config(config) if len(headers) != len(row): raise ValidationError(_("Number of headers and columns do not match.")) _get_field_value = partial(get_field_value, headers, row) if party_name_field and party_type_field: party_name = _get_field_value(party_name_field, "party_name") party_type = _get_field_value(party_type_field, "party_type") if geometry_field: coords = _get_field_value(geometry_field, "geometry_field") if coords == '': geometry = None else: try: geometry = GEOSGeometry(coords) except (ValueError, GEOSException): try: geometry = GEOSGeometry(odk_geom_to_wkt(coords)) except InvalidODKGeometryError: raise ValidationError(_("Invalid geometry.")) if location_type_field: location_type = _get_field_value(location_type_field, "location_type") type_choices = config['allowed_location_types'] if location_type and location_type not in type_choices: raise ValidationError( _("Invalid location_type: '%s'.") % location_type) if party_name_field and geometry_field: tenure_type = _get_field_value(tenure_type_field, 'tenure_type') if tenure_type and tenure_type not in config['allowed_tenure_types']: raise ValidationError( _("Invalid tenure_type: '%s'.") % tenure_type) values = (party_name, party_type, geometry, location_type, tenure_type) if not all(sanitize_string(val) for val in values): raise ValidationError(SANITIZE_ERROR) return values
def validate_row(headers, row, config): party_name, party_type, geometry, tenure_type, location_type = ( None, None, None, None, None) (party_name_field, party_type_field, location_type_field, type, geometry_field, tenure_type_field) = get_fields_from_config(config) if len(headers) != len(row): raise ValidationError( _("Number of headers and columns do not match.") ) _get_field_value = partial(get_field_value, headers, row) if party_name_field and party_type_field: party_name = _get_field_value(party_name_field, "party_name") party_type = _get_field_value(party_type_field, "party_type") if geometry_field: coords = _get_field_value(geometry_field, "geometry_field") if coords == '': geometry = None else: try: geometry = GEOSGeometry(coords) except (ValueError, GEOSException): try: geometry = GEOSGeometry(odk_geom_to_wkt(coords)) except InvalidODKGeometryError: raise ValidationError(_("Invalid geometry.")) if location_type_field: location_type = _get_field_value(location_type_field, "location_type") type_choices = [choice[0] for choice in TYPE_CHOICES] if location_type and location_type not in type_choices: raise ValidationError( _("Invalid location_type: '%s'.") % location_type ) if party_name_field and geometry_field: tenure_type = _get_field_value(tenure_type_field, 'tenure_type') if tenure_type and not TenureRelationshipType.objects.filter( id=tenure_type).exists(): raise ValidationError( _("Invalid tenure_type: '%s'.") % tenure_type ) return (party_name, party_type, geometry, location_type, tenure_type)
def validate_row(headers, row, config): party_name, party_type, geometry, tenure_type, location_type = (None, None, None, None, None) (party_name_field, party_type_field, location_type_field, type, geometry_field, tenure_type_field) = get_fields_from_config(config) if len(headers) != len(row): raise ValidationError(_("Number of headers and columns do not match.")) _get_field_value = partial(get_field_value, headers, row) if party_name_field and party_type_field: party_name = _get_field_value(party_name_field, "party_name") party_type = _get_field_value(party_type_field, "party_type") if geometry_field: coords = _get_field_value(geometry_field, "geometry_field") if coords == '': geometry = None else: try: geometry = GEOSGeometry(coords) except (ValueError, GEOSException): try: geometry = GEOSGeometry(odk_geom_to_wkt(coords)) except InvalidODKGeometryError: raise ValidationError(_("Invalid geometry.")) if location_type_field: location_type = _get_field_value(location_type_field, "location_type") type_choices = [choice[0] for choice in TYPE_CHOICES] if location_type and location_type not in type_choices: raise ValidationError( _("Invalid location_type: '%s'.") % location_type) if party_name_field and geometry_field: tenure_type = _get_field_value(tenure_type_field, 'tenure_type') if tenure_type and not TenureRelationshipType.objects.filter( id=tenure_type).exists(): raise ValidationError( _("Invalid tenure_type: '%s'.") % tenure_type) return (party_name, party_type, geometry, location_type, tenure_type)
def validate_row(headers, row, config): party_name, party_type, geometry, tenure_type, location_type = ( None, None, None, None, None) (party_name_field, party_type_field, location_type_field, type, geometry_field, tenure_type_field) = get_fields_from_config(config) if len(headers) != len(row): raise ValidationError( _("Number of headers and columns do not match.") ) if party_name_field and party_type_field: try: party_name = row[headers.index(party_name_field)] except ValueError: raise ValidationError( _("No 'party_name' column found.") ) try: party_type = row[headers.index(party_type_field)] except ValueError: raise ValidationError( _("No 'party_type' column found.") ) if geometry_field: try: coords = row[headers.index(geometry_field)] except ValueError: raise ValidationError( _("No 'geometry_field' column found.") ) try: geometry = GEOSGeometry(coords) except: pass # try ODK geom parser if not geometry: try: geometry = odk_geom_to_wkt(coords) except InvalidODKGeometryError: raise ValidationError(_("Invalid geometry.")) if location_type_field: try: location_type = row[headers.index(location_type_field)] type_choices = [choice[0] for choice in TYPE_CHOICES] if location_type and location_type not in type_choices: raise ValidationError( _("Invalid location_type: '%s'.") % location_type ) except ValueError: raise ValidationError( _("No 'location_type' column found.") ) if party_name_field and geometry_field: try: tenure_type = row[ headers.index(tenure_type_field)] if tenure_type and not TenureRelationshipType.objects.filter( id=tenure_type).exists(): raise ValidationError( _("Invalid tenure_type: '%s'.") % tenure_type ) except ValueError: raise ValidationError( _("No 'tenure_type' column found.") ) return (party_name, party_type, geometry, location_type, tenure_type)
def import_data(self, config_dict, **kwargs): content_types = dict( (key, {}) for key in self.get_content_type_keys()) (attr_map, extra_attrs, extra_headers) = self.get_attribute_map() attributes = config_dict['attributes'] party_name_field = config_dict['party_name_field'] party_type = config_dict['party_type'] location_type = config_dict['location_type'] geometry_type_field = config_dict['geometry_type_field'] geometry_field = config_dict['geometry_field'] path = config_dict['file'] try: with transaction.atomic(): with open(path, 'r', newline='') as csvfile: reader = csv.reader( csvfile, delimiter=self.delimiter, quotechar=self.quotechar ) csv_headers = [h.lower() for h in next(reader)] for row in reader: if len(csv_headers) != len(row): raise ValueError( _("Number of headers and columns " "do not match") ) party_name = row[csv_headers.index(party_name_field)] coords = row[csv_headers.index(geometry_field)] geometry_type = row[csv_headers.index( geometry_type_field)] if geometry_type not in GEOMETRY_TYPES: raise ValueError( _("Invalid geometry type") ) geometry = odk_geom_to_wkt(coords) try: tenure_type = row[ csv_headers.index(TENURE_TYPE)] except ValueError as e: raise ValueError( _("No 'tenure_type' column found") ) content_types['party.party'] = { 'project': self.project, 'name': party_name, 'type': party_type, 'attributes': {} } content_types['spatial.spatialunit'] = { 'project': self.project, 'type': location_type, 'geometry': geometry, 'attributes': {} } for attr in attributes: attribute, content_type, name = attr_map.get(attr) if (attribute is not None): val = row[csv_headers.index(attr)] if (not attribute.required and val == ""): continue content_types[content_type][ 'attributes'][ attribute.name] = val party = Party.objects.create( **content_types['party.party'] ) su = SpatialUnit.objects.create( **content_types['spatial.spatialunit'] ) tt = TenureRelationshipType.objects.get(id=tenure_type) TenureRelationship.objects.create( project=self.project, party=party, spatial_unit=su, tenure_type=tt ) except Exception as e: raise exceptions.DataImportError( line_num=reader.line_num, error=e)