Skip to content

Commit

Permalink
Fix #831: Add support for WKT in Data Import (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohare authored and oliverroick committed Nov 2, 2016
1 parent f253efa commit 3615447
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 95 deletions.
1 change: 0 additions & 1 deletion cadasta/organization/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,6 @@ class SelectDefaultsForm(forms.Form):
choices=LOCATION_TYPE_CHOICES, required=True
)
geometry_field = forms.CharField(required=False)
geometry_type_field = forms.CharField(required=False)

def __init__(self, project, user, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
27 changes: 12 additions & 15 deletions cadasta/organization/importers/csv.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import csv
from collections import OrderedDict

from django.contrib.gis.geos import GEOSGeometry
from django.db import transaction
from django.utils.translation import ugettext as _
from party.models import Party, TenureRelationship, TenureRelationshipType
Expand All @@ -16,7 +17,6 @@
'party.tenurerelationship': 'Tenure Relationship',
'party.partyrelationship': 'Party Relationship'
}
GEOMETRY_TYPES = ['', 'geopoint', 'geotrace', 'geoshape']
TENURE_TYPE = 'tenure_type'


Expand All @@ -27,7 +27,7 @@ def __init__(self, project=None, path=None,
super(CSVImporter, self).__init__(project=project)
self.path = path
self.delimiter = ',' if not delimiter else delimiter
self.quotechar = '|' if not quotechar else quotechar
self.quotechar = '"' if not quotechar else quotechar

def get_headers(self):
headers = []
Expand Down Expand Up @@ -77,7 +77,6 @@ def import_data(self, config_dict, **kwargs):
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:
Expand All @@ -92,10 +91,8 @@ def import_data(self, config_dict, **kwargs):
# validate the row
(party_name, geometry,
tenure_type) = self._validate_row(
row, party_name_field, geometry_field,
geometry_type_field
row, party_name_field, geometry_field
)

# create models
content_types['party.party'] = {
'project': self.project,
Expand All @@ -121,22 +118,22 @@ def import_data(self, config_dict, **kwargs):
raise exceptions.DataImportError(
line_num=reader.line_num, error=e)

def _validate_row(self, row, party_name_field, geometry_field,
geometry_type_field):
def _validate_row(self, row, party_name_field, geometry_field):
if len(self.csv_headers) != len(row):
raise ValueError(
_("Number of headers and columns "
"do not match")
)
party_name = row[self.csv_headers.index(party_name_field)]
coords = row[self.csv_headers.index(geometry_field)]
geometry_type = row[self.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 to parse coords as WKT first
# if that fails try to parse ODK geom string
try:
geometry = GEOSGeometry(coords)
except:
geometry = odk_geom_to_wkt(coords)

try:
tenure_type = row[
self.csv_headers.index(TENURE_TYPE)]
Expand Down
2 changes: 1 addition & 1 deletion cadasta/organization/tests/files/invalid_col.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name_mouza,j_l,tenure_type,name_of_HH,name_father_hus,present_add,village_name,Mobile_No,Occupation_HH,educational_qualification,amount_agriland,amount_othersland,class_HH,How_aquire_landw,How_aquire_landwh,How_aquire_landm,How_aquire_landd,How_aquire_landt,How_aquire_landp,Land_Calculation,deed_of_land,Khatain_of_land,Mutation_of_land,Everything,female_member,male_member,ownership_conflict,boundary_conflict,others_conflict,conflicts_resoulation,howconflict_resoulation,location_problems,tenure_name,tenure_notes,geo_type,nid_number,location_geometry,_geopoint_hh_latitude,_geopoint_hh_longitude,_geopoint_hh_altitude,_geopoint_hh_precision,image_hh,audio_hh,start,end,today,deviceid,subscriberid,sim_serial,phonenumber,meta/instanceID,_id,_uuid,_submission_time,_index,_parent_table_name,_parent_index,_tags,_notes,_version,_duration,_submitted_by
name_mouza,j_l,tenure_type,name_of_HH,name_father_hus,present_add,village_name,Mobile_No,Occupation_HH,educational_qualification,amount_agriland,amount_othersland,class_HH,How_aquire_landw,How_aquire_landwh,How_aquire_landm,How_aquire_landd,How_aquire_landt,How_aquire_landp,Land_Calculation,deed_of_land,Khatain_of_land,Mutation_of_land,Everything,female_member,male_member,ownership_conflict,boundary_conflict,others_conflict,conflicts_resoulation,howconflict_resoulation,location_problems,tenure_name,tenure_notes,geo_type,nid_number,location_geometry,_geopoint_hh_latitude,_geopoint_hh_longitude,_geopoint_hh_altitude,_geopoint_hh_precision,image_hh,audio_hh,start,end,today,deviceid,subscriberid,sim_serial,phonenumber,meta/instanceID,_id,_uuid,_submission_time,_index,_parent_table_name,_parent_index,_tags,_notes,_version,_duration,_submitted_by,
chanderhowara,90,FH,সুরুজ্জামান,"মৃত শাহা,লী মন্ডল",মন্ডল বাড়ি,চান্দের হাওড়া,০১৭২৮১০১১৩৩,farmer,Illiterate,১৮০.০০ অারঙ্গহাটি ৩৬০.০০,১৫.০০,others,inheritance,inheritance,did_not_get_from_tafshil,inheritance,generations,inheritance,yes,no,yes,no,no,4,5,yes,no,কিছু বেখলে অাছে জোর করে দখল করে ভোগ করে অন্য জমির মালক।,yes,local_justice,conflict risk_of_eviction,Freehold,some notes,,3913647224045,,,,,,,,2016-02-08T10:43:19.861+06,2016-02-09T09:03:56.526+06,2016-02-08,355335064140032,470034501263565,8988034503012635653f,,uuid:c2b46f0e-dd3c-4c0b-858e-edd582047f6c,5349241,c2b46f0e-dd3c-4c0b-858e-edd582047f6c,2016-02-09T11:30:07,1,,-1,,,201602060307,80437,arifuttaran
chanderhowara,90,FH,অাব্দুল বারেক মন্ডল,মৃত বঙ্গু মন্ডল,মন্ডল বাড়ি,চান্দের হাওড়া,০১৭৯৮০৮১৯৯৪,farmer,Illiterate,৩০.০০ আরঙ্গহাটি ১২০.০০,১৫.০০,others,inheritance,inheritance,did_not_get_from_tafshil,inheritance,generations,inheritance,yes,yes,yes,no,no,5,4,no,no,,,,,Leasehold,some notes on the tenure,,3913647224033,,,,,,,,2016-02-08T11:13:52.453+06,2016-02-09T13:58:53.495+06,2016-02-08,355335064140032,470034501263565,8988034503012635653f,,uuid:4df81c07-29d2-4a75-9fe4-e7dcc4b05d16,5349244,4df81c07-29d2-4a75-9fe4-e7dcc4b05d16,2016-02-09T11:30:17,2,,-1,,,201602060307,96301,arifuttaran
chanderhowara,90,FH,হানিফ উদ্দিন,মৃত মুগল মন্ডল,অারঙ্গহাটি,পশ্চিম অারঙ্গহাটি,০১৭৯৮০৮১৯৯৪,farmer,litteracy,১৫.০০,১৫.০০,others,inheritance,inheritance,did_not_get_from_tafshil,inheritance,generations,inheritance,yes,yes,yes,yes,no,3,4,no,no,,,,,Customary,some more notes,,3913647225965,,,,,,,,2016-02-08T11:21:20.859+06,2016-02-09T13:58:16.633+06,2016-02-08,355335064140032,470034501263565,8988034503012635653f,,uuid:405a9d1c-3c54-42ca-929d-ab4f261ce59a,5349247,405a9d1c-3c54-42ca-929d-ab4f261ce59a,2016-02-09T11:30:28,3,,-1,,,201602060307,95816,arifuttaran
Expand Down
11 changes: 0 additions & 11 deletions cadasta/organization/tests/files/invalid_geom_type.csv

This file was deleted.

Loading

0 comments on commit 3615447

Please sign in to comment.