Skip to content

Commit

Permalink
Merge pull request #11 from procrastinatio/develop
Browse files Browse the repository at this point in the history
Version 0.0.4
  • Loading branch information
procrastinatio authored Jun 27, 2024
2 parents 02f4c99 + 107941e commit 46e4290
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 94 deletions.
64 changes: 35 additions & 29 deletions SymbolsFilter.pyt
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,12 @@ def get_complex_filter_criteria(labels, values, columns):

def convert_columns(df, columns_to_convert):
# Check if conversion is possible and convert
try:
for col in columns_to_convert:

for col in columns_to_convert:
if col is None or col == "":
logger.warning(f"Not converting column: {col}")
continue
try:
if (
df[col]
.dropna()
Expand All @@ -166,10 +170,10 @@ def convert_columns(df, columns_to_convert):
):
# Fill NaN values with 0 (or another specific value) before conversion
df[col] = df[col].fillna(0).astype(int)
except KeyError as ke:
logger.error(f"Key error while converting column {col}: {ke}")
except Exception as e:
logger.error(f"Unknown error: {e}")
except KeyError as ke:
logger.error(f"Key error while converting column {col}: {ke}")
except Exception as e:
logger.error(f"Unknown error: {e}")

return df

Expand All @@ -178,7 +182,9 @@ def save_to_files(output_path, filtered, drop_null=True, engine=None):
try:
data = filtered # results["layers"]

with open(output_path.replace(".xlsx", ".json"), "w", encoding="utf-8") as f:
with open(
output_path.replace(".xlsx", ".json"), "w", encoding="windows-1252"
) as f:
# Serialize the data and write it to the file
json.dump(filtered, f, ensure_ascii=False, indent=4)
except Exception as e:
Expand Down Expand Up @@ -327,7 +333,6 @@ class SymbolFilter:

except Exception as e:
logger.error(e)
messages.addErrorMessage(type(spatial_filter))
messages.addErrorMessage(
"Layer {0} has no selected features.".format(inLayer)
)
Expand Down Expand Up @@ -359,10 +364,12 @@ class SymbolFilter:
labels = renderer.get("labels")

sql = get_query_defn(data)
messages.addMessage(f"sql={sql}")
messages.addMessage(f" sql={sql}")

if columns is None:
logger.warning(f"No headings found for {layername}: {columns}")
logger.warning(
f" No headings found for {layername}: {columns}. Skipping"
)
continue

# Get the selected features using a search cursor with spatial filter
Expand All @@ -376,15 +383,15 @@ class SymbolFilter:
feature_class_path, spatial_filter=spatial_filter, query=sql
)
df = arcgis_table_to_df("TOPGIS_GC.GC_BED_FORM_ATT")
logger.debug(df)
logger.debug(gdf)
gdf = gdf.merge(df, left_on="FORM_ATT", right_on="UUID")
logger.debug(f" ====== MERGING")
logger.debug(gdf)

# TODO
if not "toto" in layername: # "Quelle" in layername:
# TODO Attribut SEEBODEN???
if not "Deposits_Chrono" in layername: # "Quelle" in layername:
features_rules_sum = 0
if columns is None or any(col is None for col in columns):
messages.addErrorMessage(
f"<null> column are not valid: {columns}. Skipping"
)
logger.error(f"<null> column are not valid: {columns}")
continue
if gdf is None:
Expand All @@ -397,15 +404,11 @@ class SymbolFilter:
)
except Exception as e:
logger.error(
f"Error while getting dataframe fro layer {layername}: {e}"
f"Error while getting dataframe from layer {layername}: {e}"
)
continue
feat_total = str(len(gdf))

messages.addMessage(
f"{feat_total : >10} objects in selected feature".encode("cp1252")
)

complex_filter_criteria = get_complex_filter_criteria(
labels, values, columns
)
Expand All @@ -420,7 +423,7 @@ class SymbolFilter:
results = {}

for label, criteria in complex_filter_criteria:
logger.info(f"\nApplying criteria: {label}, {criteria}")
logger.debug(f"\nApplying criteria: {label}, {criteria}")

# Start with a True series to filter
filter_expression = pd.Series([True] * len(df), index=df.index)
Expand All @@ -435,13 +438,8 @@ class SymbolFilter:
# Apply the final filter to the DataFrame
filtered_df = df[filter_expression]

"""results[label] = {
"count": len(filtered_df),
"rows": filtered_df.to_json(orient='records') , # filtered_df,
"criteria": criteria,
}"""

count = len(filtered_df)
features_rules_sum += count

