示例#1
0
    def setUp(self):

        GDB_Table = self.params["gdb_table"]
        SQL_Table = self.params["sql_table"]
        weav_atts = self.params["weaver_attributes"]
        bldg_atts = self.params["building_attributes"]
        bldgs = self.versioned_buildings
        result = Tool.compare_tables(sql_table=SQL_Table, gdb_table=GDB_Table)
        folioIds = result["folioIds"]
        # folioIds, bldgs, rel_table, bldg_atts, weav_atts, version_sde, editor
        self.updater = Updater(folioIds=folioIds, bldgs=bldgs, rel_table=GDB_Table, bldg_atts=bldg_atts,
                               weav_atts=weav_atts, version_sde=self.version_sde, editor=self.edit)
    def setUp(self):

        GDB_Table = self.params["gdb_table"]
        SQL_Table = self.params["sql_table"]
        table_atts = self.params["table_attributes"]
        bldg_atts = self.params["building_attributes"]
        comb_atts = self.params["combination_attributes"]
        domains = self.params["domains"]
        bldgs = self.versioned_buildings
        result = Tool.compare_tables(sql_table=SQL_Table, gdb_table=GDB_Table)
        folioIds = result["folioIds"]
        # folioIds, bldgs, rel_table, bldg_atts, weav_atts, version_sde, editor
        self.updater = Updater(domains=domains, folioIds=folioIds, bldgs=bldgs, rel_table=GDB_Table, bldg_atts=bldg_atts,
                               table_atts=table_atts, combination_atts=comb_atts, version_sde=self.version_sde, editor=self.edit)
class TestBuildingsUpdater(TestCase):
    @classmethod
    def setUpClass(cls):
        tool = PythonTool()
        parameters = tool.getParameterInfo()
        params = tool.processParameters(parameters=parameters)
        cls.params = params

        buildings_name = params["buildings_name"]
        out_f = params["connection_folder"]
        out_n = params["edit_connection_name"]
        plat = params["platform"]
        inst = params["instance"]
        opt = params["opt"]
        edit_version_name = params["edit_version_name"]

        #  out_folder, out_name, platform, instance, options

        cls.sde_file = params["gis_gdb"]
        # opt, out_folder, uid, platform, instance, target_sde, new_name, parent_version
        cls.manager = Manager(opt=opt, connection_folder=out_f, target_sde=cls.sde_file, new_version=edit_version_name,
                              new_connection=out_n, platform=plat, instance=inst)
        cls.manager.clean_previous()
        cls.version_sde = cls.manager.connect_version()
        env.workspace = cls.version_sde
        cls.versioned_buildings = tool.get_versioned_fc(env.workspace, buildings_name)
        cls.edit = da.Editor(cls.version_sde)
        cls.edit.startEditing()

    def setUp(self):

        GDB_Table = self.params["gdb_table"]
        SQL_Table = self.params["sql_table"]
        table_atts = self.params["table_attributes"]
        bldg_atts = self.params["building_attributes"]
        comb_atts = self.params["combination_attributes"]
        domains = self.params["domains"]
        bldgs = self.versioned_buildings
        result = Tool.compare_tables(sql_table=SQL_Table, gdb_table=GDB_Table)
        folioIds = result["folioIds"]
        # folioIds, bldgs, rel_table, bldg_atts, weav_atts, version_sde, editor
        self.updater = Updater(domains=domains, folioIds=folioIds, bldgs=bldgs, rel_table=GDB_Table, bldg_atts=bldg_atts,
                               table_atts=table_atts, combination_atts=comb_atts, version_sde=self.version_sde, editor=self.edit)

    def tearDown(self):
        self.updater = None

    @classmethod
    def tearDownClass(cls):
        cls.edit.stopEditing(False)
        del cls.edit
        env.workspace = cls.sde_file
        cls.manager.clean_previous()
        for x in [cls.version_sde]:
            try:
                os.remove(x)
            except:
                pass

    def test_build_folio_dict(self):
        folios = self.updater.build_folio_dict()
        self.assertGreaterEqual(len(folios.keys()), 0)

    def test_update_buildings(self):
        result = self.updater.update_buildings()
        self.assertTrue(result)
