Modify characters in a chain [closed]

-1

I have the following string

'POLYGON ((-58.612406970999984 -34.55196600599993, -58.61272573499997 -34.552244351999946, -58.611851334999983 -34.552907077999976, -58.611473561999958 -34.552566878999983, -58.612331868999945 -34.55191298799997, -58.612406970999984 -34.55196600599993))'

and I need to get to this

[((-58.612406970999984 ,-34.55196600599993),(-58.61272573499997, -34.552244351999946),(-58.611851334999983, -34.552907077999976),(-58.611473561999958,-34.552566878999983),(-58.612331868999945,-34.55191298799997),(-58.612406970999984,-34.55196600599993))]

Sorry if I can not make a breakthrough and do not achieve any. I do not intend to give me the result if not an orientation to be able to direct the solution.

    
asked by Sebastian 06.04.2018 в 20:54
source

3 answers

0

A very simple way is the following:

cadena = 'POLYGON ((-58.612406970999984 -34.55196600599993, -58.61272573499997 -34.552244351999946, -58.611851334999983 -34.552907077999976, -58.611473561999958 -34.552566878999983, -58.612331868999945 -34.55191298799997, -58.612406970999984 -34.55196600599993))'

lista = [(float(e[0]),float(e[1])) for e in [e.split() for e in cadena[11:-2].split(",")]]
print(lista)

[(58.612406970999984, -34.55196600599993),
 (-58.61272573499997, -34.552244351999946),
 (-58.61185133499998, -34.552907077999976),
 (-58.61147356199996, -34.55256687899998),
 (-58.612331868999945, -34.55191298799997),
 (-58.612406970999984, -34.55196600599993)]

We use nested list comprehension:

  • cadena[11:-2].split(",") , we cut the string with a "slice" to remove the text and the opening and closing parentheses (we only want coordinates and commas), then we make a split by the comma, which we leave a list with each pair of coordinates.

  • After the previous list for each element we do e.split() and now we separate the pairs of coordinates by the space in a list with two elements.

  • Finally, going back through the list (float(e[0]),float(e[1])) we convert each coordinate to a floating point number and return it as a tuple.

Attention: The result is a list of tuples, in your example you indicated that you require something like a list with a single tuple and with multiple tuples inside, something like this [((a,b),(b,c))] in case it is not an error typographic and effectively require something like that, you just have to make a minimum adjustment:

lista = []
tupla = tuple((float(e[0]),float(e[1])) for e in [e.split() for e in cadena[11:-2].split(",")])
lista.append(tupla)
    
answered by 06.04.2018 / 22:10
source
2

Although you have already answered how to do it using python "to dry" (with its string functions), I think I have an answer that uses a library more appropriate to your problem, which will allow you to convert similar data types (based on WKT) that may appear in the future.

In your question history I have seen that you have several related to geometric data and that in some of them you use the python library shapely . Well, this library has functions for parsing of WKT strings, of which your case is an example. Once you have parsed them, it offers you lots of methods to operate on them, such as calculating your area, intersections with others, checking whether or not it contains a point, etc.

For example, running the following on a notebook:

from shapely.wkt import loads
cadena = 'POLYGON ((-58.612406970999984 -34.55196600599993, -58.61272573499997 -34.552244351999946, -58.611851334999983 -34.552907077999976, -58.611473561999958 -34.552566878999983, -58.612331868999945 -34.55191298799997, -58.612406970999984 -34.55196600599993))'
g = loads(cadena)

And in g you have a variable of type shapely.geometry.polygon.Polygon , which among other things knows how to draw:

>>> g

Get your area, or your centroid:

>>> print(g.area, g.centroid)
5.468957898656394e-07 POINT (-58.61209796852194 -34.5524066377988)

And what you were looking for, the list of coordinates of its outline:

>>> list(g.boundary.coords)
[(-58.612406970999984, -34.55196600599993),
 (-58.61272573499997, -34.552244351999946),
 (-58.61185133499998, -34.552907077999976),
 (-58.61147356199996, -34.55256687899998),
 (-58.612331868999945, -34.55191298799997),
 (-58.612406970999984, -34.55196600599993)]
    
answered by 08.04.2018 в 19:20
0

If you want to convert your data, you can do the following:

cadena = 'POLYGON ((-58.612406970999984 -34.55196600599993, -58.61272573499997 -34.552244351999946, -58.611851334999983 -34.552907077999976, -58.611473561999958 -34.552566878999983, -58.612331868999945 -34.55191298799997, -58.612406970999984 -34.55196600599993))'

cadena = var.replace('POLYGON ', '').replace('((', '').replace('))', '').split(', ')

result = tuple([tuple(x.split(' ')) for x in cadena])

And if you want it in float

result = tuple([tuple([float(x) for x in x.split(' ')]) for x in cadena])

Although you should look at the comments because you can find a much easier solution.

    
answered by 06.04.2018 в 22:21