Esempio n. 1
0
def computeTripLegsEmissions(request):
    startTime = datetime.datetime.now()
    post = request.POST
    transportMeanType = post['transportMeanType']
    gmaps = GoogleMaps(GOOGLE_MAPS_API_KEY)
    startLatLong = simplejson.loads(post['startLatLong'])
    endLatLong = simplejson.loads(post['endLatLong'])
    
    directions = gmaps.directions(gmaps.latlng_to_address(startLatLong[0], startLatLong[1]), \
                                  gmaps.latlng_to_address(endLatLong[0], endLatLong[1]) )
    drivingDistance = Decimal(directions['Directions']['Distance']['meters']) / 1000
    tripLeg= models.TripLeg.objects.get(id=post['tripLegId'])
    
    #get the transport mean used during this trip leg
    transportMean = tripLeg.transportMean

    if post['calculationMethod'] == 'tier1':
        #get tier1 calculation method id
        calculationMethod = models.C02CalculationMethod.objects.get(tier='1')
        
        if transportMeanType == 'generalCar':
            #get emission factor corresponding to this general car
            emissionFactor = models.TransportMeanEmissionFactor.objects.get(transportMean_content_type=ContentType.objects.get_for_model(models.GeneralCar),\
                                                                            transportMean_id=transportMean.id).emissionFactor            
        elif transportMeanType == 'bus':
            emissionFactor = models.TransportMeanEmissionFactor.objects.get(transportMean_content_type=ContentType.objects.get_for_model(models.Bus),\
                                                                            transportMean_id=transportMean.id).emissionFactor 
        elif transportMeanType == 'taxi':
            emissionFactor = models.TransportMeanEmissionFactor.objects.get(transportMean_content_type=ContentType.objects.get_for_model(models.Taxi),\
                                                                            transportMean_id=transportMean.id).emissionFactor 
        elif transportMeanType == 'motorcycle':
            emissionFactor = models.TransportMeanEmissionFactor.objects.get(transportMean_content_type=ContentType.objects.get_for_model(models.Motorcycle),\
                                                                            transportMean_id=transportMean.id).emissionFactor 
        elif transportMeanType == 'ferry':
            emissionFactor = models.TransportMeanEmissionFactor.objects.get(transportMean_content_type=ContentType.objects.get_for_model(models.Ferry),\
                                                                            transportMean_id=transportMean.id).emissionFactor 
        elif transportMeanType == 'rail':
            emissionFactor = models.TransportMeanEmissionFactor.objects.get(transportMean_content_type=ContentType.objects.get_for_model(models.Rail),\
                                                                            transportMean_id=transportMean.id).emissionFactor 
        elif transportMeanType == 'airplane':
            emissionFactor = models.TransportMeanEmissionFactor.objects.get(transportMean_content_type=ContentType.objects.get_for_model(models.Airplane),\
                                                                            transportMean_id=transportMean.id).emissionFactor                                                              
        ghgEmissions = drivingDistance * emissionFactor.directGHGEmissions
            
        #save the computed value
        tripLegEmission = models.TripLegCarbonEmission.objects.create(tripLeg=tripLeg, method=calculationMethod,\
                                                                          emissionFactor=emissionFactor, emissions=ghgEmissions)
    elif post['calculationMethod'] == 'tier2':
        #get tier2 calculation method id
        calculationMethod = models.C02CalculationMethod.objects.get(tier='2')
        #get emission factor corresponding to the specific car model
        emissionFactor = models.TransportMeanEmissionFactor.objects.get(transportMean_content_type=ContentType.objects.get_for_model(models.Car),\
                                                                            transportMean_id=transportMean.id).emissionFactor            
        ghgEmissions = drivingDistance * emissionFactor.directGHGEmissions
            
        #save the computed value
        tripLegEmission = models.TripLegCarbonEmission.objects.create(tripLeg=tripLeg, method=calculationMethod,\
                                                                          emissionFactor=emissionFactor, emissions=ghgEmissions)
    endTime = datetime.datetime.now()
    #createProvenanceGraph
    provManager = ProvManager()
    bundle = provManager.createTripLegEmissionGraph(tripLegEmission.id, calculationMethod.id, transportMean.id, transportMeanType,\
                                                            emissionFactor.id, emissionFactor.source.id, drivingDistance, tripLeg.id, tripLeg.startAddress.id,\
                                                            tripLeg.endAddress.id, startTime, endTime)
    tripLegEmission.provBundle = bundle
    tripLegEmission.save()

    
    json = simplejson.dumps( {'status': 'OK'} )
    
    return HttpResponse(json, mimetype='application/json')