Пример #1
0
 def load(Class, path):
     if not exists(path):
         raise IOError
     if path.endswith('.zip'):
         import geometryIO
         from crosscompute_table import pandas as pd
         [
             proj4,
             geometries,
             field_packs,
             field_definitions,
         ] = geometryIO.load(path)
         # Convert to (longitude, latitude)
         normalize_geometry = geometryIO.get_transformGeometry(
             proj4 or geometryIO.proj4LL)
         geometries = [normalize_geometry(x) for x in geometries]
         # Convert to (latitude, longitude)
         geometries = transform_geometries(geometries, drop_z)
         geometries = transform_geometries(geometries, flip_xy)
         # Generate table
         table = pd.DataFrame(
             field_packs, columns=[x[0] for x in field_definitions])
         table['WKT'] = [x.wkt for x in geometries]
     else:
         table = super(GeotableType, Class).load(path)
     # TODO: Consider whether there is a better way to do this
     table.interpret = create_bound_method(_interpret, table)
     return table
def normalize_grid_mv_line_geotable(grid_mv_line_geotable, demand_point_table):
    'Make sure that grid mv lines use (latitude, longitude) coordinate order'
    raw_geometries = [wkt.loads(x) for x in grid_mv_line_geotable['wkt']]
    # Remove incompatible geometries
    geometries, xs = [], []
    for x, geometry in enumerate(raw_geometries):
        if geometry.type.endswith('LineString'):
            geometries.append(geometry)
        else:
            LOG.warn(
                'Ignoring incompatible geometry '
                'in grid_mv_line_geotable (%s)' % geometry.wkt)
            xs.append(x)
    grid_mv_line_geotable.drop(grid_mv_line_geotable.index[xs])
    # Remove elevation
    geometries = transform_geometries(geometries, drop_z)
    # Match coordinate order
    if geometries:
        regular = tuple(GeometryCollection(geometries).centroid.coords[0])[:2]
        flipped = regular[1], regular[0]
        reference = tuple(demand_point_table[['latitude', 'longitude']].mean())
        # If the flipped coordinates are closer,
        if get_distance(reference, flipped) < get_distance(reference, regular):
            # Flip coordinates to get (latitude, longitude) coordinate order
            geometries = transform_geometries(geometries, flip_xy)
    grid_mv_line_geotable['wkt'] = [x.wkt for x in geometries]
    return {'grid_mv_line_geotable': grid_mv_line_geotable}
def normalize_grid_mv_line_geotable(grid_mv_line_geotable, demand_point_table):
    'Make sure that grid mv lines use (longitude, latitude) coordinate order'
    raw_geometries = [wkt.loads(x) for x in grid_mv_line_geotable['wkt']]
    # Remove incompatible geometries
    geometries, xs = [], []
    for x, geometry in enumerate(raw_geometries):
        if geometry.type.endswith('LineString'):
            geometries.append(geometry)
        else:
            L.warn('Ignoring incompatible geometry '
                   'in grid_mv_line_geotable (%s)' % geometry.wkt)
            xs.append(x)
    grid_mv_line_geotable.drop(grid_mv_line_geotable.index[xs])
    # Remove elevation
    geometries = transform_geometries(geometries, drop_z)
    # Match coordinate order
    if geometries:
        regular = tuple(GeometryCollection(geometries).centroid.coords[0])[:2]
        flipped = regular[1], regular[0]
        reference = tuple(demand_point_table[['longitude', 'latitude']].mean())
        # If the flipped coordinates are closer,
        if get_distance(reference, flipped) < get_distance(reference, regular):
            # Flip coordinates to get (longitude, latitude) coordinate order
            geometries = transform_geometries(geometries, flip_xy)
    grid_mv_line_geotable['wkt'] = [x.wkt for x in geometries]
    return {'grid_mv_line_geotable': grid_mv_line_geotable}
Пример #4
0
def _get_instance_for_csv(instance, source_proj4, target_proj4):
    transform_shapely_geometry = get_transform_shapely_geometry(
        source_proj4, target_proj4)
    instance = instance.copy()
    geometries = [
        transform_shapely_geometry(g) for g in instance.pop('geometry_object')
    ]

    for column_name in instance.columns:
        normalized_column_name = _normalize_column_name(column_name)
        if normalized_column_name == 'wkt':
            break
        if target_proj4 != LONGITUDE_LATITUDE_PROJ4:
            continue
        if normalized_column_name == 'longitudelatitudewkt':
            break
        if normalized_column_name == 'latitudelongitudewkt':
            geometries = transform_geometries(geometries, flip_xy)
            break
    else:
        column_name = 'wkt'

    instance[column_name] = [g.wkt for g in geometries]
    instance['geometry_proj4'] = normalize_proj4(target_proj4 or source_proj4)
    return instance
def load_geotable(path):
    if path.endswith('.csv'):
        table = read_csv(path)
    elif path.endswith('.zip'):
        proj4, geometries, fields, definitions = geometryIO.load(path)
        normalize_geometry = geometryIO.get_transformGeometry(proj4)
        # Convert to (longitude, latitude)
        geometries = [normalize_geometry(x) for x in geometries]
        # Convert to (latitude, longitude)
        geometries = transform_geometries(geometries, flip_xy)
        table = DataFrame(fields, columns=[x[0] for x in definitions])
        table['WKT'] = [x.wkt for x in geometries]
    else:
        raise UnsupportedFormat('cannot load geotable (%s)' % path)
    return table
Пример #6
0
 def load(Class,
          source_path_or_url,
          source_proj4=None,
          target_proj4=None,
          drop_z=False,
          bounding_box=None,
          bounding_polygon=None,
          **kw):
     t = Class._load(source_path_or_url, source_proj4, target_proj4, **kw)
     if drop_z:
         t['geometry_object'] = geometry.transform_geometries(
             t['geometry_object'], geometry.drop_z)
     if bounding_box and bounding_box != (-180, -90, +180, +90):
         f = get_transform_shapely_geometry(LONGITUDE_LATITUDE_PROJ4,
                                            target_proj4)
         bounding_polygon = f(box(*bounding_box))
     if bounding_polygon:
         indices = [
             i for i, g in enumerate(t.geometries)
             if g.intersects(bounding_polygon)
         ]
         t = t.iloc[indices].copy()
     return t
Пример #7
0
def load(source_path, source_proj4=None, target_proj4=None, with_z=True, **kw):
    t = GeoTable.load(source_path, source_proj4, target_proj4, **kw)
    if not with_z:
        t['geometry_object'] = transform_geometries(t['geometry_object'],
                                                    drop_z)
    return t
Пример #8
0
 def load_flipped_geometry_object(row):
     geometry_object = load_geometry_object(row)
     return transform_geometries([geometry_object], flip_xy)[0]