Shapely is cool and elegant, but why not using still ogr, with its spatial operators (in OGRGeometry class)?
sample code:
from osgeo import ogr
driver = ogr.GetDriverByName('ESRI Shapefile')
polyshp = driver.Open('/home/pcorti/data/shapefile/multipoly.shp')
polylyr = polyshp.GetLayer(0)
pointshp = driver.Open('/home/pcorti/data/shapefile/point.shp')
pointlyr = pointshp.GetLayer(0)
point = pointlyr.GetNextFeature()
polygon = polylyr.GetNextFeature()
point_geom = point.GetGeometryRef()
polygon_geom = polygon.GetGeometryRef()
print point_geom.Within(polygon_geom)
Note that you need to compile GDAL with GEOS support.
You can use PostGIS to achieve what you need. PostGIS has a function called ST_line_interpolate_point(geom, location) which can take a line string e.g your road, and a location which is a percentage from the start of the road e.g distance/road_length.
I would, as I did this at work last week for something like this, create a table with your Accidents (events) and a table with your roads (just upload the one you have from QGIS into PostGIS using SPIT) and then join them using a common attribute.
SELECT ST_line_interpolate_point(roads.the_geom, event.distance / ST_Length(roads.the_geom)), event.id
FROM event
JOIN roads on event.roadid = roads."TIS_Code"
The above syntax might not be 100% but should get you some of the way. You might have to tweak it to handle if the road is shorter then the event distance etc (this might work added to your join WHERE event.distance > roads.BEGM AND event.distance < roads.ENDM)
If you want to then get the corrdinates from the points you can use the range of ST_As...
functions or you can wrap the above query in a another query.
SELECT ST_X(point),ST_Y(point), id
FROM (
SELECT ST_line_interpolate_point(roads.the_geom, event.distance / ST_Length(roads.the_geom)) as point,
event.id as id
FROM event
JOIN roads on event.roadid = roads."TIS_Code")
Works like a charm.
EDIT:
This might work better:
SELECT ST_line_interpolate_point(roads.the_geom, event.distance / ST_Length(roads.the_geom)), event.id
FROM event
JOIN roads on event.roadid = roads."TIS_Code" AND event.distance > roads.BEGM AND event.distance < roads.END
Best Answer
So, I've done both; created the as-the-crow-flies lines that just go straight from the surface hole to the bottom hole and taken directional survey data and created the actual path the drill bit followed (sometimes referred to as the "worm track") to create the borehole. As-the-crow-flies is much easier, and as Kirk mentioned in a comment, the XY to Line Tool should do what you need I think. Basically, you want to connect the surface hole location (SHL) with the bottom hole location (BHL) - those two sets of coordinates for a directional/horizontal well are usually available. A few years back, I wrote some Python and created a tool to do what that does, as the XY to Line Tool wasn't around back then. So you ended up with something like this:
Now, as far as plotting the actual wellbore, if you can get your hands on the directional survey, you can use the azimuth and northing/easting of each MWD point to get a XY value for each point. So, like in the above as-the-crow-flies scenario, you are plotting the SHL and BHL, but also every other point in between. If you just need 2D, then the XY will get you that, if you want 3D, then you need to use the depth value (Z) from the MWD for each point as well. You can also M-enable the line which will allow you to plot the perforations along the wellbore. With this method, you can achieve something like the green lines below (apologies on the sloppy screenshot, had to cover up some info on it), where multiple wells are drilled from the same wellpad:
If you're lucky, the directional survey vendor might do some of the conversions of azimuth northing/easting for you already. Once you have your XY's (and ZM's possibly), then you plot the points in order from surface down and connect the dots to form the line. I did this in SQL Server 2008 Spatial before, and I'm not sure if ArcGIS has tools that can accomplish this out of the box; it could surely be coded in Python though.