Esri / mdcs-py

MDCS is an acronym for Mosaic Dataset Configuration Script and is the entry point to a collection of Python classes/libraries that could be consumed by a Python client application to complete a given workflow for creating a mosaic dataset, populating it with data, and setting all required/desired parameters.
Apache License 2.0
64 stars 29 forks source link

Clip and Import Geometry #97

Closed giugent closed 2 years ago

giugent commented 2 years ago

Hello, I did not found any command to clip the geometry of boundary or footprints and replace their original geometry with the new clipped geometry. Is it under development ? Thanks a lot

vijaygit02 commented 2 years ago

@giugent , its the import mosaic dataset geometry command (IG) image

resQm3 commented 1 year ago

Hei, I stumbled over the same issue. I wrote this user command to MDCS_UC.py:

def ClipFootprints(self, data):
        """clip footprints with specified AOI and reimport footprint shape
        """
        base = data['base']
        base_workspace = data['workspace']  # base.m_geoPath
        md = data['mosaicdataset']
        log = data['log']
        xmlDOM = data['mdcs']
        ds = os.path.join(base_workspace, md)
        node = base.getXMLNode(xmlDOM, 'ClipFootprints')
        in_feature_class = os.path.normpath(base.getXMLNodeValue(node, 'in_feature_class'))
        where = base.getXMLNodeValue(node, 'where_clause')
        out_feature_class = os.path.normpath(base.getXMLNodeValue(node, 'out_feature_class'))

        # testing output workspace
        out_wksp, out_f = os.path.split(out_feature_class)
        if not out_wksp:
            # use FeatureClass Name without Database --> assuming that FeatureClass should be created in base.m_geoPath
            out_wksp = base.m_geoPath
        if not arcpy.Exists(out_wksp):
            log.Message("\tCreating Footprint Database: {}".format(out_wksp), base.const_general_text)
            out_wksp_parent_dir, out_wksp_name = os.path.split(out_wksp)
            try:
                arcpy.CreateFileGDB_management(out_wksp_parent_dir, out_wksp_name)
            except arcpy.ExecuteError as e:
                log.Message(arcpy.GetMessages(), base.const_critical_text)
                return False
            except Exception as e:
                log.Message(str(e), base.const_critical_text)
                return False

        fullpath = os.path.join(out_wksp, out_f)

        # make layer
        md_lyr = "mdlyr_{}_{}".format(md, dt.strftime(dt.now(), "%Y%m%d%H%M%S"))
        arcpy.MakeMosaicLayer_management(ds, md_lyr, "Category = 1")

        in_feature_class_basename = os.path.basename(in_feature_class)
        log.Message("\tSelecting feature from {0} where {1}".format(in_feature_class_basename, where), base.const_general_text)
        clipper_lyr = "clipper_{}".format(dt.strftime(dt.now(), "%Y%m%d%H%M%S"))
        arcpy.MakeFeatureLayer_management(in_feature_class, clipper_lyr, where)

        # test if clipper layer contains at least 1 feature
        item_count = int(arcpy.GetCount_management(clipper_lyr)[0])
        if item_count == 0:
            log.Message("No features selected. Nothing to clip.", base.const_warning_text)
            return True

        # clip footprints to polygon
        footprint_sub_layer = md_lyr + "/Footprint"
        log.Message("\tClip footprints...", base.const_general_text)
        try:
            with arcpy.EnvManager(overwriteOutput=True):
                arcpy.analysis.Clip(footprint_sub_layer, clipper_lyr, fullpath)
        except arcpy.ExecuteError as e:
            log.Message(arcpy.GetMessages(), base.const_warning_text)
        except Exception as e:
            log.Message(str(e), base.const_critical_text)
            return False
        log.Message("\tCreated clipped footprint geometry: {}.".format(fullpath), base.const_general_text)

        # import footprints to mosaic
        log.Message("\tImport clipped footprint geometry...", base.const_general_text)
        try:
            arcpy.ImportMosaicDatasetGeometry_management(md_lyr, 'FOOTPRINT', "Name", fullpath, "Name")
        except arcpy.ExecuteError as e:
            log.Message(arcpy.GetMessages(), base.const_critical_text)
            return False
        except Exception as e:
            log.Message(str(e), base.const_critical_text)
            return False

        arcpy.Delete_management([md_lyr, clipper_lyr])
        del md_lyr
        del clipper_lyr
        return True

And the command parameters in configuration XML:

<ClipFootprints>
    <in_feature_class>FEATURE TO CLIP TO</in_feature_class>
    <where_clause>WHERE CLAUSE TO FILTER IN_FEATURE_CLASS</where_clause>
    <out_feature_class>(TEMPORARY) LOCATION OF THE CLIPPED FEATURE CLASS</out_feature_class>
</ClipFootprints>

If you don't need the clipped footprints you could use in_memory workspace: in_memory/footprint_clipped

Hope this method could be integrated into MDCS as a standard command. Would be n1ce!!

Chamlika commented 1 year ago

@resQm3 Thanks for your feedback and the code to enhance MDCS. We'll review it to have the code integrated if it's still relevant in a future release. FYI @vijaygit02