[GRASS-user] Uploading NumPy array as a new column to a vector map

Greetings,

I have a municipality polygon layer with a poverty index (percent) and a population count, I’m multiplying them to visualize the vulnerability for each municipality, due to the distribution of that new variable I want to compute the logarithm, which is not possible in sqlite, so I tried reading the column with numpy and issuing a np.log10(). I’m happy with the result, but how can I get this new numpy array to a new column in the vector map in a simple way?

This is what I already have:

#!/usr/bin/env python

import grass.script as gs
import numpy as np
import matplotlib.pyplot as plt

nbi_pob = np.array(gs.vector_db_select(“muniant”, columns = “nbi_pob”)[“values”].values()).astype(float).squeeze()

plt.hist(nbi_pob)
plt.show()

log_nbi_pob = np.log10(nbi_pob)

plt.hist(log_nbi_pob)
plt.show()

I’m trying to introduce this kind of analysis to students who are not familiar nor proficient with programming (this is an GIS introductory course) so the numpy bit is already complex enough to them, and I’d like to avoid using for loops with cursors to achieve this.

Is there a simple solution to this?

···

César Augusto Ramírez Franco

Never tried it but you got me curious.

It looks simpler to define a new function or the database using python sqlite3 libs

https://docs.python.org/2/library/sqlite3.html#sqlite3.Connection.create_function

Cheers

On Thu, Feb 14, 2019 at 12:50 PM César Augusto Ramírez Franco <caesarivs@gmail.com> wrote:

Greetings,

I have a municipality polygon layer with a poverty index (percent) and a population count, I’m multiplying them to visualize the vulnerability for each municipality, due to the distribution of that new variable I want to compute the logarithm, which is not possible in sqlite, so I tried reading the column with numpy and issuing a np.log10(). I’m happy with the result, but how can I get this new numpy array to a new column in the vector map in a simple way?

This is what I already have:

#!/usr/bin/env python

import grass.script as gs
import numpy as np
import matplotlib.pyplot as plt

nbi_pob = np.array(gs.vector_db_select(“muniant”, columns = “nbi_pob”)[“values”].values()).astype(float).squeeze()

plt.hist(nbi_pob)
plt.show()

log_nbi_pob = np.log10(nbi_pob)

plt.hist(log_nbi_pob)
plt.show()

I’m trying to introduce this kind of analysis to students who are not familiar nor proficient with programming (this is an GIS introductory course) so the numpy bit is already complex enough to them, and I’d like to avoid using for loops with cursors to achieve this.

Is there a simple solution to this?

César Augusto Ramírez Franco


grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

···

On 2/14/19 4:49 PM, César Augusto Ramírez Franco wrote:

Greetings,

I have a municipality polygon layer with a poverty index (percent) and a population count, I’m multiplying them to visualize the vulnerability for each municipality, due to the distribution of that new variable I want to compute the logarithm, which is not possible in sqlite, so I tried reading the column with numpy and issuing a np.log10(). I’m happy with the result, but how can I get this new numpy array to a new column in the vector map in a simple way?

This is what I already have:

#!/usr/bin/env python

import grass.script as gs
import numpy as np
import matplotlib.pyplot as plt

nbi_pob = np.array(gs.vector_db_select(“muniant”, columns = “nbi_pob”)[“values”].values()).astype(float).squeeze()

plt.hist(nbi_pob)
plt.show()

log_nbi_pob = np.log10(nbi_pob)

plt.hist(log_nbi_pob)
plt.show()

I’m trying to introduce this kind of analysis to students who are not familiar nor proficient with programming (this is an GIS introductory course) so the numpy bit is already complex enough to them, and I’d like to avoid using for loops with cursors to achieve this.

I also can’t think of any simple way to do this. However PostgreSQL does have a log() function. So if you migrate your backend DB to PostgreSQL you should be able to do (not tested):

gs.run_command(‘v.db.addcolumn’, map_=“muniant”, column=“log_nbi_pob DOUBLE”)

