def join_layers(self, layers, layerOpts, layerFeatures): """ joins features in layers """ from geometry.utils import join_features for id in layers: if layerOpts[id]['join'] is not False: unjoined = 0 join = layerOpts[id]['join'] groupBy = join['group-by'] groups = join['groups'] if not groups: # auto populate groups groups = {} for feat in layerFeatures[id]: fid = feat.props[groupBy] groups[fid] = [fid] groupAs = join['group-as'] groupFeatures = {} res = [] for feat in layerFeatures[id]: found_in_group = False for g_id in groups: if g_id not in groupFeatures: groupFeatures[g_id] = [] if feat.props[groupBy] in groups[g_id] or str( feat.props[groupBy]) in groups[g_id]: groupFeatures[g_id].append(feat) found_in_group = True break if not found_in_group: unjoined += 1 res.append(feat) #print unjoined,'features were not joined' for g_id in groups: props = {} for feat in groupFeatures[g_id]: fprops = feat.props for key in fprops: if key not in props: props[key] = fprops[key] else: if props[key] != fprops[key]: props[key] = "---" if groupAs is not False: props[groupAs] = g_id if g_id in groupFeatures: res += join_features(groupFeatures[g_id], props) layerFeatures[id] = res
def join_layers(self, layers, layerOpts, layerFeatures): """ joins features in layers """ from geometry.utils import join_features for id in layers: if layerOpts[id]['join'] is not False: unjoined = 0 join = layerOpts[id]['join'] groupBy = join['group-by'] groups = join['groups'] if not groups: # auto populate groups groups = {} for feat in layerFeatures[id]: fid = feat.props[groupBy] groups[fid] = [fid] groupAs = join['group-as'] groupFeatures = {} res = [] for feat in layerFeatures[id]: found_in_group = False for g_id in groups: if g_id not in groupFeatures: groupFeatures[g_id] = [] if feat.props[groupBy] in groups[g_id] or str(feat.props[groupBy]) in groups[g_id]: groupFeatures[g_id].append(feat) found_in_group = True break if not found_in_group: unjoined += 1 res.append(feat) #print unjoined,'features were not joined' for g_id in groups: props = {} for feat in groupFeatures[g_id]: fprops = feat.props for key in fprops: if key not in props: props[key] = fprops[key] else: if props[key] != fprops[key]: props[key] = "---" if groupAs is not False: props[groupAs] = g_id if g_id in groupFeatures: res += join_features(groupFeatures[g_id], props) layerFeatures[id] = res
def _join_features(self): """ ### Joins features within a layer. Sometimes you want to merge or join multiple features (say polygons) into a single feature. Kartograph uses the geometry.union() method of shapely to do that. """ from geometry.utils import join_features for layer in self.layers: if layer.options['join'] is not False: unjoined = 0 join = layer.options['join'] # The property we want to group the features by. groupBy = join['group-by'] groups = join['groups'] if groupBy is not False and not groups: # If no groups are defined, we'll create a group for each # unique value of the ``group-by` property. groups = {} for feat in layer.features: fid = feat.props[groupBy] groups[fid] = [fid] groupFeatures = {} # Group all features into one group if no group-by is set if groupBy is False: groupFeatures[layer.id] = [] groups = [layer.id] res = [] # Find all features for each group. for feat in layer.features: if groupBy is False: groupFeatures[layer.id].append(feat) else: found_in_group = False for g_id in groups: if g_id not in groupFeatures: groupFeatures[g_id] = [] if feat.props[groupBy] in groups[g_id] or str(feat.props[groupBy]) in groups[g_id]: groupFeatures[g_id].append(feat) found_in_group = True break if not found_in_group: unjoined += 1 res.append(feat) for g_id in groups: # Make a copy of the input features properties. props = {} for feat in groupFeatures[g_id]: fprops = feat.props for key in fprops: if key not in props: props[key] = fprops[key] else: if props[key] != fprops[key]: props[key] = "---" # If ``group-as``was set, we store the group id as # new property. groupAs = join['group-as'] if groupAs is not False: props[groupAs] = g_id # group.attributes allows you to keep or define # certain attributes for the joined features # # attributes: # FIPS_1: code # use the value of 'code' stored in one of the grouped features # NAME: # define values for each group-id # GO: Gorenjska # KO: Koroka if 'attributes' in join: attrs = join['attributes'] for key in attrs: if isinstance(attrs[key], dict): if g_id in attrs[key]: props[key] = attrs[key][g_id] else: props[key] = groupFeatures[g_id][0].props[attrs[key]] # use first value # Finally join (union) the feature geometries. if g_id in groupFeatures: if 'buffer' in join: buffer_polygons = join['buffer'] else: buffer_polygons = 0 res += join_features(groupFeatures[g_id], props, buf=buffer_polygons) # Export ids as JSON dict, if requested if join['export-ids']: exp = {} for g_id in groups: exp[g_id] = [] for feat in groupFeatures[g_id]: exp[g_id].append(feat.props[join['export-ids']]) import json print json.dumps(exp) layer.features = res
def _join_features(self): """ ### Joins features within a layer. Sometimes you want to merge or join multiple features (say polygons) into a single feature. Kartograph uses the geometry.union() method of shapely to do that. """ from geometry.utils import join_features for layer in self.layers: if layer.options['join'] is not False: unjoined = 0 join = layer.options['join'] # The property we want to group the features by. groupBy = join['group-by'] groups = join['groups'] if groupBy is not False and not groups: # If no groups are defined, we'll create a group for each # unique value of the ``group-by` property. groups = {} for feat in layer.features: fid = feat.props[groupBy] groups[fid] = [fid] groupFeatures = {} # Group all features into one group if no group-by is set if groupBy is False: groupFeatures[layer.id] = [] groups = [layer.id] res = [] # Find all features for each group. for feat in layer.features: if groupBy is False: groupFeatures[layer.id].append(feat) else: found_in_group = False for g_id in groups: if g_id not in groupFeatures: groupFeatures[g_id] = [] if feat.props[groupBy] in groups[g_id] or str(feat.props[groupBy]) in groups[g_id]: groupFeatures[g_id].append(feat) found_in_group = True break if not found_in_group: unjoined += 1 res.append(feat) for g_id in groups: # Make a copy of the input features properties. props = {} for feat in groupFeatures[g_id]: fprops = feat.props for key in fprops: if key not in props: props[key] = fprops[key] else: if props[key] != fprops[key]: props[key] = "---" # If ``group-as``was set, we store the group id as # new property. groupAs = join['group-as'] if groupAs is not False: props[groupAs] = g_id # Finally join (union) the feature geometries. if g_id in groupFeatures: if 'buffer' in join: buffer_polygons = join['buffer'] else: buffer_polygons = 0 res += join_features(groupFeatures[g_id], props, buf=buffer_polygons) layer.features = res