def test_cant_add_task_if_not_supplied_feature_type(self):
        # Arrange
        invalid_feature = geojson.MultiPolygon([[(2.38, 57.322),
                                                 (23.194, -20.28),
                                                 (-120.43, 19.15),
                                                 (2.38, 10.33)]])
        # Arrange

        with self.assertRaises(InvalidGeoJson):
            Task.from_geojson_feature(1, invalid_feature)
    def _attach_tasks_to_project(draft_project: Project, tasks_geojson):
        """
        Validates then iterates over the array of tasks and attach them to the draft project
        :param draft_project: Draft project in scope
        :param tasks_geojson: GeoJSON feature collection of mapping tasks
        :raises InvalidGeoJson, InvalidData
        """
        tasks = geojson.loads(json.dumps(tasks_geojson))

        if type(tasks) is not geojson.FeatureCollection:
            raise InvalidGeoJson(
                'Tasks: Invalid GeoJson must be FeatureCollection')

        is_valid_geojson = geojson.is_valid(tasks)
        if is_valid_geojson['valid'] == 'no':
            raise InvalidGeoJson(
                f"Tasks: Invalid FeatureCollection - {is_valid_geojson['message']}"
            )

        task_count = 1
        for feature in tasks['features']:
            try:
                task = Task.from_geojson_feature(task_count, feature)
            except (InvalidData, InvalidGeoJson) as e:
                raise e

            draft_project.tasks.append(task)
            task_count += 1

        task_count -= 1  # Remove last increment before falling out loop
        draft_project.total_tasks = task_count
    def test_task_can_generate_valid_feature_collection(self):
        if self.skip_tests:
            return

        # Act
        feature_collection = Task.get_tasks_as_geojson_feature_collection(
            self.test_project.id)

        # Assert
        self.assertIsInstance(feature_collection, geojson.FeatureCollection)
        self.assertEqual(1, len(feature_collection.features))
示例#4
0
    def create_task_poly_files(dto: ProjectFileDTO):
        """ Creates .poly file for each feature of task and extracts the data from a project file """
        geojson = Task.get_tasks_as_geojson_feature_collection(dto.project_id)
        dir = os.path.join(dto.path, os.path.splitext(dto.file_name)[0])
        if not os.path.exists(dir):
            os.makedirs(dir)
        tasks_file = os.path.join(
            dir, "{project_id}_tasks.geojson".format(
                project_id=str(dto.project_id)))

        with open(tasks_file, 'w') as t:
            t.write(str(geojson))

        poly_cmd = './server/tools/ogr2poly.py {file} -p {dir}/ -f taskId'.format(
            file=tasks_file, dir=dir)
        subprocess.call(poly_cmd, shell=True)
        os.remove(tasks_file)