def test_not_has(self): """Test !has filter function""" ff = create_filter(['!has', 'a']) passing = Feature(geometry=line_geometry, properties=dict(b=3)) failing = Feature(geometry=line_geometry, properties=dict(a=1)) self.assertTrue(ff(passing)) self.assertFalse(ff(failing))
def test_in(self): """Test in filter function""" ff = create_filter(['in', 'a', 1, 2]) passing = Feature(geometry=line_geometry, properties=dict(a=1)) failing = Feature(geometry=line_geometry, properties=dict(a=3)) self.assertTrue(ff(passing)) self.assertFalse(ff(failing))
def test_comparison(self): """Test comparison filter function""" ff = create_filter(['==', 'a', 5]) passing = Feature(geometry=line_geometry, properties=dict(a=5)) failing = Feature(geometry=line_geometry, properties=dict(a=4)) self.assertTrue(ff(passing)) self.assertFalse(ff(failing))
def test_geometry_comparison(self): """Test $type specific filters for comparison""" ff = create_filter(['==', '$type', 'Polygon']) # print(_compile(['==', '$type', 'Polygon'])) passing = Feature(geometry=polygon_geometry) failing = Feature(geometry=line_geometry) self.assertTrue(ff(passing)) self.assertFalse(ff(failing))
def test_none(self): """Test none filter function""" ff = create_filter(['none', ['==', 'a', 5], ['==', 'b', 3]]) passing = Feature(geometry=line_geometry, properties=dict(b=1)) failing1 = Feature(geometry=line_geometry, properties=dict(a=5, b=3)) failing2 = Feature(geometry=line_geometry, properties=dict(a=5)) failing3 = Feature(geometry=line_geometry, properties=dict(b=3)) self.assertTrue(ff(passing)) self.assertFalse(ff(failing1)) self.assertFalse(ff(failing2)) self.assertFalse(ff(failing3))
def _mapper(x, y, z, data, args): """Iterate over OSM QA Tiles and return a label for each tile Iterate over the .mbtiles input. Decode the provided vector tile. Depending upon the desired type of eventual machine learning training, return a label list: - For 'object-detection' tasks, each list element is a bounding box like [xmin, ymin, xmax, ymax, class_index]. There is one list element for each feature matching the provided classes. - For 'classification' tasks, the entire list is a "one-hot" vector representing which class the tile matches Parameters ------------ x, y, z: int tile indices data: str Encoded vector tile data args: dict Additional arguments passed to the tile worker Returns --------- label: tuple The first element is a tile index of the form x-y-z. The second element is a list representing the label of the tile """ ml_type = args.get('ml_type') classes = args.get('classes') if data is None: return ('{!s}-{!s}-{!s}'.format(x, y, z), _create_empty_label(ml_type, classes)) tile = mapbox_vector_tile.decode(data) # for each class, determine if any features in the tile match if tile['osm']['features']: if ml_type == 'classification': class_counts = np.zeros(len(classes) + 1, dtype=np.int) for i, cl in enumerate(classes): ff = create_filter(cl.get('filter')) class_counts[i + 1] = int( bool([f for f in tile['osm']['features'] if ff(f)])) # if there are no classes, activate the background if np.sum(class_counts) == 0: class_counts[0] = 1 return ('{!s}-{!s}-{!s}'.format(x, y, z), class_counts) elif ml_type == 'object-detection': bboxes = _create_empty_label(ml_type, classes) for feat in tile['osm']['features']: for i, cl in enumerate(classes): ff = create_filter(cl.get('filter')) if ff(feat): geo = shape(feat['geometry']) if cl.get('buffer'): geo = geo.buffer(cl.get('buffer'), 4) bb = _pixel_bbox(geo.bounds) + [i + 1] bboxes = np.append(bboxes, np.array([bb]), axis=0) return ('{!s}-{!s}-{!s}'.format(x, y, z), bboxes) elif ml_type == 'segmentation': geos = [] for feat in tile['osm']['features']: for i, cl in enumerate(classes): ff = create_filter(cl.get('filter')) if ff(feat): feat['geometry']['coordinates'] = _convert_coordinates( feat['geometry']['coordinates']) geo = shape(feat['geometry']) try: geo = geo.intersection(clip_mask) except TopologicalError as e: print(e, 'skipping') break if cl.get('buffer'): geo = geo.buffer(cl.get('buffer'), 4) if not geo.is_empty: geos.append((mapping(geo), i + 1)) result = rasterize(geos, out_shape=(256, 256)) return ('{!s}-{!s}-{!s}'.format(x, y, z), result) return ('{!s}-{!s}-{!s}'.format(x, y, z), np.array())