示例#4
0
    def execute(self, parameters, messages):
        """The method calls classes defined in external files."""
        arcpy.AddMessage("WeaverGDBUpdate.execute()")
        params = self.process_parameters(parameters=parameters)
        connection_folder = params["connection_folder"]
        platform = params["platform"]
        instance = params["instance"]
        sde_file = params["gis_gdb"]
        table_db = params["table_db"]
        bldgs = params["bldgs"]
        sql_table = params["sql_table"]
        gdb_table = params["gdb_table"]
        gdb_table_name = params["gdb_table_name"]
        buildings_name = params["buildings_name"]
        edit_connection_name = params["edit_connection_name"]
        edit_version_name = params["edit_version_name"]
        building_attributes = params["building_attributes"]
        weaver_attributes = params["weaver_attributes"]
        opt = params["opt"]

        try:

            # These values need to be removed when the user parameters are created
            result = UpdateNoiseMitSDE.compare_tables(sql_table=sql_table, gdb_table=gdb_table)

            compare_result = result["compare_result"]
            folioIds = result["folioIds"]
            match_fields = result["match_fields"]
            add_rows = result["add_rows"]
            exist_rows = result["exist_rows"]

            # compare result if True means that changes need to be made to the GDB Table and thus the Buildings
            if compare_result:
                arcpy.AddMessage({"# rows to add": len(add_rows),
                                  "# rows to remove": len(exist_rows)})
                # create VersionManager class object to create new version, connect to it,
                # and create an sde connection file, set as current workspace

                version_manager = VersionManager(opt, connection_folder, sde_file, edit_version_name, edit_connection_name, platform, instance)
                version_manager.clean_previous()
                version_sde_file = version_manager.connect_version()

                if os.path.exists(version_sde_file):
                    arcpy.AddMessage(version_sde_file)
                else:
                    raise Exception("version_sde_file not created")

                editor = da.Editor(version_sde_file)
                editor.startEditing()
                env.workspace = version_sde_file
                gdb_table = arcpy.ListTables("*{}*".format(gdb_table_name))[0]
                if arcpy.Exists(gdb_table):
                    # create GDBTableUpdater class object
                    weaver_updater = GDBTableUpdater(weaver_attributes, folioIds, match_fields, gdb_table, add_rows, exist_rows,
                                                     version_sde_file, editor)

                    # should return True when editing is complete
                    table_updated = weaver_updater.perform_update()

                    # create BuildingUpdater class object
                    version_buildings = self.get_versioned_fc(version_sde_file, buildings_name)
                    if arcpy.Exists(version_buildings):
                        try:
                            building_updater = BuildingsUpdater(folioIds, version_buildings, gdb_table, building_attributes,
                                                                weaver_attributes, version_sde_file, editor)

                            # should return True when editing it complete
                            buildings_updated = building_updater.update_buildings()

                            editor.stopEditing(True)
                            del editor

                            try:
                                version_manager.rec_post()
                            except Exception as e:
                                arcpy.AddError("Exception occurred during the rec/post operation, " +
                                "the edits were saved in the version however the version will be removed without the " +
                                "edits having been posted to the default version :: {} :: {}".format(e.message,
                                                                                                     traceback.print_exc()))

                        except Exception as e:
                            editor.stopEditing(False)
                            del editor
                            arcpy.AddError("Exception occured during buildings updates, edits have not been saved :: {}"\
                                           ":: {}".format(e.message, traceback.print_exc()))
                    else:
                        editor.stopEditing(False)
                        del editor
                        arcpy.AddError("Unable to determine the buildings feature class\
                                       using the version connection")
                else:
                    editor.stopEditing(False)
                    del editor
                    arcpy.AddError("Unable to determine the gdb table\
                                    using the version connection")

                version_manager.clean_previous()
                del version_manager

                # Verify that the edits where posted
                # TODO- determine failproof methods for isolating the changed features and viewing the change
                env.workspace = sde_file
                fields = [x for x in building_attributes.itervalues()]
                cursor = da.SearchCursor(bldgs, fields,
                                         "{} in ('{}')".format(building_attributes["Folio Number"], "','".join(folioIds)))
                try:
                    values = cursor.next()
                    arcpy.AddMessage("This is an edited row in the buildings table :: {}".format(values))
                except StopIteration:
                    arcpy.AddMessage("No buildings found with folioIDs in {}".format(folioIds))
                del cursor

            else:
                arcpy.AddMessage("The files are identical, no edits needed")

            return True

        except:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            arcpy.AddError(repr(traceback.format_exception(exc_type, exc_value, exc_traceback)))