Ejemplo n.º 1
0
 def __init__(self,
              lat=None,
              lon=None,
              pop=None,
              name=None,
              area=None,
              forced_id=None):
     Node.__init__(self, lat, lon, pop, name, area, forced_id)
     self.air_temperature = []
     self.land_temperature = []
     self.rainfall = []
     self.humidity = []
def duplicate_nodeID_check(nodelist):
    nodeIDs = pd.Series([n.id for n in nodelist])
    dups = nodeIDs.duplicated()
    while any(dups):
        # In lieu of something more clever, find the first non-unique, find a nearby unused ID,
        # and loop until all IDs are unique
        ind2fix = dups[dups].index[0]
        oldNodeID = nodeIDs[ind2fix]
        newNodeID = oldNodeID
        shift = 0
        while newNodeID == oldNodeID:
            shift += 1
            for xs in range(-1 * shift, shift):
                for ys in range(-1 * shift, shift):
                    testId = oldNodeID + xs * 2**16 + ys
                    if not any(nodeIDs.isin([testId])):
                        newNodeID = testId
        nodeIDs[ind2fix] = newNodeID
        # nodelist[ind2fix]['NodeID'] = int(newNodeID)
        n = deepcopy(nodelist[ind2fix])
        nodelist[ind2fix] = Node(lat=n.lat,
                                 lon=n.lon,
                                 pop=n.pop,
                                 forced_id=int(newNodeID),
                                 extra_attributes=n.extra_attributes)
        dups = nodeIDs.duplicated()
    return nodelist
def fill_nodes(node_info, res=30 / 3600):
    out_nodes = []
    states2keep = ['katsina', 'kano', 'jigawa', 'kaduna', 'bauchi']
    for index, row in node_info.iterrows():
        if row['dot_name'].split(':')[1] in states2keep:
            pop = int(max(5000, 1000 * row['population']))
            lat = row['latitude']
            lon = row['longitude']
            extra_attributes = {
                'Area_deg2': row['area'],
                'Area_km2': row['area'] * 111 * 111
            }
            meta = {'dot_name': row['dot_name']}

            if (pop / extra_attributes['Area_deg2']) > 10000000:
                extra_attributes['Urban'] = 1
                extra_attributes['BirthRate'] = 0.00024
            else:
                extra_attributes['Urban'] = 0
                extra_attributes['BirthRate'] = 0.000288

            node = Node(lat=lat,
                        lon=lon,
                        pop=pop,
                        name=int(node_ID_from_lat_long(lat, lon, res)),
                        forced_id=int(node_ID_from_lat_long(lat, lon, res)),
                        extra_attributes=extra_attributes,
                        meta=meta)

            out_nodes.append(node)
    out_nodes = duplicate_nodeID_check(out_nodes)
    return out_nodes
    def from_file(cls, base_file):
        content = json.load(open(base_file, 'rb'))
        nodes = []

        # Load the nodes
        for node in content["Nodes"]:
            nodes.append(Node.from_data(node))

        # Load the idref
        idref = content['Metadata']['IdReference']

        # Create the file
        return cls(nodes, idref, base_file)
    def from_file(cls,
                  cb,
                  population_input_file,
                  demographics_type='static',
                  res_in_arcsec=DEFAULT_RESOLUTION,
                  update_demographics=None,
                  default_pop=1000):
        nodes_list = list()
        with open(population_input_file, 'r') as pop_csv:
            reader = csv.DictReader(pop_csv)
            for row in reader:
                # Latitude
                if not 'lat' in row:
                    raise ValueError(
                        'Column lat is required in input population file.')
                lat = float(row['lat'])

                # Longitude
                if not 'lon' in row:
                    raise ValueError(
                        'Column lon is required in input population file.')
                lon = float(row['lon'])

                # Node label
                res_in_deg = cls.arcsec_to_deg(res_in_arcsec)
                node_label = row[
                    'node_label'] if 'node_label' in row else nodeid_from_lat_lon(
                        lat, lon, res_in_deg)

                # Population
                pop = int(float(row['pop'])) if 'pop' in row else default_pop

                # Append the newly created node to the list
                nodes_list.append(Node(lat, lon, pop, node_label))

        return cls(cb, nodes_list, demographics_type, res_in_arcsec,
                   update_demographics, default_pop)