def get_features(self, filter=None, bbox=None, ignore_holes=False, charset='utf-8', min_area=0): # Eventually we convert the bbox list into a proper BBox instance if bbox is not None and not isinstance(bbox, BBox): bbox = BBox(bbox[2] - bbox[0], bbox[3] - bbox[1], bbox[0], bbox[1]) mode = self.mode if mode in ('line', 'polygon'): coords = [] features = [] for row in self.cr: attrs = dict() for i in range(len(row)): key = self.header[i] if key == self.xfield: x = float(row[i]) elif key == self.yfield: y = float(row[i]) else: attrs[key] = row[i] if self.proj is not None: # inverse project coord x, y = self.proj(x, y, inverse=True) if mode == 'points': features.append(create_feature(Point(x, y), attrs)) else: coords.append((x, y)) if mode == 'line': features.append(create_feature(LineString(coords), dict())) elif mode == 'polygon': features.append(create_feature(Polygon(coords), dict())) return features
def get_features(self, attr=None, filter=None, bbox=None, ignore_holes=False, min_area=False, charset='utf-8'): """ ### Get features """ res = [] # We will try these encodings.. known_encodings = ['utf-8', 'latin-1', 'iso-8859-2', 'iso-8859-15'] try_encodings = [charset] for enc in known_encodings: if enc != charset: try_encodings.append(enc) # Eventually we convert the bbox list into a proper BBox instance if bbox is not None and not isinstance(bbox, BBox): bbox = BBox(bbox[2] - bbox[0], bbox[3] - bbox[1], bbox[0], bbox[1]) ignored = 0 for i in range(0, len(self.recs)): # Read all record attributes drec = {} for j in range(len(self.attributes)): drec[self.attributes[j]] = self.recs[i][j] # For each record that is not filtered.. if filter is None or filter(drec): props = {} # ..we try to decode the attributes (shapefile charsets are arbitrary) for j in range(len(self.attributes)): val = self.recs[i][j] decoded = False if isinstance(val, str): for enc in try_encodings: try: val = val.decode(enc) decoded = True break except: if verbose: print 'warning: could not decode "%s" to %s' % (val, enc) if not decoded: raise KartographError('having problems to decode the input data "%s"' % val) if isinstance(val, (str, unicode)): val = val.strip() props[self.attributes[j]] = val # Read the shape from the shapefile (can take some time..).. shp = self.get_shape(i) # ..and convert the raw shape into a shapely.geometry geom = shape2geometry(shp, ignore_holes=ignore_holes, min_area=min_area, bbox=bbox, proj=self.proj) if geom is None: ignored += 1 self.forget_shape(i) continue # Finally we construct the map feature and append it to the # result list feature = create_feature(geom, props) res.append(feature) if bbox is not None and ignored > 0 and verbose: print "-ignoring %d shapes (not in bounds %s )" % (ignored, bbox) return res
def get_features(self, filter=None, bbox=None, verbose=False, ignore_holes=False, min_area=False, charset='utf-8'): """ ### Get features """ # build query query = self.query if query == '': query = 'true' if bbox: # Check for intersection with bounding box bbox_coords = (bbox[0], bbox[2], bbox[1], bbox[2], bbox[1], bbox[3], bbox[0], bbox[3], bbox[0], bbox[2]) bbox_poly = 'POLYGON((%f %f, %f %f, %f %f, %f %f, %f %f))' % bbox_coords query = "(%s) AND ST_Intersects( %s, ST_SetSRID(ST_GeomFromEWKT('%s'), 4326) )" % (query, self.geom_col, bbox_poly) # print "reading from postgis database / " + self.query # Open database connection cur = self.conn.cursor() fields = self.fields # Create a store for properties features = [] # Query features cur.execute('SELECT "%s" FROM %s WHERE %s' % ('", "'.join(fields), self.table, query)) for rec in cur: # Populate property dictionary meta = {} geom_wkb = None geom = None for f in range(len(fields)): if fields[f] != self.geom_col: # but ignore null values if rec[f]: if isinstance(rec[f], (str, unicode)): try: meta[fields[f]] = rec[f].decode('utf-8') except: print('decoding error', fields[f], rec[f]) meta[fields[f]] = '--decoding error--' else: meta[fields[f]] = rec[f] else: # Store geometry geom_wkb = rec[f] if filter is None or filter(meta): # construct geometry geom = shapely.wkb.loads(geom_wkb.decode('hex')) # Finally we construct the map feature and append it to the # result list features.append(create_feature(geom, meta)) return features
def get_features(self, filter=None, bbox=None, verbose=False, ignore_holes=False, min_area=False, charset='utf-8', bounding=False): """ ### Get features """ # build query query = self.query if query == '': query = 'true' if bbox: # Check for intersection with bounding box bbox_coords = (bbox[0], bbox[2], bbox[1], bbox[2], bbox[1], bbox[3], bbox[0], bbox[3], bbox[0], bbox[2]) bbox_poly = 'POLYGON((%f %f, %f %f, %f %f, %f %f, %f %f))' % bbox_coords query = "(%s) AND ST_Intersects( %s, ST_SetSRID(ST_GeomFromEWKT('%s'), 4326) )" % ( query, self.geom_col, bbox_poly) # print "reading from postgis database / " + self.query # Open database connection cur = self.conn.cursor() fields = self.fields # Create a store for properties features = [] # Query features cur.execute('SELECT "%s" FROM %s WHERE %s' % ('", "'.join(fields), self.table, query)) for rec in cur: # Populate property dictionary meta = {} geom_wkb = None geom = None for f in range(len(fields)): if fields[f] != self.geom_col: # but ignore null values if rec[f]: if isinstance(rec[f], (str, unicode)): try: meta[fields[f]] = rec[f].decode('utf-8') except: print 'decoding error', fields[f], rec[f] meta[fields[f]] = '--decoding error--' else: meta[fields[f]] = rec[f] else: # Store geometry geom_wkb = rec[f] if filter is None or filter(meta): # construct geometry geom = shapely.wkb.loads(geom_wkb.decode('hex')) # Finally we construct the map feature and append it to the # result list features.append(create_feature(geom, meta)) return features
def get_main_feat(self, attr=None, main_filter=None, bbox=None, ignore_holes=False, min_area=False, charset='utf-8', bounding=False): """ ### Get features """ result = None # We will try these encodings.. known_encodings = ['utf-8', 'latin-1', 'iso-8859-2', 'iso-8859-15'] try_encodings = [charset] for enc in known_encodings: if enc != charset: try_encodings.append(enc) # Eventually we convert the bbox list into a proper BBox instance ignored = 0 # Read all record attributes drec = {} for i in range(0, len(self.recs)): for j in range(len(self.attributes)): drec[self.attributes[j]] = self.recs[i][j] # For each record that is not filtered.. is_nameless = True the_feat_name = '' if 'NAME' in drec: the_feat_name = drec['NAME'] elif 'FULLNAME' in drec: the_feat_name = drec['FULLNAME'] if len(the_feat_name.strip()) > 0: is_nameless = False if main_filter is None or main_filter(drec): sq_miles_water = drec['AWATER'] / (640 * 4046.86) # if sq_miles_water>=1: # print 'Name: {0}\t{1:.2f} sq miles'.format(the_feat_name, sq_miles_water) props = {} # ..we try to decode the attributes (shapefile charsets are arbitrary) for j in range(len(self.attributes)): val = self.recs[i][j] decoded = False if isinstance(val, str): for enc in try_encodings: try: val = val.decode(enc) decoded = True break except: if verbose: print 'warning: could not decode "%s" to %s' % ( val, enc) if not decoded: raise KartographError( 'having problems to decode the input data "%s"' % val) if isinstance(val, (str, unicode)): val = val.strip() props[self.attributes[j]] = val # Read the shape from the shapefile (can take some time..).. shp = self.get_shape(i) shp.bounding = bounding shp.the_feat_name = the_feat_name geom = shape2geometry(shp, ignore_holes=ignore_holes, min_area=min_area, bbox=bbox, proj=self.proj) feat = create_feature(geom, props) # ..and return the geom of the place we wanted return feat #self.proj=None #print 'res={0}'.format(res) raise KartographError('having problems with main feature') return None
def get_features(self, attr=None, filter=None, bbox=None, ignore_holes=False, min_area=False, charset='utf-8', bounding=False, bounding_geom=None, contained_geom=None): """ ### Get features """ res = [] max_intersect = 0 #if contained_geom is None: # print '\t\tcontained_geom is None' #if bounding_geom is None: # print '\t\tbounding_geom is None' # We will try these encodings.. known_encodings = ['utf-8', 'latin-1', 'iso-8859-2', 'iso-8859-15'] try_encodings = [charset] for enc in known_encodings: if enc != charset: try_encodings.append(enc) # Eventually we convert the bbox list into a proper BBox instance if bbox is not None and not isinstance(bbox, BBox): bbox = BBox(bbox[2] - bbox[0], bbox[3] - bbox[1], bbox[0], bbox[1]) ignored = 0 #print 'len(self.recs)={0}'.format(len(self.recs)) for i in range(0, len(self.recs)): # Read all record attributes drec = {} for j in range(len(self.attributes)): drec[self.attributes[j]] = self.recs[i][j] # For each record that is not filtered.. is_nameless = True the_feat_name = '' # print drec if 'NAME' in drec: the_feat_name = drec['NAME'] elif 'FULLNAME' in drec: the_feat_name = drec['FULLNAME'] if len(the_feat_name.strip()) > 0: is_nameless = False desired_geom = False drec['DESIRED_GEOM'] = False if bounding_geom is not None: # Check if we want it to intersect shp = self.get_shape(i) shp.bounding = bounding shp.the_feat_name = the_feat_name geom = self.get_geom(i, ignore_holes=ignore_holes, min_area=min_area, bbox=bbox, proj=self.proj) #(shape2geometry(shp, ignore_holes=ignore_holes, min_area=min_area, bbox=bbox, proj=self.proj) if geom is None: ignored += 1 continue intersect_geom = bounding_geom.intersection(geom) # if intersect_geom.area>0: # print 'intersect_geom.area={0}'.format(intersect_geom.area) if intersect_geom.area >= self.intersect_tol * geom.area: desired_geom = True # print 'Found intersecting feature {0}'.format(the_feat_name) # Check for sufficient intersection to add places automatically drec['DESIRED_GEOM'] = desired_geom if filter is None or filter(drec): #if contained_geom is not None: # print '\tIn for the_feat_name {0}'.format(drec['NAME']) props = {} # ..we try to decode the attributes (shapefile charsets are arbitrary) for j in range(len(self.attributes)): val = self.recs[i][j] decoded = False if isinstance(val, str): for enc in try_encodings: try: val = val.decode(enc) decoded = True break except: if verbose: print 'warning: could not decode "%s" to %s' % ( val, enc) if not decoded: raise KartographError( 'having problems to decode the input data "%s"' % val) if isinstance(val, (str, unicode)): val = val.strip() props[self.attributes[j]] = val if bounding_geom is not None: # print 'type(bounding_geom)={0}'.format(type(bounding_geom)) x = bounding_geom.intersection(geom) if x.area < self.intersect_tol * geom.area: #print 'Name: {0} does not intersect'.format(shp.the_feat_name) ignored += 1 self.forget_shape(i) continue else: ignored += 0 # print 'Name: {0} intersects'.format(shp.the_feat_name) else: # If we didn't already set the shape and geom above, we set it here instead shp = self.get_shape(i) shp.bounding = bounding shp.the_feat_name = the_feat_name # ..and convert the raw shape into a shapely.geometry geom = self.get_geom(i, ignore_holes=ignore_holes, min_area=min_area, bbox=bbox, proj=self.proj) #shape2geometry(shp, ignore_holes=ignore_holes, min_area=min_area, bbox=bbox, proj=self.proj) if geom is None: ignored += 1 continue if contained_geom is not None: # Add a circle if no good at the end after we get all the good features #print 'Checking county {0}'.format(drec['NAME']) # Find if it's the most intersecting of the geometries with # contained_geom (which should really be contained_geom but haven't # changed yet) curr_intersect = contained_geom.intersection(geom) if curr_intersect.area == 0: # print '\tfail: curr_intersect.area={0}'.format(curr_intersect.area) ignored += 1 self.forget_shape(i) #continue else: # Set this to be the new intersection level #print '\tNew largest area intersection, area={0}'.format(curr_intersect.area) max_intersect = curr_intersect.area feature = create_feature(geom, props) res.append(feature) #continue else: #print 'Constructing feature {0}'.format(drec['NAME']) feature = create_feature(geom, props) self.feature = feature res.append(feature) if bbox is not None and ignored > 0 and verbose: print "-ignoring %d shapes (not in bounds %s )" % (ignored, bbox) #self.proj=None # print 'res={0}'.format(res) # Add a feature consisting of a circle around the contained_geom if it's too small # if contained_geom is not None and bounding_geom is None: # highlight_circ=self.get_highlight_circle(res, contained_geom) # if highlight_circ is not None: # #print('\tAdding a highlight_circ') # # Create and append feature # curr_props={'STATEFP':'00', 'COUNTYFP': '000', 'NAME': 'HighlightThePlace'} # feature=create_feature(highlight_circ, curr_props) # res.append(feature) return res