0

I have created two different point shapefiles which represent towns within a state. However I want to know the distance between both towns, but first I want to draw a line between them. Is this possible or should I merge both shapefiles together first?

I was trying to merge the point shapefiles together as one but I experienced an error.

TypeError: list indices must be integers or slices, not str

vicksent = ".vick.shp"
towncent= ".town.shp"

from shapely.geometry import MultiPoint, point, shape, mapping

schema = {'geometry': 'Point',
         'properties': {}, }
with fiona.open('mew.shp', 'w', 'ESRI Shapefile', schema,     crs=from_epsg(29900)) as output:
   prop = {}
   centriod1= vicksent
   centriod2 = towncent
   output.write([centriod1,{'geometry': point, 'properties': prop}])
   output.write([centriod2, {'geometry': point, 'properties': prop}])

1 Answer 1

1

You dont need to merge or create a line between them to measure distance:

import fiona, shapely.geometry

p1 = '/home/bera/GIS/Data/testdata/p1.shp'
p2 = '/home/bera/GIS/Data/testdata/p2.shp'

#https://gis.stackexchange.com/questions/140069/how-to-measure-distance-using-shapely
firstpoint = [shapely.geometry.shape(f['geometry']) for f in fiona.open(p1)][0]
secondpoint = [shapely.geometry.shape(f['geometry']) for f in fiona.open(p2)][0]

print(firstpoint.distance(secondpoint))

Im assuming you only have one point in each shapefile.

6
  • yes, i only have one point in each shapefile
    – rachel
    Commented May 10, 2020 at 17:34
  • but if i was to create a line between them, how willl i go with that if you dont mind me asking
    – rachel
    Commented May 10, 2020 at 17:39
  • 1
    That is a different question than what you have posted
    – Bera
    Commented May 10, 2020 at 17:50
  • its just i have been having difficulty for more than 3 hours trying to draw the line between two different point shapefile and time is not on my side, so please any little idea would be appreciated
    – rachel
    Commented May 10, 2020 at 17:52
  • 1
    Stop thinking about them as shapefiles, they are points once you have read them in.
    – Ian Turton
    Commented May 10, 2020 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.