def init(cls, eltype, data, kdims, vdims): import pandas as pd from geopandas import GeoDataFrame, GeoSeries if kdims is None: kdims = eltype.kdims if isinstance(data, GeoSeries): data = data.to_frame() if isinstance(data, list): if all(isinstance(d, geom_types) for d in data): data = [{'geometry': d} for d in data] if all( isinstance(d, dict) and 'geometry' in d and isinstance(d['geometry'], geom_types) for d in data): data = GeoDataFrame(data) if not isinstance(data, GeoDataFrame): vdims = vdims or eltype.vdims data = from_multi(eltype, data, kdims, vdims) elif not isinstance(data, GeoDataFrame): raise ValueError("GeoPandasInterface only support geopandas " "DataFrames not %s." % type(data)) elif 'geometry' not in data: cls.geo_column(data) if vdims is None: vdims = [ col for col in data.columns if not isinstance(data[col], GeoSeries) ] index_names = data.index.names if isinstance( data, pd.DataFrame) else [data.index.name] if index_names == [None]: index_names = ['index'] for kd in kdims + vdims: kd = dimension_name(kd) if kd in data.columns: continue if any(kd == ('index' if name is None else name) for name in index_names): data = data.reset_index() break try: shp_types = { gt[5:] if 'Multi' in gt else gt for gt in data.geom_type } except: shp_types = [] if len(shp_types) > 1: raise DataError( 'The GeopandasInterface can only read dataframes which ' 'share a common geometry type, found %s types.' % shp_types, cls) return data, {'kdims': kdims, 'vdims': vdims}, {}
def init(cls, eltype, data, kdims, vdims): odict_types = (OrderedDict, cyODict) if kdims is None: kdims = eltype.kdims if vdims is None: vdims = eltype.vdims dimensions = [dimension_name(d) for d in kdims + vdims] if isinstance(data, geom_types): data = {'geometry': data} if not cls.applies(data): raise ValueError( "GeomDictInterface only handles dictionary types " "containing a 'geometry' key and shapely geometry " "value.") unpacked = [] for d, vals in data.items(): if isinstance(d, tuple): vals = np.asarray(vals) if vals.shape == (0, ): for sd in d: unpacked.append((sd, np.array([], dtype=vals.dtype))) elif not vals.ndim == 2 and vals.shape[1] == len(d): raise ValueError("Values for %s dimensions did not have " "the expected shape.") else: for i, sd in enumerate(d): unpacked.append((sd, vals[:, i])) elif d not in dimensions: unpacked.append((d, vals)) else: if not isscalar(vals): vals = np.asarray(vals) if not vals.ndim == 1 and d in dimensions: raise ValueError( 'DictInterface expects data for each column to be flat.' ) unpacked.append((d, vals)) if not cls.expanded( [vs for d, vs in unpacked if d in dimensions and not isscalar(vs)]): raise ValueError( 'DictInterface expects data to be of uniform shape.') if isinstance(data, odict_types): data.update(unpacked) else: data = OrderedDict(unpacked) return data, {'kdims': kdims, 'vdims': vdims}, {}
def init(cls, eltype, data, kdims, vdims): import pandas as pd from geopandas import GeoDataFrame if isinstance(data, list): if all(isinstance(d, geom_types) for d in data): data = [{'geometry': d} for d in data] if all('geometry' in d and isinstance(d['geometry'], geom_types) for d in data): data = GeoDataFrame(data) elif not isinstance(data, GeoDataFrame): raise ValueError( "GeoPandasInterface only support geopandas DataFrames.") elif 'geometry' not in data: raise ValueError( "GeoPandas dataframe must contain geometry column, " "to plot non-geographic data use pandas DataFrame.", cls) if kdims is None: kdims = eltype.kdims if vdims is None: vdims = eltype.vdims index_names = data.index.names if isinstance( data, pd.DataFrame) else [data.index.name] if index_names == [None]: index_names = ['index'] for kd in kdims + vdims: kd = dimension_name(kd) if kd in data.columns: continue if any(kd == ('index' if name is None else name) for name in index_names): data = data.reset_index() break shp_types = {gt[5:] if 'Multi' in gt else gt for gt in data.geom_type} if len(shp_types) > 1: raise DataError( 'The GeopandasInterface can only read dataframes which ' 'share a common geometry type, found %s types.' % shp_types, cls) return data, {'kdims': kdims, 'vdims': vdims}, {}
def init(cls, eltype, data, kdims, vdims): import pandas as pd from geopandas import GeoDataFrame if isinstance(data, list): if all(isinstance(d, geom_types) for d in data): data = [{'geometry': d} for d in data] if all('geometry' in d and isinstance(d['geometry'], geom_types) for d in data): data = GeoDataFrame(data) elif not isinstance(data, GeoDataFrame): raise ValueError("GeoPandasInterface only support geopandas DataFrames.") elif 'geometry' not in data: raise ValueError("GeoPandas dataframe must contain geometry column, " "to plot non-geographic data use pandas DataFrame.", cls) if kdims is None: kdims = eltype.kdims if vdims is None: vdims = eltype.vdims index_names = data.index.names if isinstance(data, pd.DataFrame) else [data.index.name] if index_names == [None]: index_names = ['index'] for kd in kdims+vdims: kd = dimension_name(kd) if kd in data.columns: continue if any(kd == ('index' if name is None else name) for name in index_names): data = data.reset_index() break shp_types = {gt[5:] if 'Multi' in gt else gt for gt in data.geom_type} if len(shp_types) > 1: raise DataError('The GeopandasInterface can only read dataframes which ' 'share a common geometry type, found %s types.' % shp_types, cls) return data, {'kdims': kdims, 'vdims': vdims}, {}
def init(cls, eltype, data, kdims, vdims): odict_types = (OrderedDict, cyODict) if kdims is None: kdims = eltype.kdims if vdims is None: vdims = eltype.vdims dimensions = [dimension_name(d) for d in kdims + vdims] if isinstance(data, geom_types): data = {'geometry': data} elif not isinstance(data, dict) or 'geometry' not in data: xdim, ydim = kdims[:2] from shapely.geometry import (Point, LineString, Polygon, MultiPoint, MultiPolygon, MultiLineString, LinearRing) data = to_geom_dict(eltype, data, kdims, vdims, GeomDictInterface) geom = data.get('geom_type') or MultiInterface.geom_type(eltype) poly = 'holes' in data or geom == 'Polygon' if poly: single_type, multi_type = Polygon, MultiPolygon elif geom == 'Line': single_type, multi_type = LineString, MultiLineString elif geom == 'Ring': single_type, multi_type = LinearRing, MultiPolygon else: single_type, multi_type = Point, MultiPoint data['geometry'] = geom_from_dict(data, xdim.name, ydim.name, single_type, multi_type) if not cls.applies(data): raise ValueError( "GeomDictInterface only handles dictionary types " "containing a 'geometry' key and shapely geometry " "value.") unpacked = [] for d, vals in data.items(): if isinstance(d, tuple): vals = np.asarray(vals) if vals.shape == (0, ): for sd in d: unpacked.append((sd, np.array([], dtype=vals.dtype))) elif not vals.ndim == 2 and vals.shape[1] == len(d): raise ValueError("Values for %s dimensions did not have " "the expected shape.") else: for i, sd in enumerate(d): unpacked.append((sd, vals[:, i])) elif d not in dimensions: unpacked.append((d, vals)) else: if not isscalar(vals): vals = np.asarray(vals) if not vals.ndim == 1 and d in dimensions: raise ValueError( 'DictInterface expects data for each column to be flat.' ) unpacked.append((d, vals)) if not cls.expanded( [vs for d, vs in unpacked if d in dimensions and not isscalar(vs)]): raise ValueError( 'DictInterface expects data to be of uniform shape.') if isinstance(data, odict_types): data.update(unpacked) else: data = OrderedDict(unpacked) return data, {'kdims': kdims, 'vdims': vdims}, {}