Exemple #1
0
def missinggeo(csv_path):
    log.info(csv_path)

    with codecs.open(csv_path, "r", "utf-8") as csv_file:
        geometry = Geometry()
        p = 0
        for line in csv_file.read().splitlines():
            line = re.sub("#.*$", "", line)
            line = line.strip()
            if not line:
                continue
            parts = [part.strip() for part in line.split(DELIMITER)]
            (iso2, name) = parts[:2]
            (latitude, longitude) = [float(value) for value in parts[2:]]

            geometry.add_point(longitude, latitude, 0)
            geometry.set_point_attr_string("iso2", p, iso2)
            geometry.set_point_attr_string("name", p, name)
            p += 1

        sys.stdout.write(geometry.render().encode("utf-8"))
Exemple #2
0
def missinggeo(csv_path):
    log.info(csv_path)

    with codecs.open(csv_path, "r", "utf-8") as csv_file:
        geometry = Geometry()
        p = 0
        for line in csv_file.read().splitlines():
            line = re.sub("#.*$", "", line)
            line = line.strip()
            if not line:
                continue
            parts = [part.strip() for part in line.split(DELIMITER)]
            (iso2, name) = parts[:2]
            (latitude, longitude) = [float(value) for value in parts[2:]]

            geometry.add_point(longitude, latitude, 0)
            geometry.set_point_attr_string("iso2", p, iso2)
            geometry.set_point_attr_string("name", p, name)
            p += 1

        sys.stdout.write(geometry.render().encode("utf-8"))
Exemple #3
0
def shp2geo(sf):
    geometry = Geometry()
    index_admin = sf.get_field("ADMIN")
    index_iso2 = sf.get_field("ISO_A2")

    p = 0
    for s, shaperecord in enumerate(sf.records()):
        shaperecord = sf.shapeRecord(s)
        admin = shaperecord.record[index_admin]
        iso2 = shaperecord.record[index_iso2]

        parts = shaperecord.shape.parts
        part_shapes = []
        for i, point in enumerate(shaperecord.shape.points):
            if i in parts:
                part_shapes.append([])
            part_shapes[-1].append(point)
        p += len(part_shapes)
        for part_shape in part_shapes:
            point_numbers = []
            for point in part_shape[:-1]:  # because it's closed
                point_number = geometry.add_point(trunc(point[0]),
                                                  trunc(point[1]), 0.0)
                point_numbers.append(point_number)
            prim_number = geometry.add_prim(point_numbers)

            geometry.set_prim_attr_string('iso2', prim_number, iso2)
            geometry.set_prim_attr_int('prim', prim_number, prim_number)
    log.warning(repr((s, p)))

    d = defaultdict(int)
    for p, point in enumerate(geometry.points):
        d[point] += 1
    for p, point in enumerate(geometry.points):
        geometry.set_point_attr_int('freq', p, d[point])

    return geometry
Exemple #4
0
def shp2geo(sf):
    geometry = Geometry()
    index_admin = sf.get_field("ADMIN")
    index_iso2 = sf.get_field("ISO_A2")

    p = 0
    for s, shaperecord in enumerate(sf.records()):
        shaperecord = sf.shapeRecord(s)
        admin = shaperecord.record[index_admin]
        iso2 = shaperecord.record[index_iso2]

        parts = shaperecord.shape.parts
        part_shapes = []
        for i, point in enumerate(shaperecord.shape.points):
            if i in parts:
                part_shapes.append([])
            part_shapes[-1].append(point)
        p += len(part_shapes)
        for part_shape in part_shapes:
            point_numbers = []
            for point in part_shape[:-1]:  # because it's closed
                point_number = geometry.add_point(trunc(point[0]), trunc(point[1]), 0.0)
                point_numbers.append(point_number)
            prim_number = geometry.add_prim(point_numbers)

            geometry.set_prim_attr_string("iso2", prim_number, iso2)
            geometry.set_prim_attr_int("prim", prim_number, prim_number)
    log.warning(repr((s, p)))

    d = defaultdict(int)
    for p, point in enumerate(geometry.points):
        d[point] += 1
    for p, point in enumerate(geometry.points):
        geometry.set_point_attr_int("freq", p, d[point])

    return geometry
