def handle(self, *args, **options): return domain = Domain.objects.get(name='Health') specifications = Specification.objects.filter(domain=domain) fields = [ u'uuid', u'upstream', u'source', u'name', u'version', u'date_modified', u'completeness', u'source_url', u'raw-source'] for specification in specifications: fields.append(specification.attribute.key) countries = Country.objects.all().order_by('name') for country in countries: polygons = country.polygon_geometry # query for each of ATTRIBUTE healthsites = get_heathsites_master().in_polygon( polygons) # generate shapefiles for country insert_to_shapefile(healthsites, fields, country.name) # generate shapefiles for all country insert_to_shapefile(get_heathsites_master(), fields, 'facilities')
def get(self, request, *args, **kwargs): validation = self.extract_request(request) if validation: return self.api_response( {'error': validation} ) # get data if 'extent' in request.GET: polygon = parse_bbox(request.GET.get('extent')) # if page is not presented, use page = 1 page = self.page if not page: page = 1 facilities = self.get_query_by_page( get_heathsites_master().in_polygon(polygon), page ) facilities = self.query_to_json(facilities, self.format) return self.api_response(facilities) elif 'page' in request.GET: if not self.page: return self.api_response( {'error': 'page is wrong type'} ) facilities = self.get_query_by_page(get_heathsites_master(), self.page) facilities = self.query_to_json(facilities, self.format) return self.api_response(facilities) else: return self.api_response( {'error': 'need parameter'} )
def get(self, request, *args, **kwargs): validation = self.extract_request(request) if validation: return self.api_response({'error': validation}) # get data if 'extent' in request.GET: polygon = parse_bbox(request.GET.get('extent')) # if page is not presented, use page = 1 page = self.page if not page: page = 1 facilities = self.get_query_by_page( get_heathsites_master().in_polygon(polygon), page) facilities = self.query_to_json(facilities, self.format) return self.api_response(facilities) elif 'page' in request.GET: if not self.page: return self.api_response({'error': "page is wrong type"}) facilities = self.get_query_by_page(get_heathsites_master(), self.page) facilities = self.query_to_json(facilities, self.format) return self.api_response(facilities) else: return self.api_response({'error': "need parameter"})
def handle(self, *args, **options): if len(args) != 2: icon_size = self.default_size else: try: icon_size = [int(size) for size in args[0:2]] except Exception as e: raise CommandError(str(e)) if any((size < 0 for size in icon_size)): # icon sizes should be positive raise CommandError('Icon sizes should be positive numbers') # check the folder if not os.path.exists(settings.CLUSTER_CACHE_DIR): os.makedirs(settings.CLUSTER_CACHE_DIR) for zoom in range(settings.CLUSTER_CACHE_MAX_ZOOM + 1): filename = os.path.join( settings.CLUSTER_CACHE_DIR, '{}_{}_{}_localities.json'.format(zoom, *icon_size) ) localities = get_heathsites_master().in_bbox(parse_bbox('-180,-90,180,90')) object_list = cluster(localities, zoom, *icon_size) with open(filename, 'wb') as cache_file: json.dump(object_list, cache_file) self.stdout.write('Generated cluster cache for zoom: %s' % zoom) for country in Country.objects.all(): self.stdout.write('Generating cluster for %s' % country.name) polygon = country.polygon_geometry localities = get_heathsites_master().in_polygon(polygon) for zoom in range(settings.CLUSTER_CACHE_MAX_ZOOM + 1): filename = os.path.join( settings.CLUSTER_CACHE_DIR, '{}_{}_{}_localities_{}.json'.format( zoom, icon_size[0], icon_size[1], country.name.encode('ascii', 'ignore') ) ) object_list = cluster(localities, zoom, *icon_size) with open(filename, 'wb') as cache_file: json.dump(object_list, cache_file) self.stdout.write('Generated cluster cache for zoom: %s' % zoom)
def handle(self, *args, **options): if len(args) != 2: icon_size = self.default_size else: try: icon_size = [int(size) for size in args[0:2]] except Exception as e: raise CommandError(str(e)) if any((size < 0 for size in icon_size)): # icon sizes should be positive raise CommandError('Icon sizes should be positive numbers') # check the folder if not os.path.exists(settings.CLUSTER_CACHE_DIR): os.makedirs(settings.CLUSTER_CACHE_DIR) for zoom in range(settings.CLUSTER_CACHE_MAX_ZOOM + 1): filename = os.path.join( settings.CLUSTER_CACHE_DIR, '{}_{}_{}_localities.json'.format(zoom, *icon_size) ) localities = get_heathsites_master().in_bbox(parse_bbox('-180,-90,180,90')) object_list = cluster(localities, zoom, *icon_size) with open(filename, 'wb') as cache_file: json.dump(object_list, cache_file) self.stdout.write('Generated cluster cache for zoom: %s' % zoom) for country in Country.objects.all(): self.stdout.write('Generated cluster for %s' % country.name) polygon = country.polygon_geometry localities = get_heathsites_master().in_polygon(polygon) for zoom in range(settings.CLUSTER_CACHE_MAX_ZOOM + 1): filename = os.path.join( settings.CLUSTER_CACHE_DIR, '{}_{}_{}_localities_{}.json'.format( zoom, icon_size[0], icon_size[1], country.name ) ) object_list = cluster(localities, zoom, *icon_size) with open(filename, 'wb') as cache_file: json.dump(object_list, cache_file) self.stdout.write('Generated cluster cache for zoom: %s' % zoom)
def search_place(request, place): # getting country's polygon result = {} result['locality_count'] = get_heathsites_master().count() result['countries'] = Country.objects.order_by('name').values( 'name').distinct() # geonames if place: google_maps_api_key = settings.GOOGLE_MAPS_API_KEY gmaps = googlemaps.Client(key=google_maps_api_key) try: geocode_result = gmaps.geocode(place)[0] viewport = geocode_result['geometry']['viewport'] northeast_lat = viewport['northeast']['lat'] northeast_lng = viewport['northeast']['lng'] southwest_lat = viewport['southwest']['lat'] southwest_lng = viewport['southwest']['lng'] request.session['tempe_bongkrek'] = 'alfonso' result['northeast_lat'] = "%f" % northeast_lat result['northeast_lng'] = "%f" % northeast_lng result['southwest_lat'] = "%f" % southwest_lat result['southwest_lng'] = "%f" % southwest_lng except: print "getting place error" return result
def handle(self, *args, **options): if options['countries']: countries = [ countries_name for countries_name in options['countries'].split(',') ] countries = Country.objects.filter(name__in=countries) else: countries = Country.objects.all() # check the folder if not os.path.exists(settings.STATISTIC_CACHE_DIR): os.makedirs(settings.STATISTIC_CACHE_DIR) try: # write world cache filename = os.path.join(settings.STATISTIC_CACHE_DIR, 'world_statistic') healthsites = get_heathsites_master().all() output = get_statistic(healthsites) result = json.dumps(output, cls=DjangoJSONEncoder) file = open(filename, 'w') file.write(result) # python will convert \n to os.linesep file.close( ) # you can omit in most cases as the destructor will call it print 'world cache is finished' except Exception: print 'skip world' for country in countries: try: polygons = country.polygon_geometry # write country cache filename = os.path.join(settings.STATISTIC_CACHE_DIR, country.name + '_statistic') healthsites = get_heathsites_master().in_polygon(polygons) output = get_statistic(healthsites) result = json.dumps(output, cls=DjangoJSONEncoder) file = open(filename, 'w') file.write(result) # python will convert \n to os.linesep file.close( ) # you can omit in most cases as the destructor will call it print country.name + ' cache is finished' except Exception as e: print e print 'skip'
def handle(self, *args, **options): if options['countries']: countries = [countries_name for countries_name in options['countries'].split(',')] countries = Country.objects.filter(name__in=countries) else: countries = Country.objects.all() # check the folder if not os.path.exists(settings.CLUSTER_CACHE_DIR): os.makedirs(settings.CLUSTER_CACHE_DIR) try: # write world cache filename = os.path.join( settings.CLUSTER_CACHE_DIR, 'world_statistic') healthsites = get_heathsites_master().all() output = get_statistic(healthsites) result = json.dumps(output, cls=DjangoJSONEncoder) file = open(filename, 'w') file.write(result) # python will convert \n to os.linesep file.close() # you can omit in most cases as the destructor will call it print 'world cache is finished' except Exception: print 'skip world' for country in countries: try: polygons = country.polygon_geometry # write country cache filename = os.path.join( settings.CLUSTER_CACHE_DIR, country.name + '_statistic' ) healthsites = get_heathsites_master().in_polygon( polygons) output = get_statistic(healthsites) result = json.dumps(output, cls=DjangoJSONEncoder) file = open(filename, 'w') file.write(result) # python will convert \n to os.linesep file.close() # you can omit in most cases as the destructor will call it print country.name + ' cache is finished' except Exception as e: print e print 'skip'
def handle(self, *args, **options): domain = Domain.objects.get(name="Health") specifications = Specification.objects.filter(domain=domain) for specification in specifications: fields.append(specification.attribute.key) countries = Country.objects.all().order_by('name') for country in countries: polygons = country.polygon_geometry # query for each of ATTRIBUTE healthsites = get_heathsites_master().in_polygon(polygons) insert_to_shapefile( healthsites, country.name) # generate shapefiles for country insert_to_shapefile( get_heathsites_master(), 'facilities') # generate shapefiles for all country
def get_healthsite_master_by_polygon(self, polygon, facility_type): """ Get healthsites master by polygon. :return: filtered facilities """ if facility_type: facilities_id = (Value.objects.filter( specification__attribute__key='type').filter( data=facility_type).values_list('locality__id', flat=True)) facilities = get_heathsites_master().filter( id__in=facilities_id).in_polygon(polygon) else: facilities = get_heathsites_master().in_polygon(polygon) if self.page: facilities = self.get_query_by_page(facilities, self.page) else: facilities = facilities[:100] return facilities
def get_healthsite_master_by_polygon(self, polygon, facility_type): """ Get healthsites master by polygon. :return: filtered facilities """ if facility_type: facilities_id = ( Value.objects. filter(specification__attribute__key='type'). filter(data=facility_type). values_list('locality__id', flat=True) ) facilities = get_heathsites_master().filter( id__in=facilities_id).in_polygon(polygon) else: facilities = get_heathsites_master().in_polygon(polygon) if self.page: facilities = self.get_query_by_page(facilities, self.page) else: facilities = facilities[:100] return facilities
def get(self, request, *args, **kwargs): super(FacilitiesApiView, self).get(request) # checking page if 'page' not in request.GET: page = None else: try: page = request.GET.get('page') page = int(page) if page == 0: return HttpResponse(self.formating_response( {'error': "page less than 1"}), content_type='application/json') except ValueError: return HttpResponse(self.formating_response( {'error': "page is not a number"}), content_type='application/json') # get data if 'extent' in request.GET: polygon = parse_bbox(request.GET.get('extent')) if not page: page = 1 facilities = self.get_heathsites_by_page( get_heathsites_master().in_polygon(polygon), page) return HttpResponse(self.formating_response(facilities), content_type='application/json') elif 'page' in request.GET: if not page: return HttpResponse(self.formating_response( {'error': "page is wrong type"}), content_type='application/json') facilities = self.get_heathsites_by_page(get_heathsites_master(), page) return HttpResponse(self.formating_response(facilities), content_type='application/json') else: return HttpResponse(self.formating_response( {'error': "need parameter"}), content_type='application/json')
def get_healthsite_master_by_polygon(self, polygon, facility_type): healthsites = get_heathsites_master().in_polygon(polygon) output = [] index = 1; for healthsite in healthsites: if healthsite.is_type(facility_type): if self.format == 'geojson': output.append(geojson_serializer(healthsite)) else: output.append(json_serializer(healthsite)) index += 1 if index == self.limit: break return output
def get(self, request, *args, **kwargs): context = self.get_context_data(**kwargs) context['debug'] = settings.DEBUG context['locality_count'] = get_heathsites_master().count() if request.user.is_authenticated(): if request.user.is_staff: context['uploader'] = True else: permission = DataLoaderPermission.objects.filter( uploader=request.user) if len(permission) <= 0: context['uploader'] = False else: context['uploader'] = True else: context['uploader'] = False return self.render_to_response(context)
def search_place(request, place): # getting country's polygon result = {} result['locality_count'] = get_heathsites_master().count() result['countries'] = Country.objects.order_by('name').values('name').distinct() # geonames if place: google_maps_api_key = settings.GOOGLE_MAPS_API_KEY gmaps = googlemaps.Client(key=google_maps_api_key) try: geocode_result = gmaps.geocode(place)[0] viewport = geocode_result['geometry']['viewport'] northeast_lat = viewport['northeast']['lat'] northeast_lng = viewport['northeast']['lng'] southwest_lat = viewport['southwest']['lat'] southwest_lng = viewport['southwest']['lng'] request.session['tempe_bongkrek'] = 'alfonso' result['northeast_lat'] = '%f' % northeast_lat result['northeast_lng'] = '%f' % northeast_lng result['southwest_lat'] = '%f' % southwest_lat result['southwest_lng'] = '%f' % southwest_lng except: print 'getting place error' return result