예제 #1
0
#loads the classify_adm3_names dictionary, used to classify the admin 3 boundaries
from adm3_name_dict import classify_adm3_names

print classify_service_types

dataReader = csv.reader(open(csv_filepathname), delimiter=',', quotechar='"')
 
#The objects can't have the same name as the class, and a new object needs to be created each time a row is looped through

for row in dataReader:

	#print 'hi'
		
	if row[0] != 'id': # Ignore the header row, import everything else
		
		EffortInstanceObj = EffortInstance()
		
		EffortInstanceObj.effort_instance_id = row[0]
		
		if row[1] == 'Long-term':
			EffortInstanceObj.date_start = '9999-12-31'
			#print 'hey'
		else:
			#need to parse and re-enter the date format as year-month-day for django to like it'
			 
			split = re.split(r'\/', row[1].strip())
			
			print split[2] + '-' + split[0] + '-' + split[1]
			
			EffortInstanceObj.date_start = split[2] + '-' + split[0] + '-' + split[1]
		
예제 #2
0
def FillTables(locationOrder, adm2Bool, adm3Bool):

    if row[0] != 'id':  # Ignore the header row, import everything else

        #checks to see if sector category is health
        if re.search('Health', row[12]):

            print 'row: id:'
            print row[0]

            EffortInstanceObj = EffortInstance()

            EffortInstanceObj.effort_instance_id = row[0]

            if locationOrder == 2:

                EffortInstanceObj.effort_instance_id = row[0] + '02'

            if locationOrder == 3:

                EffortInstanceObj.effort_instance_id = row[0] + '03'

            split = re.split(r'\/', row[9].strip())

            print split[2] + '-' + split[0] + '-' + split[1]

            EffortInstanceObj.date_start = split[2] + '-' + split[
                0] + '-' + split[1]

            split = re.split(r'\/', row[10].strip())

            print split[2] + '-' + split[0] + '-' + split[1]

            EffortInstanceObj.date_end = split[2] + '-' + split[
                0] + '-' + split[1]

            #cool way to make Service Providers unique, if obj does not exist then it creates it.
            #https://docs.djangoproject.com/en/1.6/ref/models/querysets/#get-or-create
            ServiceProviderObj, created = ServiceProvider.objects.get_or_create(
                provider_name=row[1])

            EffortInstanceObj.service_provider = ServiceProvider.objects.get(
                provider_name=row[1])

            EffortInstanceObj.updated_on = utc_datetime

            EffortInstanceObj.updated_by = 'Haiti Aid Map scrape'

            #HaitiAipMap does not have any lat lon coords, so don't add a location point
            #Also, don't use random lat lon. It will break spatial queries

            #loc = Location()
            #loc.latitude = str(random.randint(0,10))
            #loc.longitude = str(random.randint(0,10))
            #loc.save()
            '''
			Insert new code here to match admin boundaries: The location_information column is in column 21 (row [20]). This contains a list
			of admin boundaries where the facility is located at. The list contains entries delimited with '>'. The first entry on the list is Haiti. 
			The second entry is an admin 2 boundary. The third entry is ?.
			
			There can be more than one place listed in the location column. If this is the case then we need to create a new effort instance
			for each location in Haiti.
			'''

            if row[20]:

                split_each_location = re.split(r'\|Haiti>', row[20].strip())

                if len(split_each_location) > 1:
                    split_loc = re.split(
                        r'>', split_each_location[locationOrder].strip())

                    if len(split_loc) > 1:

                        UpperAdmin1 = split_loc[0].upper()

                        UpperAdmin1 = re.split(r'\|', UpperAdmin1)

                        print 'length of split_loc:'

                        print len(split_loc)

                        print 'UpperAdmin1:'

                        print UpperAdmin1[0]

                        EffortInstanceObj.adm_1 = haiti_adm1_minustah.objects.get(
                            adm1=classify_adm1_names[UpperAdmin1[0]])

                        if len(split_loc) > 1:

                            UpperAdmin2 = split_loc[1].upper()

                            UpperAdmin2 = re.split(r'\|', UpperAdmin2)

                            print 'UpperAdmin2:'

                            print UpperAdmin2[0]

                            try:

                                EffortInstanceObj.adm_2 = haiti_adm2_minustah.objects.get(
                                    adm2=classify_adm2_names[UpperAdmin2[0]])

                            except:

                                print "guess no match"

                            if len(split_loc) > 2:

                                UpperAdmin3 = split_loc[2].upper()

                                UpperAdmin3 = re.split(r'\|', UpperAdmin3)

                                print 'UpperAdmin3:'

                                print UpperAdmin3[0]

                                try:

                                    EffortInstanceObj.adm_3 = haiti_adm3_minustah.objects.get(
                                        adm3=classify_adm3_names[
                                            UpperAdmin3[0]])

                                except:

                                    print "guess no match"

            #need a way first in seeing if a location exists close by
            #EffortInstanceObj.location = loc.objects.get
            EffortInstanceObj.save()

            if len(split_each_location) > 1:
                print 'location 1:'
                print split_each_location[1]

            if len(split_each_location) > 2 and adm2Bool == False:
                print 'location 2:'
                print split_each_location[2]
                FillTables(2, True, False)

            if len(split_each_location
                   ) > 3 and adm3Bool == False and adm2Bool == True:
                print 'location 3:'
                print split_each_location[3]
                FillTables(3, True, True)