Exemple #5
0
def shp2geo(sf, iso32, border_switch, border_deny):
    geometry = Geometry()

    index_iso3_left = sf.get_field("adm0_a3_l")
    index_iso3_right = sf.get_field("adm0_a3_r")
    index_type = sf.get_field("type")

    p = 0
    for s, shaperecord in enumerate(sf.records()):
        shaperecord = sf.shapeRecord(s)
        iso3_left = shaperecord.record[index_iso3_left]
        iso3_right = shaperecord.record[index_iso3_right]
        type_ = shaperecord.record[index_type]

        try:
            iso2_left = iso32[iso3_left]
            iso2_right = iso32[iso3_right]
        except KeyError as e:
            log.warning(repr((iso3_left, iso3_right, str(e))))
            raise e

        border_key = tuple(sorted([iso2_left, iso2_right]))

        if border_key in border_switch:
            log.warning(repr(border_switch))
            log.warning("switch %s -> %s" % (repr(border_key), repr(border_switch[border_key])))
            iso2_left, iso2_right = border_switch[border_key]
            border_key = tuple(sorted([iso2_left, iso2_right]))            
            log.info(shaperecord.shape.parts)

        if border_key in border_deny:
            log.warning("inhibit %s" % repr(border_key))
            continue

        if iso2_left == iso2_right:
            continue

        if hasattr(shaperecord.shape, "parts"):
            parts = shaperecord.shape.parts
        else:
            log.warning(s)
            parts = []
        part_shapes = []
        for i, point in enumerate(shaperecord.shape.points):
            if i in parts:
                part_shapes.append([])
            part_shapes[-1].append(point)
        p += len(part_shapes)
        for part_shape in part_shapes:
            point_numbers = []
            for point in part_shape[:]:  # because it's open
                point_number = geometry.add_point(trunc(point[0]), trunc(point[1]), 0.0)
                point_numbers.append(point_number)
            prim_number = geometry.add_prim(point_numbers, closed=False)
            
            geometry.set_prim_attr_string('iso2_left', prim_number, iso2_left)
            geometry.set_prim_attr_string('iso2_right', prim_number, iso2_right)
            geometry.set_prim_attr_string('type', prim_number, type_)
            geometry.set_prim_attr_int('prim', prim_number, prim_number)

    d = defaultdict(int)
    for p, point in enumerate(geometry.points):
        d[point] += 1
    for p, point in enumerate(geometry.points):
        geometry.set_point_attr_int('freq', p, d[point])
    
    return geometry
Exemple #6
0
def shp2geo(sf, iso32, border_switch, border_deny):
    geometry = Geometry()

    index_iso3_left = sf.get_field("adm0_a3_l")
    index_iso3_right = sf.get_field("adm0_a3_r")
    index_type = sf.get_field("type")

    p = 0
    for s, shaperecord in enumerate(sf.records()):
        shaperecord = sf.shapeRecord(s)
        iso3_left = shaperecord.record[index_iso3_left]
        iso3_right = shaperecord.record[index_iso3_right]
        type_ = shaperecord.record[index_type]

        try:
            iso2_left = iso32[iso3_left]
            iso2_right = iso32[iso3_right]
        except KeyError as e:
            log.warning(repr((iso3_left, iso3_right, str(e))))
            raise e

        border_key = tuple(sorted([iso2_left, iso2_right]))

        if border_key in border_switch:
            log.warning(repr(border_switch))
            log.warning("switch %s -> %s" %
                        (repr(border_key), repr(border_switch[border_key])))
            iso2_left, iso2_right = border_switch[border_key]
            border_key = tuple(sorted([iso2_left, iso2_right]))
            log.info(shaperecord.shape.parts)

        if border_key in border_deny:
            log.warning("inhibit %s" % repr(border_key))
            continue

        if iso2_left == iso2_right:
            continue

        if hasattr(shaperecord.shape, "parts"):
            parts = shaperecord.shape.parts
        else:
            log.warning(s)
            parts = []
        part_shapes = []
        for i, point in enumerate(shaperecord.shape.points):
            if i in parts:
                part_shapes.append([])
            part_shapes[-1].append(point)
        p += len(part_shapes)
        for part_shape in part_shapes:
            point_numbers = []
            for point in part_shape[:]:  # because it's open
                point_number = geometry.add_point(trunc(point[0]),
                                                  trunc(point[1]), 0.0)
                point_numbers.append(point_number)
            prim_number = geometry.add_prim(point_numbers, closed=False)

            geometry.set_prim_attr_string('iso2_left', prim_number, iso2_left)
            geometry.set_prim_attr_string('iso2_right', prim_number,
                                          iso2_right)
            geometry.set_prim_attr_string('type', prim_number, type_)
            geometry.set_prim_attr_int('prim', prim_number, prim_number)

    d = defaultdict(int)
    for p, point in enumerate(geometry.points):
        d[point] += 1
    for p, point in enumerate(geometry.points):
        geometry.set_point_attr_int('freq', p, d[point])

    return geometry