if count > 0:
count_str = str(count)
Expand All @@ -458,14 +456,22 @@ class SymbolFilter:
logger.info(f"Count: {result['count']}")
logger.info("Matching Rows:")
logger.info(result["rows"])"""
logger.info("---")

filtered[layername] = results
messages.addMessage(
f" ----------\n{feat_total : >10} in selected extent (with query_defn)".encode(
"cp1252"
)
)
messages.addMessage(
f"{features_rules_sum : >10} in classes".encode("cp1252")
)

messages.addMessage(f"---- Saving results to {output_path} ----------")

# TODO: encoding issue
save_to_files(output_path, filtered, drop_null=True, engine=None)
messages.addMessage("Done.")

return

Expand Down
2 changes: 1 addition & 1 deletion SymbolsFilter.pyt.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0"?>
<metadata xml:lang="en"><Esri><CreaDate>20240625</CreaDate><CreaTime>10371500</CreaTime><ArcGISFormat>1.0</ArcGISFormat><SyncOnce>TRUE</SyncOnce><ModDate>20240627</ModDate><ModTime>142801</ModTime><scaleRange><minScale>150000000</minScale><maxScale>5000</maxScale></scaleRange><ArcGISProfile>ItemDescription</ArcGISProfile></Esri><toolbox name="SymbolsFilter" alias="Geocover"><arcToolboxHelpPath>c:\program files\arcgis\pro\Resources\Help\gp</arcToolboxHelpPath><toolsets/></toolbox><dataIdInfo><idCitation><resTitle>SymbolsFilter</resTitle></idCitation><idPurp>Cleaning up symbols without any features in a given extent</idPurp><searchKeys><keyword>symbols</keyword><keyword>rules</keyword></searchKeys></dataIdInfo><distInfo><distributor><distorFormat><formatName>ArcToolbox Toolbox</formatName></distorFormat></distributor></distInfo><mdHrLv><ScopeCd value="005"></ScopeCd></mdHrLv><mdDateSt Sync="TRUE">20240626</mdDateSt></metadata>
<metadata xml:lang="en"><Esri><CreaDate>20240625</CreaDate><CreaTime>10371500</CreaTime><ArcGISFormat>1.0</ArcGISFormat><SyncOnce>TRUE</SyncOnce><ModDate>20240627</ModDate><ModTime>211147</ModTime><scaleRange><minScale>150000000</minScale><maxScale>5000</maxScale></scaleRange><ArcGISProfile>ItemDescription</ArcGISProfile></Esri><toolbox name="SymbolsFilter" alias="Geocover"><arcToolboxHelpPath>c:\program files\arcgis\pro\Resources\Help\gp</arcToolboxHelpPath><toolsets/></toolbox><dataIdInfo><idCitation><resTitle>SymbolsFilter</resTitle></idCitation><idPurp>Cleaning up symbols without any features in a given extent</idPurp><searchKeys><keyword>symbols</keyword><keyword>rules</keyword></searchKeys></dataIdInfo><distInfo><distributor><distorFormat><formatName>ArcToolbox Toolbox</formatName></distorFormat></distributor></distInfo><mdHrLv><ScopeCd value="005"></ScopeCd></mdHrLv><mdDateSt Sync="TRUE">20240626</mdDateSt></metadata>
66 changes: 2 additions & 64 deletions layer_symbols_rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -16674,7 +16674,7 @@
"headings": [
"TTEC_STATUS",
"TTEC_META_STA",
null
"TTEC_LIM_TYP"
],
"labels": [
"gesichert, Decken trennend",
Expand Down Expand Up @@ -17152,68 +17152,6 @@
],
"dataSource": "Instance=GCOVERP,Database Platform=Oracle,Version=SDE.DEFAULT (Traditional),Authentication Type=Operating System Authentication,Feature Dataset=TOPGIS_GC.GC_ROCK_BODIES,Dataset=TOPGIS_GC.GC_LINEAR_OBJECTS"
},
"Unco Deposits_Chrono": {
"renderer": {
"headings_alias": [
""
],
"headings": [
null
],
"labels": [
"Pliozän",
"Holozän",
"Frühes Pleistozän",
"Mittleres Pleistozän",
"Spätes Pleistozän",
"Pleistozän"
],
"values": [
[
[
"15001014"
]
],
[
[
"15001004"
]
],
[
[
"15001009"
]
],
[
[
"15001007"
]
],
[
[
"15001006"
]
],
[
[
"15001005"
]
]
]
},
"query_defn": [
{
"name": ""
},
{
"sql": "RUNC_LITHO IN (15101019, 15101021, 15101024, 15101027, 15101028, 15101029, 15101030, 15101031, 15101032, 15101033, 15101034, 15101046, 15101047, 15101048, 15101049, 15101055, 15101058)"
},
{
"isActive": true
}
],
"dataSource": "Instance=GCOVERP,Database Platform=Oracle,Version=SDE.DEFAULT (Traditional),Authentication Type=Operating System Authentication,Feature Dataset=TOPGIS_GC.GC_ROCK_BODIES,Dataset=TOPGIS_GC.GC_UNCO_DESPOSIT"
},
"Unco Deposits_Litho_>20000": {
"renderer": {
"type": "SimpleRenderer"
Expand All @@ -17237,7 +17175,7 @@
"Seebodesediment"
],
"headings": [
null
"SEEBODESEDIMENT"
],
"labels": [
"Seebodensediment, Holozän",
Expand Down

0 comments on commit 46e4290

Please sign in to comment.