gs.run_command(‘v.to.db’, map_=“muniant”, column=“log_nbi_pob”, query=“log(nbi_pob)”)

But I must say that it seems strange that you are introducing numpy, matplotlib and the GRASS python interface to students who don’t yet have the basic tools of programming, such as ‘for’ loops. Isn’t this is a case of “putting the wagon in front of the horse”?? :slight_smile:

Is there a simple solution to this?

César Augusto Ramírez Franco

_______________________________________________
grass-user mailing list
[grass-user@lists.osgeo.org](mailto:grass-user@lists.osgeo.org)
[https://lists.osgeo.org/mailman/listinfo/grass-user](https://lists.osgeo.org/mailman/listinfo/grass-user)
-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918

Hi,

Am Do., 14. Feb. 2019, 15:50 hat César Augusto Ramírez Franco <caesarivs@gmail.com> geschrieben:

Greetings,

I have a municipality polygon layer with a poverty index (percent) and a population count, I’m multiplying them to visualize the vulnerability for each municipality, due to the distribution of that new variable I want to compute the logarithm, which is not possible in sqlite,

Yes but there is an extension for this.

See
https://grasswiki.osgeo.org/wiki/Build_SQLite_extension_on_Linux

It may solve the issue.

Best
Markus

so I tried reading the column with numpy and issuing a np.log10(). I’m happy with the result, but how can I get this new numpy array to a new column in the vector map in a simple way?

This is what I already have:

#!/usr/bin/env python

import grass.script as gs
import numpy as np
import matplotlib.pyplot as plt

nbi_pob = np.array(gs.vector_db_select(“muniant”, columns = “nbi_pob”)[“values”].values()).astype(float).squeeze()

plt.hist(nbi_pob)
plt.show()

log_nbi_pob = np.log10(nbi_pob)

plt.hist(log_nbi_pob)
plt.show()

I’m trying to introduce this kind of analysis to students who are not familiar nor proficient with programming (this is an GIS introductory course) so the numpy bit is already complex enough to them, and I’d like to avoid using for loops with cursors to achieve this.

Is there a simple solution to this?

César Augusto Ramírez Franco


grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

On Fri, Feb 15, 2019, 06:01 Micha Silver <tsvibar@gmail.com wrote:

On 2/14/19 4:49 PM, César Augusto Ramírez Franco wrote:

Greetings,

I have a municipality polygon layer with a poverty index (percent) and a population count, I’m multiplying them to visualize the vulnerability for each municipality, due to the distribution of that new variable I want to compute the logarithm, which is not possible in sqlite, so I tried reading the column with numpy and issuing a np.log10(). I’m happy with the result, but how can I get this new numpy array to a new column in the vector map in a simple way?

This is what I already have:

#!/usr/bin/env python

import grass.script as gs
import numpy as np
import matplotlib.pyplot as plt

nbi_pob = np.array(gs.vector_db_select(“muniant”, columns = “nbi_pob”)[“values”].values()).astype(float).squeeze()

plt.hist(nbi_pob)
plt.show()

log_nbi_pob = np.log10(nbi_pob)

plt.hist(log_nbi_pob)
plt.show()

I’m trying to introduce this kind of analysis to students who are not familiar nor proficient with programming (this is an GIS introductory course) so the numpy bit is already complex enough to them, and I’d like to avoid using for loops with cursors to achieve this.

I also can’t think of any simple way to do this. However PostgreSQL does have a log() function. So if you migrate your backend DB to PostgreSQL you should be able to do (not tested):

gs.run_command(‘v.db.addcolumn’, map_=“muniant”, column=“log_nbi_pob DOUBLE”)

gs.run_command(‘v.to.db’, map_=“muniant”, column=“log_nbi_pob”, query=“log(nbi_pob)”)

But I must say that it seems strange that you are introducing numpy, matplotlib and the GRASS python interface to students who don’t yet have the basic tools of programming, such as ‘for’ loops. Isn’t this is a case of “putting the wagon in front of the horse”?? :slight_smile:

That script hasn’t been introduced to students, I was just fiddling with the data after a class when the question arose, trying to figure out how to do that. I do most of the Lab research analysis with R, and lately I’ve been studying the scipy ecosystem to try and do them within R, but maybe Markus’ suggestion would do to get students going with their course’s projects.

Thanks!

Is there a simple solution to this?

César Augusto Ramírez Franco

_______________________________________________
grass-user mailing list
[grass-user@lists.osgeo.org](mailto:grass-user@lists.osgeo.org)
[https://lists.osgeo.org/mailman/listinfo/grass-user](https://lists.osgeo.org/mailman/listinfo/grass-user)
-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918

On Fri, Feb 15, 2019, 09:17 Markus Neteler <neteler@osgeo.org wrote:

Hi,

Am Do., 14. Feb. 2019, 15:50 hat César Augusto Ramírez Franco <caesarivs@gmail.com> geschrieben:

Greetings,

I have a municipality polygon layer with a poverty index (percent) and a population count, I’m multiplying them to visualize the vulnerability for each municipality, due to the distribution of that new variable I want to compute the logarithm, which is not possible in sqlite,

Yes but there is an extension for this.

See
https://grasswiki.osgeo.org/wiki/Build_SQLite_extension_on_Linux

It may solve the issue.

Best
Markus

Thanks!

That’s actually pretty neat! I’ll test this in the lab’s computers this week.

so I tried reading the column with numpy and issuing a np.log10(). I’m happy with the result, but how can I get this new numpy array to a new column in the vector map in a simple way?

This is what I already have:

#!/usr/bin/env python

import grass.script as gs
import numpy as np
import matplotlib.pyplot as plt

nbi_pob = np.array(gs.vector_db_select(“muniant”, columns = “nbi_pob”)[“values”].values()).astype(float).squeeze()

plt.hist(nbi_pob)
plt.show()

log_nbi_pob = np.log10(nbi_pob)

plt.hist(log_nbi_pob)
plt.show()

I’m trying to introduce this kind of analysis to students who are not familiar nor proficient with programming (this is an GIS introductory course) so the numpy bit is already complex enough to them, and I’d like to avoid using for loops with cursors to achieve this.

Is there a simple solution to this?

César Augusto Ramírez Franco


grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

On Thu, Feb 14, 2019, 19:25 Daniel Victoria <daniel.victoria@gmail.com wrote:

Never tried it but you got me curious.

It looks simpler to define a new function or the database using python sqlite3 libs

https://docs.python.org/2/library/sqlite3.html#sqlite3.Connection.create_function

Cheers

Thanks for the suggestion, this seems still kind of complex to do without proper programming knowledge, I’ll try Markus’ suggestion on monday, it seems like a much nicer approach than trying to explain numpy in the python console to students.

On Thu, Feb 14, 2019 at 12:50 PM César Augusto Ramírez Franco <caesarivs@gmail.com> wrote:

Greetings,

I have a municipality polygon layer with a poverty index (percent) and a population count, I’m multiplying them to visualize the vulnerability for each municipality, due to the distribution of that new variable I want to compute the logarithm, which is not possible in sqlite, so I tried reading the column with numpy and issuing a np.log10(). I’m happy with the result, but how can I get this new numpy array to a new column in the vector map in a simple way?

This is what I already have:

#!/usr/bin/env python

import grass.script as gs
import numpy as np
import matplotlib.pyplot as plt

nbi_pob = np.array(gs.vector_db_select(“muniant”, columns = “nbi_pob”)[“values”].values()).astype(float).squeeze()

plt.hist(nbi_pob)
plt.show()

log_nbi_pob = np.log10(nbi_pob)

plt.hist(log_nbi_pob)
plt.show()

I’m trying to introduce this kind of analysis to students who are not familiar nor proficient with programming (this is an GIS introductory course) so the numpy bit is already complex enough to them, and I’d like to avoid using for loops with cursors to achieve this.

Is there a simple solution to this?

César Augusto Ramírez Franco


grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user