def write_points(points, base_dir, service_suffix):
    """Write the sorted points into separate directories and files.

    Args:
        points (list of Point): A list of points sorted by species name.
        base_dir (str): The base directory to write points.
        service_suffix (str): The service suffix for the data files.
    """
    current_species = None
    out_file = None
    for point in points:
        if point.species_name != current_species:
            if out_file:
                out_file.close()
            current_species = point.species_name
            #print(point)
            #print(current_species)
            out_file = open(
                get_species_filename(current_species, base_dir, service_suffix,
                                     '.csv'), 'w')
        if isinstance(point.flags, list):
            flags = ','.join(point.flags)
        elif isinstance(point.flags, str):
            flags = point.flags
        else:
            flags = ''
        out_file.write('{}, {}, {}, "{}"\n'.format(point.species_name, point.x,
                                                   point.y, flags))
    if out_file:
        out_file.close()
Exemplo n.º 2
0
def get_gbif(species_key, species_name, base_dir):
    """Attempt to get gbif record for species."""
    # Get points
    json_points = get_points_from_gbif(species_key)
    converted_points = []
    if len(json_points) > 0:
        # Convert points
        converted_points = convert_json_to_point(
            json_points,
            itemgetter('species'),
            itemgetter('decimalLongitude'),
            itemgetter('decimalLatitude'),
            flags_getter=itemgetter('issues'))
    # Get file name
    sp_filename = get_species_filename(species_name, base_dir, '_gbif', '.csv')
    # Write points
    write_points(sp_filename, converted_points)
Exemplo n.º 3
0
def get_powo(species_key, species_name, base_dir):
    """Attempt to get powo record for species."""
    # Get data
    #try:
    result = {}
    fq_id = get_kew_id_for_species(species_name)
    #except Exception:
    #    fq_id = None
    if fq_id:
        result = get_species_kew(fq_id)

    # Get file name
    sp_filename = get_species_filename(species_name, base_dir, '_powo',
                                       '.json')
    # Write
    with open(sp_filename, 'w', encoding='utf8') as powo_out:
        json.dump(result, powo_out)
Exemplo n.º 4
0
def get_idigbio(species_key, species_name, base_dir):
    """Attempt to get idigbio record for species."""
    # Get points
    json_points = get_points_from_idigbio(species_key=species_key)
    converted_points = []
    if len(json_points) > 0:
        # Convert points
        converted_points = convert_json_to_point(
            json_points,
            species_chain_getter('indexTerms', 'canonicalname'),
            chain_getter('indexTerms', 'geopoint', 'lon'),
            chain_getter('indexTerms', 'geopoint', 'lat'),
            flags_getter=chain_getter('indexTerms', 'flags'))
    # Get file name
    sp_filename = get_species_filename(species_name, base_dir, '_idigbio',
                                       '.csv')
    # Write points
    write_points(sp_filename, converted_points)