Python Forum
Creating a Point from coordinate fields in shapefile
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a Point from coordinate fields in shapefile
#1
Hi there,
I've been trying to use the Point method from shapely.geometry module to create a point, but using two fields in the shapefile being used.
As you can see I have created a dataframe from the shapefile. Even though the features have their own geometry I want to use a separate field with eastings/northing to create points. They represent another feature.

PTV_Playground_Joined_initial = gpd.read_file("file string to shp")

df = PTV_Playground_Joined_initial

orig_xy = []
for i in df: #create point of df

    orig_xy.append([Point(i[float(df.loc[i,"PTV_E"])], i[float(df.loc[i,"PTV_N"])])])
    
print(orig_xy)     
The problem must be due to the fact that I am using two strings from two fields in the Points method, which I guess isn't the normal way of using this. There is KeyError: 'RouteUsing' in the below error...

(base) C:\Users\geogjo>python C:\Users\geogjo\Desktop\PythonTraining\Network_Analysis_Test.py
Traceback (most recent call last):
  File "C:\Users\geogjo\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py", line 2657, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 129, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index_class_helper.pxi", line 91, in pandas._libs.index.Int64Engine._check_type
KeyError: 'RouteUsing'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\geogjo\Desktop\PythonTraining\Network_Analysis_Test.py", line 106, in <module>
    orig_xy.append([Point(i[float(df.loc[i,"PTV_E"])], i[float(df.loc[i,"PTV_N"])])])
  File "C:\Users\geogjo\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexing.py", line 1494, in __getitem__
    return self._getitem_tuple(key)
  File "C:\Users\geogjo\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexing.py", line 868, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "C:\Users\geogjo\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexing.py", line 988, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "C:\Users\geogjo\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexing.py", line 1913, in _getitem_axis
    return self._get_label(key, axis=axis)
  File "C:\Users\geogjo\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexing.py", line 141, in _get_label
    return self.obj._xs(label, axis=axis)
  File "C:\Users\geogjo\AppData\Roaming\Python\Python37\site-packages\pandas\core\generic.py", line 3585, in xs
    loc = self.index.get_loc(key)
  File "C:\Users\geogjo\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py", line 2659, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 129, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index_class_helper.pxi", line 91, in pandas._libs.index.Int64Engine._check_type
KeyError: 'RouteUsing'
I am fairly new to Python so apologies if this is an obvious fix.

Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare fields from two csv files georgebijum 3 1,335 Apr-25-2022, 11:16 PM
Last Post: Pedroski55
  Random coordinate generator speed improvement saidc 0 2,021 Aug-01-2021, 11:09 PM
Last Post: saidc
  Json fields not being retrieved mrcurious2020 4 1,989 Sep-14-2020, 06:24 AM
Last Post: bowlofred
  Return draw.rectangle pixel coordinate data to .csv file CephloRhod 0 2,362 May-20-2020, 10:37 AM
Last Post: CephloRhod
  connecting the first point to the last point Matplotlib omar_mohsen 0 4,527 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen
  image grid coordinate system Bmart6969 0 1,360 Oct-12-2019, 05:20 AM
Last Post: Bmart6969
  Question about shapefile oz1988 3 2,811 Jul-25-2018, 09:24 AM
Last Post: buran
  Mapping Fields zak308 0 2,467 Jan-09-2018, 10:02 PM
Last Post: zak308

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020