def get_statistic_with_cache(extent, country, timestamp_from, timestamp_to): """ Checking cache of statistic data How it is work? Get cache, if it presents, return cache If not, call statistic function and save into cache. :param extent: extent of data :type extent: str (with comma separator) :param country: specific country :type country: str :return: json """ cached_data = get_statistic_cache(extent, country) country_data_into_statistic_task.delay(extent, country) # if cahed data presented if cached_data: return cached_data # if not return from statistic return get_statistic( filter_locality( extent=extent, country=country, timestamp_from=timestamp_from, timestamp_to=timestamp_to ) )
def country_data_into_shapefile(country=None): """ Convert country osm data into shapefile :param country_name: Country name :type: str """ queryset = filter_locality(extent=None, country=country).order_by('row') country_name = 'World' if country: country_name = country # get field that needs to be saved fields = fields_for_model(LocalityOSM).keys() insert_to_shapefile( LocalityOSMSerializer(queryset, many=True).data, fields, country_name)
def get(self, request): q = request.GET.get('q', '').capitalize() if len(q) > 2: if ' in ' in q: search_query = q.split(' in ') place = search_query[1] q = search_query[0] try: query = filter_locality(place=place) except: # noqa query = all_locality() else: query = all_locality() search_qs = query.filter(name__icontains=q) serializer = \ LocalityOSMAutoCompleteSerializer(search_qs, many=True) return Response(serializer.data) else: return HttpResponseBadRequest('Insufficient characters.')
def handle(self, *args, **options): """ Do your work here """ extent = options.get('extent', None) country = options.get('country', None) filename = get_statistic_cache_filename(extent=extent, country=country) # create already run indicator dirname = os.path.dirname(filename) is_run_file = os.path.join(dirname, 'is_run') if os.path.exists(filename): if os.path.exists(is_run_file): print '%s statistic generation already run' % country LOG.info('%s statistic generation already run' % country) return try: file = open(is_run_file, 'w+') file.close() except IOError as e: pass cache_data = get_statistic_cache(extent, country) healthsites = filter_locality(extent=extent, country=country) if cache_data: if cache_data['localities'] == healthsites.count(): LOG.info('%s statistic generated skipped' % country) os.remove(is_run_file) # remove run indicator return statistic = get_statistic(healthsites) # save the process LOG.info('%s statistic generated' % country) try: file = open(filename, 'w+') file.write(json.dumps(statistic)) file.close() os.remove(is_run_file) # remove run indicator except IOError as e: pass except Exception as e: print e
def get_healthsites(self, request): extent = request.GET.get('extent', None) country = request.GET.get('country', None) timestamp_from = request.GET.get('from', None) timestamp_to = request.GET.get('to', None) if timestamp_from: timestamp_from = datetime.fromtimestamp(int(timestamp_from)) if timestamp_to: timestamp_to = datetime.fromtimestamp(int(timestamp_to)) # check extent data try: queryset = filter_locality(extent=extent, country=country, timestamp_from=timestamp_from, timestamp_to=timestamp_to) except Exception as e: raise BadRequestError('%s' % e) return queryset
def country_data_into_shapefile(country=None): """ Convert country osm data into shapefile :param country: Country name :type: str """ if country == 'World' or country == 'world': country = None queryset = filter_locality(extent=None, country=country).order_by('row') country_name = 'World' if country: country_name = country # get field that needs to be saved fields = fields_for_model(LocalityOSM).keys() # get the folders shp_filename = country_name.replace('.', '') dir_cache = get_shapefile_folder(country_name) dir_shapefile = os.path.join(dir_cache, 'output') # delete the cache try: shutil.rmtree(dir_shapefile) except OSError: pass # generate the node shapefile insert_to_shapefile(query=queryset.filter(osm_type=LocalityOSMView.NODE), fields=fields, dir_shapefile=dir_shapefile, shp_filename=u'{}-node'.format(shp_filename), TYPE=shapefile.POINT) # generate the way shapefile insert_to_shapefile(query=queryset.filter(osm_type=LocalityOSMView.WAY), fields=fields, dir_shapefile=dir_shapefile, shp_filename=u'{}-way'.format(shp_filename), TYPE=shapefile.POLYGON) # zip this output print 'rezipping the files' if not os.path.exists(directory_media): os.makedirs(directory_media) filename = os.path.join(directory_media, '%s.zip' % country_name) try: os.remove(filename) except OSError: pass # copy the licenses for license in settings.LICENSES: name = os.path.basename(license) shutil.copyfile(license, os.path.join(dir_shapefile, name)) zipf = zipfile.ZipFile(filename, 'w', allowZip64=True) zipdir(dir_shapefile, zipf) zipf.close() try: shutil.rmtree(dir_cache) except OSError: pass