1
$\begingroup$

I am trying to write a script to use in the Sverchok addon, that utilizes a file path input, but I am having trouble making this work.

Here is my script:

"""
in path FP
out Attibute s
"""

import geopandas as gpd
import pandas as pd
import fiona
import numpy as np

path2 = path

gpd1 = gpd.read_file(path2, layer = "test shapes - polygons 1")

# call geo interface method on geodataframe
gi = gpd1.__geo_interface__

# variable to control which attribute is being extracted
variableAttribute = 'Integer'

# create empty list to add attributes to
listAttribute = []

# loop through geointerface (gi) and extract values from the 'Integer' column, and add them to the list created above
for features in range(len(gi['features'])):
    value = [gi['features'][features]['properties'][variableAttribute]]
    
    listAttribute.append(value)
    
Attribute = listAttribute

Here is a screenshot of my node setup:

enter image description here

For some reason, the filepath input works in a basic script (see the 'test_filepath1.py' in the screenshot). But, when trying to implement this in a more complex script, I cannot make this work.

Here is a zip file with sample data, blend file, and scripts: https://drive.google.com/file/d/1GZMr7hYXxMN8YJNQ2lIls4B7O_ANEI1F/view?usp=sharing

Any ideas or info on why this isn't working? Thanks,

$\endgroup$

1 Answer 1

1
$\begingroup$

In your example code there is a variable-name-typo (results in an Exception; Attribute vs Attibute, but after that's fixed there's still some things to explain.

"""
in path FP
out Attribute s
"""

import geopandas as gpd
import pandas as pd
import fiona
import numpy as np


gpd1 = gpd.read_file(path, layer = "test shapes - polygons 1")
gi = gpd1.__geo_interface__
variableAttribute = 'Integer'

# loop through geointerface (gi) and extract values from the 'Integer' column, and add them to the list created above
for features in range(len(gi['features'])):
    value = [gi['features'][features]['properties'][variableAttribute]]
    Attribute.append(value)

If you have the command prompt open or the info panel, you might be able to see the nature of the error if you enable "raise error to treelevel" from the N panel of the node.

shows location of the raiseerror toggle

Usually this will be enough to show the error/exception right beside the node in the nodeview.

show error in nodeview

Additionally, when connected to a Filepath node, the content of the socket will be wrapped by two sets of square brackets (this is the nature of the FilePath socket stream). Something like

[["D:\my_file.txt"]]

in that case your gpd.read_file function line should read something like

gpd1 = gpd.read_file(path[0][0], layer = "test shapes - polygons 1")

$\endgroup$
1
  • $\begingroup$ Thanks for catching that typo, unfortunately fixing it did not make the script work. I tried some of your other suggestions here, but am still cannot get the file path to work. $\endgroup$
    – MarcusR
    Mar 31, 2022 at 4:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .