hello_12039
hello_12039

Reputation: 69

how to split tuple and assign to new variables python

I have the following code to get a shapefile's layer extent in python which prints the extent as (317044.25, 322287.25, 681703.75, 685053.25)

from osgeo import ogr

# Get a Layer's Extent
inShapefile = "c:/shapefile.shp"
inDriver = ogr.GetDriverByName("ESRI Shapefile")
inDataSource = inDriver.Open(inShapefile, 0)
inLayer = inDataSource.GetLayer()
extent = inLayer.GetExtent()
featureCount = inLayer.GetFeatureCount()

print(extent)

I'd now like to do a few things to this tuple:

Therefore leaving my results as follows:

extent1 = 317044, 322287
extent2 = 681703, 685053

I've tried the following code to split the tuple, but can't figure out how to assign to new variables and do the other requirements

# tuple into 2 tuples
res = tuple(extent[x:x + 2] 
      for x in range(0, len(extent), 2))
  
# printing result
print (str(res))

Upvotes: 0

Views: 769

Answers (2)

tony
tony

Reputation: 988

Separating tuples into variables

Sequence unpacking and tuple packing?

from osgeo import ogr

## Get a Layer's Extent
inShapefile = "c:/shapefile.shp"
inDriver = ogr.GetDriverByName("ESRI Shapefile")
inDataSource = inDriver.Open(inShapefile, 0)
inLayer = inDataSource.GetLayer()

## Unpack
x_min, x_max, y_min, y_max = inLayer.GetExtent()
# (317044.25, 322287.25, 681703.75, 685053.25)

## Pack
extent1 = x_min, x_max
# (317044.25, 322287.25)
extent2 = y_min, y_max
# (681703.75, 685053.25)

Removing decimal place

If you want to round the floats, you can wrap the variables in int():

extent1 = int(x_min), int(x_max)
# (317044, 322287)
extent2 = int(y_min), int(y_max)
# (681703, 685053)

You can also use round(x[, n]), math.floor(x), math.ceil(x) depending on what your needs are.

P.S. You may examples from Python GDAL/OGR Cookbook of use.

Removing brackets

My hunch is you may be mistaking these tuple / sequence operations for string outputs. I don't believe you intend to be doing this.

However, lets say you really wanted to coalesce a tuple like (317044, 322287) into a str object that printed like 317044 322287:

extent1_str = " ".join([str(item) for item in extent1])
# '317044 322287'
extent2_str = " ".join([str(item) for item in extent2])
# '681703 685053'

For future reference, we'd say "317044 322287" (in single or double quotes) which would imply we're referring to textual data (aka "string literal"). These look like magic, but in reality they're computed by "dunder" methods on the object: __str__

Upvotes: 2

Kalyan Reddy
Kalyan Reddy

Reputation: 326

The question is bit unclear, But I assume that you want to convert decimle to integer and assign it to two varibles
Hers's the code that might solve your problem.

extent=(317044.25, 322287.25, 681703.75, 685053.25)
extent = list(map(int,extent))

extent1=extent[:2]
extent2=extent[2:]

print(extent1,extent2)

The output:

[317044, 322287] [681703, 685053]

To print according to your requirement:

print('extent1 = {},{}'.format(extent1[0],extent1[1]))
print('extent2 = {},{}'.format(extent2[0],extent2[1]))

Output:

extent1 = 317044,322287
extent2 = 681703,685053

Upvotes: 0

Related Questions