Friday, 4 November 2016

arcgis desktop - PySAL natural breaks from Numpy array


I am attempting to extract natural break values from a raster using Python.


The intended process goes: Arcpy raster object to NumPy Array (using RasterToNumPyArray) to natural break values (using PySAL Natural Breaks function).



import arcpy, pysal
from pysal.esda.mapclassify import Natural_Breaks as nb
# code to create greenIndex arcpy Raster object here
greenArray = arcpy.RasterToNumPyArray(greenIndex)
breaks = nb(greenArray,k=2,initial=20)

This code returns the error, "ValueError: matrix must be 2-dimensional".


As far as I know, greenArray is a 2-dimensional array.



Answer



Thanks for the help, Branco and om_henners.



The answer to my problem appears to be to use numpy.ravel() to change the array produced by arcpy.RasterToNumPy() to a 1D array:


import arcpy, pysal
from pysal.esda.mapclassify import Natural_Breaks as nb
# code to create greenIndex arcpy Raster object here
greenArray = arcpy.RasterToNumPyArray(greenIndex)
breaks = nb(greenArray.ravel(),k=2,initial=20)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...