Example #1
0
 def test_join_datasets_with_2_ids(self):
     from numpy import ma
     storage = StorageFactory().get_storage('dict_storage')
     
     storage.write_table(
         table_name='data1',
         table_data={
             'id1':array([2,4,2]),
             'id2':array([1,2,3]),
             'attr1':array([4,7,1]),
             'attr2':array([100,0,1000]),
             }
         )
     storage.write_table(
         table_name='data2',
         table_data={
             'id1':array([4,2,2]),
             'id2':array([2,3,1]),
             'attr1':array([50,60,70])
             }
         )
     
     ds1 = Dataset(in_storage=storage, in_table_name='data1', id_name=['id1', 'id2'], dataset_name='data1')
     ds2 = Dataset(in_storage=storage, in_table_name='data2', id_name=['id1', 'id2'], dataset_name='data2')
     ds1.join(ds2, 'attr1')
     self.assertEqual(ma.allequal(ds1.get_attribute('attr1'), array([70,50,60])), True)
     self.assertEqual(ma.allequal(ds1.get_attribute('attr2'), array([100,0,1000])), True)
 def run(self, spatial_table_name, storage_type, data_path, dataset, attribute_names, join_attribute=None, new_table_name=None, 
         files_to_copy_postfix=['shp', 'shx']):
     logger.start_block('Run SpatialTableJoin')
     storage = StorageFactory().get_storage(type=storage_type, storage_location=data_path)
     spatial_dataset = Dataset(in_storage=storage, in_table_name=spatial_table_name, dataset_name='spatial_dataset', id_name=[])
     spatial_dataset.join(dataset, name=attribute_names, join_attribute=join_attribute, metadata=AttributeType.PRIMARY)
     if new_table_name is None:
         out_table_name = spatial_table_name
     else:
         out_table_name = new_table_name
         for postfix in files_to_copy_postfix:
             file_name = os.path.join(data_path, '%s.%s' % (spatial_table_name, postfix))
             if os.path.exists(file_name):
                 new_file_name = os.path.join(data_path, '%s.%s' % (new_table_name, postfix))
                 logger.log_status('Copying %s into %s.' % (file_name, new_file_name))
                 shutil.copy(file_name, new_file_name)
     logger.log_status('New table written into %s/%s.' % (data_path, out_table_name))
     spatial_dataset.write_dataset(out_storage=storage, out_table_name=out_table_name, attributes=AttributeType.PRIMARY)
     logger.end_block()
 def run(self,
         spatial_table_name,
         storage_type,
         data_path,
         dataset,
         attribute_names,
         join_attribute=None,
         new_table_name=None,
         files_to_copy_postfix=['shp', 'shx']):
     logger.start_block('Run SpatialTableJoin')
     storage = StorageFactory().get_storage(type=storage_type,
                                            storage_location=data_path)
     spatial_dataset = Dataset(in_storage=storage,
                               in_table_name=spatial_table_name,
                               dataset_name='spatial_dataset',
                               id_name=[])
     spatial_dataset.join(dataset,
                          name=attribute_names,
                          join_attribute=join_attribute,
                          metadata=AttributeType.PRIMARY)
     if new_table_name is None:
         out_table_name = spatial_table_name
     else:
         out_table_name = new_table_name
         for postfix in files_to_copy_postfix:
             file_name = os.path.join(
                 data_path, '%s.%s' % (spatial_table_name, postfix))
             if os.path.exists(file_name):
                 new_file_name = os.path.join(
                     data_path, '%s.%s' % (new_table_name, postfix))
                 logger.log_status('Copying %s into %s.' %
                                   (file_name, new_file_name))
                 shutil.copy(file_name, new_file_name)
     logger.log_status('New table written into %s/%s.' %
                       (data_path, out_table_name))
     spatial_dataset.write_dataset(out_storage=storage,
                                   out_table_name=out_table_name,
                                   attributes=AttributeType.PRIMARY)
     logger.end_block()