def __str__(self): """Render as name, number of features, geometry type """ g_type_str = geometrytype2string(self.geometry_type) return ('Vector data set: %s, %i features, geometry type ' '%s (%s)' % (self.name, len(self), str(self.geometry_type), g_type_str))
def __str__(self): """Render as name, number of features, geometry type """ g_type_str = geometrytype2string(self.geometry_type) return ('Vector data set: %s, %i features, geometry type ' '%s (%s)' % (self.name, len(self), str(self.geometry_type), g_type_str))
def interpolate_polygon_points(source, target, layer_name=None): """Interpolate from polygon vector layer to point vector data Args: * source: Vector data set (polygon) * target: Vector data set (points) * layer_name: Optional name of returned interpolated layer. If None the name of target is used for the returned layer. Output I: Vector data set; points located as target with values interpolated from source Note All attribute names from polygons are transferred to the points that are inside them. """ msg = ('Vector layer to interpolate to must be point geometry. ' 'I got OGR geometry type %s' % geometrytype2string(target.geometry_type)) verify(target.is_point_data, msg) msg = ('Name must be either a string or None. I got %s' % (str(type(target)))[1:-1]) verify(layer_name is None or isinstance(layer_name, basestring), msg) attribute_names = source.get_attribute_names() #---------------- # Start algorithm #---------------- # Extract point features points = ensure_numeric(target.get_geometry()) attributes = target.get_data() original_geometry = target.get_geometry() # Geometry for returned data # Extract polygon features geom = source.get_geometry(as_geometry_objects=True) data = source.get_data() verify(len(geom) == len(data)) # Include polygon_id as attribute attribute_names.append('polygon_id') attribute_names.append(DEFAULT_ATTRIBUTE) # Augment point features with empty attributes from polygon for a in attributes: # Create all attributes that exist in source for key in attribute_names: a[key] = None # Traverse polygons and assign attributes to points that fall inside for i, polygon in enumerate(geom): # Carry all attributes across from source poly_attr = data[i] # Assign default attribute to indicate points inside poly_attr[DEFAULT_ATTRIBUTE] = True # Clip data points by polygons and add polygon attributes indices = inside_polygon(points, polygon.outer_ring, holes=polygon.inner_rings) for k in indices: for key in poly_attr: # Assign attributes from polygon to points attributes[k][key] = poly_attr[key] attributes[k]['polygon_id'] = i # Store id for associated polygon # Create new Vector instance and return V = Vector(data=attributes, projection=target.get_projection(), geometry=original_geometry, name=layer_name) return V
def interpolate(self, X, name=None, attribute=None): """Interpolate values of this vector layer to other layer Input X: Layer object defining target name: Optional name of interpolated layer attribute: Optional attribute name to use. If None, all attributes are used. FIXME (Ole): Single attribute not tested well yet and not implemented for lines Output Y: Layer object with values of this vector layer interpolated to geometry of input layer X """ msg = 'Input to Vector.interpolate must be a vector layer instance' verify(X.is_vector, msg) X_projection = X.get_projection() S_projection = self.get_projection() msg = ('Projections must be the same: I got %s and %s' % (S_projection, X_projection)) verify(S_projection == X_projection, msg) msg = ('Vector layer to interpolate from must be polygon geometry. ' 'I got OGR geometry type %s' % geometrytype2string(self.geometry_type)) verify(self.is_polygon_data, msg) # FIXME (Ole): Maybe organise this the same way it is done with rasters # FIXME (Ole): Retain original geometry to use with returned data here if X.is_polygon_data: # Use centroids, in case of polygons X = convert_polygons_to_centroids(X) elif X.is_line_data: # Clip lines to polygon and return centroids # FIXME (Ole): Need to separate this out, but identify what is # common with points and lines # #X.write_to_file('line_data.shp') #self.write_to_file('poly_data.shp') # Extract line features lines = X.get_geometry() line_attributes = X.get_data() N = len(X) verify(len(lines) == N) verify(len(line_attributes) == N) # Extract polygon features polygons = self.get_geometry() poly_attributes = self.get_data() verify(len(polygons) == len(poly_attributes)) # Data structure for resulting line segments clipped_geometry = [] clipped_attributes = [] # Clip line lines to polygons for i, polygon in enumerate(polygons): for j, line in enumerate(lines): inside, outside = clip_line_by_polygon(line, polygon) # Create new attributes # FIXME (Ole): Not done single specified polygon # attribute inside_attributes = {} outside_attributes = {} for key in line_attributes[j]: inside_attributes[key] = line_attributes[j][key] outside_attributes[key] = line_attributes[j][key] for key in poly_attributes[i]: inside_attributes[key] = poly_attributes[i][key] outside_attributes[key] = None # Always create default attribute flagging if segment was # inside any of the polygons inside_attributes[DEFAULT_ATTRIBUTE] = True outside_attributes[DEFAULT_ATTRIBUTE] = False # Assign new attribute set to clipped lines for segment in inside: clipped_geometry.append(segment) clipped_attributes.append(inside_attributes) for segment in outside: clipped_geometry.append(segment) clipped_attributes.append(outside_attributes) # Create new Vector instance and return V = Vector(data=clipped_attributes, projection=X.get_projection(), geometry=clipped_geometry, geometry_type='line') #V.write_to_file('clipped_and_tagged.shp') return V # The following applies only to Polygon-Point interpolation msg = ('Vector layer to interpolate to must be point geometry. ' 'I got OGR geometry type %s' % geometrytype2string(X.geometry_type)) verify(X.is_point_data, msg) msg = ('Name must be either a string or None. I got %s' % (str(type(X)))[1:-1]) verify(name is None or isinstance(name, basestring), msg) msg = ('Attribute must be either a string or None. I got %s' % (str(type(X)))[1:-1]) verify(attribute is None or isinstance(attribute, basestring), msg) attribute_names = self.get_attribute_names() if attribute is not None: msg = ('Requested attribute "%s" did not exist in %s' % (attribute, attribute_names)) verify(attribute in attribute_names, msg) #---------------- # Start algorithm #---------------- # Extract point features points = ensure_numeric(X.get_geometry()) attributes = X.get_data() N = len(X) # Extract polygon features geom = self.get_geometry() data = self.get_data() verify(len(geom) == len(data)) # Augment point features with empty attributes from polygon for a in attributes: if attribute is None: # Use all attributes for key in attribute_names: a[key] = None else: # Use only requested attribute # FIXME (Ole): Test for this is not finished a[attribute] = None # Always create default attribute flagging if point was # inside any of the polygons a[DEFAULT_ATTRIBUTE] = None # Traverse polygons and assign attributes to points that fall inside for i, polygon in enumerate(geom): if attribute is None: # Use all attributes poly_attr = data[i] else: # Use only requested attribute poly_attr = {attribute: data[i][attribute]} # Assign default attribute to indicate points inside poly_attr[DEFAULT_ATTRIBUTE] = True # Clip data points by polygons and add polygon attributes indices = inside_polygon(points, polygon) for k in indices: for key in poly_attr: # Assign attributes from polygon to points attributes[k][key] = poly_attr[key] # Create new Vector instance and return V = Vector(data=attributes, projection=X.get_projection(), geometry=X.get_geometry()) return V
def interpolate_polygon_points(source, target, layer_name=None): """Interpolate from polygon vector layer to point vector data Args: * source: Vector data set (polygon) * target: Vector data set (points) * layer_name: Optional name of returned interpolated layer. If None the name of target is used for the returned layer. Output I: Vector data set; points located as target with values interpolated from source Note All attribute names from polygons are transferred to the points that are inside them. """ msg = ('Vector layer to interpolate to must be point geometry. ' 'I got OGR geometry type %s' % geometrytype2string(target.geometry_type)) verify(target.is_point_data, msg) msg = ('Name must be either a string or None. I got %s' % (str(type(target)))[1:-1]) verify(layer_name is None or isinstance(layer_name, basestring), msg) attribute_names = source.get_attribute_names() #---------------- # Start algorithm #---------------- # Extract point features points = ensure_numeric(target.get_geometry()) attributes = target.get_data() original_geometry = target.get_geometry() # Geometry for returned data # Extract polygon features geom = source.get_geometry(as_geometry_objects=True) data = source.get_data() verify(len(geom) == len(data)) # Include polygon_id as attribute attribute_names.append('polygon_id') attribute_names.append(DEFAULT_ATTRIBUTE) # Augment point features with empty attributes from polygon for a in attributes: # Create all attributes that exist in source for key in attribute_names: a[key] = None # Traverse polygons and assign attributes to points that fall inside for i, polygon in enumerate(geom): # Carry all attributes across from source poly_attr = data[i] # Assign default attribute to indicate points inside poly_attr[DEFAULT_ATTRIBUTE] = True # Clip data points by polygons and add polygon attributes indices = inside_polygon(points, polygon.outer_ring, holes=polygon.inner_rings) for k in indices: for key in poly_attr: # Assign attributes from polygon to points attributes[k][key] = poly_attr[key] attributes[k]['polygon_id'] = i # Store id for associated polygon # Create new Vector instance and return V = Vector(data=attributes, projection=target.get_projection(), geometry=original_geometry, name=layer_name) return V
def interpolate_polygon_points(source, target, layer_name=None, attribute_name=None): """Interpolate from polygon vector layer to point vector data Args: * source: Vector data set (polygon) * target: Vector data set (points) * layer_name: Optional name of returned interpolated layer. If None the name of target is used for the returned layer. * attribute_name: Name for new attribute. If None (default) the name of source is used Output I: Vector data set; points located as target with values interpolated from source """ msg = ('Vector layer to interpolate to must be point geometry. ' 'I got OGR geometry type %s' % geometrytype2string(target.geometry_type)) verify(target.is_point_data, msg) msg = ('Name must be either a string or None. I got %s' % (str(type(target)))[1:-1]) verify(layer_name is None or isinstance(layer_name, basestring), msg) msg = ('Attribute must be either a string or None. I got %s' % (str(type(target)))[1:-1]) verify(attribute_name is None or isinstance(attribute_name, basestring), msg) attribute_names = source.get_attribute_names() if attribute_name is not None: msg = ('Requested attribute "%s" did not exist in %s' % (attribute_name, attribute_names)) verify(attribute_name in attribute_names, msg) #---------------- # Start algorithm #---------------- # Extract point features points = ensure_numeric(target.get_geometry()) attributes = target.get_data() original_geometry = target.get_geometry() # Geometry for returned data # Extract polygon features geom = source.get_geometry() data = source.get_data() verify(len(geom) == len(data)) # Augment point features with empty attributes from polygon for a in attributes: if attribute_name is None: # Use all attributes for key in attribute_names: a[key] = None else: # Use only requested attribute # FIXME (Ole): Test for this is not finished a[attribute_name] = None # Always create default attribute flagging if point was # inside any of the polygons a[DEFAULT_ATTRIBUTE] = None # Traverse polygons and assign attributes to points that fall inside for i, polygon in enumerate(geom): if attribute_name is None: # Use all attributes poly_attr = data[i] else: # Use only requested attribute poly_attr = {attribute_name: data[i][attribute_name]} # Assign default attribute to indicate points inside poly_attr[DEFAULT_ATTRIBUTE] = True # Clip data points by polygons and add polygon attributes indices = inside_polygon(points, polygon) for k in indices: for key in poly_attr: # Assign attributes from polygon to points attributes[k][key] = poly_attr[key] # Create new Vector instance and return V = Vector(data=attributes, projection=target.get_projection(), geometry=original_geometry, name=layer_name) return V
def interpolate(self, X, name=None, attribute=None): """Interpolate values of this vector layer to other layer Input X: Layer object defining target name: Optional name of interpolated layer attribute: Optional attribute name to use. If None, all attributes are used. FIXME (Ole): Single attribute not tested well yet and not implemented for lines Output Y: Layer object with values of this vector layer interpolated to geometry of input layer X """ msg = 'Input to Vector.interpolate must be a vector layer instance' verify(X.is_vector, msg) X_projection = X.get_projection() S_projection = self.get_projection() msg = ('Projections must be the same: I got %s and %s' % (S_projection, X_projection)) verify(S_projection == X_projection, msg) msg = ('Vector layer to interpolate from must be polygon geometry. ' 'I got OGR geometry type %s' % geometrytype2string(self.geometry_type)) verify(self.is_polygon_data, msg) # FIXME (Ole): Maybe organise this the same way it is done with rasters # FIXME (Ole): Retain original geometry to use with returned data here if X.is_polygon_data: # Use centroids, in case of polygons X = convert_polygons_to_centroids(X) elif X.is_line_data: # Clip lines to polygon and return centroids # FIXME (Ole): Need to separate this out, but identify what is # common with points and lines # #X.write_to_file('line_data.shp') #self.write_to_file('poly_data.shp') # Extract line features lines = X.get_geometry() line_attributes = X.get_data() N = len(X) verify(len(lines) == N) verify(len(line_attributes) == N) # Extract polygon features polygons = self.get_geometry() poly_attributes = self.get_data() verify(len(polygons) == len(poly_attributes)) # Data structure for resulting line segments clipped_geometry = [] clipped_attributes = [] # Clip line lines to polygons for i, polygon in enumerate(polygons): for j, line in enumerate(lines): inside, outside = clip_line_by_polygon(line, polygon) # Create new attributes # FIXME (Ole): Not done single specified polygon # attribute inside_attributes = {} outside_attributes = {} for key in line_attributes[j]: inside_attributes[key] = line_attributes[j][key] outside_attributes[key] = line_attributes[j][key] for key in poly_attributes[i]: inside_attributes[key] = poly_attributes[i][key] outside_attributes[key] = None # Always create default attribute flagging if segment was # inside any of the polygons inside_attributes[DEFAULT_ATTRIBUTE] = True outside_attributes[DEFAULT_ATTRIBUTE] = False # Assign new attribute set to clipped lines for segment in inside: clipped_geometry.append(segment) clipped_attributes.append(inside_attributes) for segment in outside: clipped_geometry.append(segment) clipped_attributes.append(outside_attributes) # Create new Vector instance and return V = Vector(data=clipped_attributes, projection=X.get_projection(), geometry=clipped_geometry, geometry_type='line') #V.write_to_file('clipped_and_tagged.shp') return V # The following applies only to Polygon-Point interpolation msg = ('Vector layer to interpolate to must be point geometry. ' 'I got OGR geometry type %s' % geometrytype2string(X.geometry_type)) verify(X.is_point_data, msg) msg = ('Name must be either a string or None. I got %s' % (str(type(X)))[1:-1]) verify(name is None or isinstance(name, basestring), msg) msg = ('Attribute must be either a string or None. I got %s' % (str(type(X)))[1:-1]) verify(attribute is None or isinstance(attribute, basestring), msg) attribute_names = self.get_attribute_names() if attribute is not None: msg = ('Requested attribute "%s" did not exist in %s' % (attribute, attribute_names)) verify(attribute in attribute_names, msg) #---------------- # Start algorithm #---------------- # Extract point features points = ensure_numeric(X.get_geometry()) attributes = X.get_data() N = len(X) # Extract polygon features geom = self.get_geometry() data = self.get_data() verify(len(geom) == len(data)) # Augment point features with empty attributes from polygon for a in attributes: if attribute is None: # Use all attributes for key in attribute_names: a[key] = None else: # Use only requested attribute # FIXME (Ole): Test for this is not finished a[attribute] = None # Always create default attribute flagging if point was # inside any of the polygons a[DEFAULT_ATTRIBUTE] = None # Traverse polygons and assign attributes to points that fall inside for i, polygon in enumerate(geom): if attribute is None: # Use all attributes poly_attr = data[i] else: # Use only requested attribute poly_attr = {attribute: data[i][attribute]} # Assign default attribute to indicate points inside poly_attr[DEFAULT_ATTRIBUTE] = True # Clip data points by polygons and add polygon attributes indices = inside_polygon(points, polygon) for k in indices: for key in poly_attr: # Assign attributes from polygon to points attributes[k][key] = poly_attr[key] # Create new Vector instance and return V = Vector(data=attributes, projection=X.get_projection(), geometry=X.get_geometry()) return V
def interpolate_polygon_points(source, target, layer_name=None, attribute_name=None): """Interpolate from polygon vector layer to point vector data Args: * source: Vector data set (polygon) * target: Vector data set (points) * layer_name: Optional name of returned interpolated layer. If None the name of target is used for the returned layer. * attribute_name: Name for new attribute. If None (default) the name of source is used Output I: Vector data set; points located as target with values interpolated from source """ msg = ('Vector layer to interpolate to must be point geometry. ' 'I got OGR geometry type %s' % geometrytype2string(target.geometry_type)) verify(target.is_point_data, msg) msg = ('Name must be either a string or None. I got %s' % (str(type(target)))[1:-1]) verify(layer_name is None or isinstance(layer_name, basestring), msg) msg = ('Attribute must be either a string or None. I got %s' % (str(type(target)))[1:-1]) verify(attribute_name is None or isinstance(attribute_name, basestring), msg) attribute_names = source.get_attribute_names() if attribute_name is not None: msg = ('Requested attribute "%s" did not exist in %s' % (attribute_name, attribute_names)) verify(attribute_name in attribute_names, msg) #---------------- # Start algorithm #---------------- # Extract point features points = ensure_numeric(target.get_geometry()) attributes = target.get_data() original_geometry = target.get_geometry() # Geometry for returned data # Extract polygon features geom = source.get_geometry() data = source.get_data() verify(len(geom) == len(data)) # Augment point features with empty attributes from polygon for a in attributes: if attribute_name is None: # Use all attributes for key in attribute_names: a[key] = None else: # Use only requested attribute # FIXME (Ole): Test for this is not finished a[attribute_name] = None # Always create default attribute flagging if point was # inside any of the polygons a[DEFAULT_ATTRIBUTE] = None # Traverse polygons and assign attributes to points that fall inside for i, polygon in enumerate(geom): if attribute_name is None: # Use all attributes poly_attr = data[i] else: # Use only requested attribute poly_attr = {attribute_name: data[i][attribute_name]} # Assign default attribute to indicate points inside poly_attr[DEFAULT_ATTRIBUTE] = True # Clip data points by polygons and add polygon attributes indices = inside_polygon(points, polygon) for k in indices: for key in poly_attr: # Assign attributes from polygon to points attributes[k][key] = poly_attr[key] # Create new Vector instance and return V = Vector(data=attributes, projection=target.get_projection(), geometry=original_geometry, name=layer_name) return V