Repository: azavea/raster-vision

Issue:
Rasterization Impossible for (At Least Some) `class_id_to_filter` Arguments
A setup like this:
```python
    class_id_filter_dict = {
        0: ['==', 'default', 'Background'],
        1: ['==', 'default', 'Cloud'],
    }

    label_source = SemanticSegmentationLabelSourceConfig(
        raster_source=RasterizedSourceConfig(
            vector_source=GeoJSONVectorSourceConfig(
                uri=hrefs[1],
                class_id_to_filter=class_id_filter_dict,
                default_class_id=0),
            rasterizer_config=RasterizerConfig(background_class_id=0)
        ))
```

produces this error
```
Traceback (most recent call last):                                                                                                                                                                                   
  File "/opt/conda/lib/python3.6/runpy.py", line 193, in _run_module_as_main                                                                                                                                         
    "__main__", mod_spec)                                                                                                                                                                                            
  File "/opt/conda/lib/python3.6/runpy.py", line 85, in _run_code                                                                                                                                                    
    exec(code, run_globals)                                                                                                                                                                                          
  File "/opt/src/rastervision_pipeline/rastervision/pipeline/cli.py", line 248, in <module>                                                                                                                          
    main()                                                                                                                                                                                                           
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 722, in __call__                                                                                                                                 
    return self.main(*args, **kwargs)                                                                                                                                                                                
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 697, in main                                                                                                                                     
    rv = self.invoke(ctx)                                                                                                                                                                                            
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 1066, in invoke                                                                                                                                  
    return _process_result(sub_ctx.command.invoke(sub_ctx))                                                                                                                                                          
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 895, in invoke                                                                                                                                   
    return ctx.invoke(self.callback, **ctx.params)                                                                                                                                                                   
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 535, in invoke                                                                                                                                   
    return callback(*args, **kwargs)                                                                                                                                                                                 
  File "/opt/src/rastervision_pipeline/rastervision/pipeline/cli.py", line 240, in run_command                                                                                                                       
    runner=runner)
  File "/opt/src/rastervision_pipeline/rastervision/pipeline/cli.py", line 217, in _run_command                                                                                                             [94/1935]
    command_fn()                                                                                                                                                                                                     
  File "/opt/src/rastervision_core/rastervision/core/rv_pipeline/rv_pipeline.py", line 107, in chip                                                                                                                  
    chip_scene(s.build(class_cfg, self.tmp_dir), TRAIN)                                                                                                                                                              
  File "/opt/src/rastervision_core/rastervision/core/rv_pipeline/rv_pipeline.py", line 96, in chip_scene                                                                                                             
    labels = self.get_train_labels(window, scene)                                                                                                                                                                    
  File "/opt/src/rastervision_core/rastervision/core/rv_pipeline/semantic_segmentation.py", line 112, in get_train_labels                                                                                            
    return scene.ground_truth_label_source.get_labels(window=window)                                                                                                                                                 
  File "/opt/src/rastervision_core/rastervision/core/data/label_source/semantic_segmentation_label_source.py", line 98, in get_labels                                                                                
    raw_labels = self.raster_source.get_raw_chip(window)                                                                                                                                                             
  File "/opt/src/rastervision_core/rastervision/core/data/raster_source/raster_source.py", line 101, in get_raw_chip                                                                                                 
    return self._get_chip(window)                                                                                                                                                                                    
  File "/opt/src/rastervision_core/rastervision/core/data/raster_source/rasterized_source.py", line 107, in _get_chip                                                                                                
    self.get_extent())                                                                                                                                                                                               
  File "/opt/src/rastervision_core/rastervision/core/data/raster_source/rasterized_source.py", line 44, in geoms_to_raster
    all_touched=all_touched)                                                    
  File "/opt/conda/lib/python3.6/site-packages/rasterio/env.py", line 366, in wrapper
    return f(*args, **kwds)                                                        
  File "/opt/conda/lib/python3.6/site-packages/rasterio/features.py", line 280, in rasterize                                                  
    if not validate_dtype(shape_values, valid_dtypes):                                                                                                   
  File "/opt/conda/lib/python3.6/site-packages/rasterio/dtypes.py", line 184, in validate_dtype
    get_minimum_dtype(values) in valid_dtypes)                                    
  File "/opt/conda/lib/python3.6/site-packages/rasterio/dtypes.py", line 107, in get_minimum_dtype
    min_value = values.min()                                                          
  File "/opt/conda/lib/python3.6/site-packages/numpy/core/_methods.py", line 32, in _amin
    return umr_minimum(a, axis, None, out, keepdims, initial)                                
TypeError: cannot perform reduce with flexible type
```
which is evidently due to the `class_id`s being strings rather than integers.



Discussion:

Resolving patch:
diff --git a/rastervision_core/rastervision/core/data/vector_source/class_inference.py b/rastervision_core/rastervision/core/data/vector_source/class_inference.py
--- a/rastervision_core/rastervision/core/data/vector_source/class_inference.py
+++ b/rastervision_core/rastervision/core/data/vector_source/class_inference.py
@@ -18,7 +18,8 @@ def __init__(self,
         if self.class_id_to_filter is not None:
             self.class_id_to_filter = {}
             for class_id, filter_exp in class_id_to_filter.items():
-                self.class_id_to_filter[class_id] = create_filter(filter_exp)
+                self.class_id_to_filter[int(class_id)] = create_filter(
+                    filter_exp)
 
     def infer_class_id(self, feature):
         """Infer the class_id for a GeoJSON feature.


Test patch: