-2

I am using shapely to convert longitudes and latitudes contained in a CSV to WKB format. For context, the CSV is parsed using a python AWS Lambda code.

The format of the file being parsed is as follows:

Name Comments Yes/No Latitude Longitude
Test_1 This is a test Yes 56.872993679521244 -4.141840954280135
Test_2 This is a test No 55.20394960316738 -2.5378432182396935

I have then tried to convert these to WKB by calling the longtiude/latitude columns

g = ('Point(longitude, latitude)')
new = wkb.dumps(g, hex=True, srid=4326)
print (new)

but I am getting the error 'str' object has no attribute '_geom'

How can I overcome this? The CSV has 100s of rows and I don't want to have to convert each long/lat individually - I just wanted to call the two columns and it would produce the WKB for each point.

  • 2
    Post the full error. – felipe Jun 18 '21 at 17:39
  • 2
    Your code sets `g` to a string value. But `dumps()` is expecting a `Point` object. I think you might mean `g = Point(longitude, latitude)` without enclosing quotes. I'm puzzled why you thought it needed quotes. – BoarGules Jun 18 '21 at 17:50
  • 1
    @BoarGules because it tells me point isn't defined if I try ```g = Point(longitude, latitude)``` – lambda_new Jun 18 '21 at 17:55
  • You need to add `from shapely.geometry import Point` at the top of your script, then remove the quotes from around `g = ('Point(longitude, latitude)')` so it's `g = Point(longitude, latitude)` or maybe yet something else, such as a list of `Point`s? – Czaporka Jun 18 '21 at 18:01
  • that solves the ```Point is not defined``` issue but then I get the error that ```longtiude is not defined```, so I done ```g = Point('longitude', 'latitude')``` but now there is an error ```must be real number, not str``` – lambda_new Jun 18 '21 at 18:10
  • Putting quotes around variable names may make the error message go away but it won't solve your problem. `longitude is not defined` is telling you very straightforwardly that somewhere before that your code must *assign* a value to `longitude`, like this: `longitude = -4.141840954280135`. If you don't have that, or something equivalent, `shapely` won't have any numbers to work with. I think you may need to spend a couple of hours working through this: https://docs.python.org/3/tutorial/ – BoarGules Jun 18 '21 at 21:03

2 Answers2

0

You need to pass the column values to Point function as following-

def calculate_wkb(lon, lat):
    g = Point(lon, lat)
    new = wkb.dumps(g, hex=True, srid=4326)
    return new

df['wkb'] = df.apply(lambda x: calculate_wkb(x.Longitude, x.Latitude), axis=1)
Shradha
  • 2,232
  • 1
  • 14
  • 26
0

You have to convert the point definition to wkt before converting to wkb, additionally coordinates must be separated by blank space, not colon.

from shapely import wkb
from shapely import wkt

g = wkt.loads('Point(-4.141840954280135 56.872993679521244)')
new = wkb.dumps(g, hex=True, srid=4326)
print (new)
Pepe N O
  • 1,678
  • 1
  • 7
  • 11
  • I don't want to convert each point individually though. Is there not a way to do this for all the entries in the longitude and latitude columns? – lambda_new Jun 18 '21 at 18:00
  • i wrote the values instead of longitude and latitude variables for illustration purposes only, you can substitute them in your code without a problem (i guess). – Pepe N O Jun 18 '21 at 18:03
  • Thats what I have tried and it doesn't work unfortunately. It throws the above error – lambda_new Jun 18 '21 at 18:06