Dive into Code

Discover code snippets, tutorials, and programming insights

Python

Read ics file with python3

Read ics file with python3

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import urllib.request
from icalendar import Calendar
url = 'your ics url here'
req = urllib.request.Request(url, headers={'User-Agent': "Mozila Firefox"})
con = urllib.request.urlopen(req)
gcal = Calendar.from_ical(con.read())
for component in gcal.walk():
    if component.name == "VEVENT":
        print(component.get('summary'))
        print(component.get('dtstart').dt)
        print(component.get('dtend').dt)
        print(component.get('dtstamp'))
con.close()
Python

Read srid of shapefile

Read srid of a shapefile

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from osgeo import ogr, osr
shapefile = ogr.Open("shapefile.shp")
prj = ogr.Open("shapefile.prj")
layer = shapefile.GetLayer(0)
crs = layer.GetSpatialRef()
prj_file = open("shapefile.prj", 'r')
prj_txt = prj_file.read()
srs = osr.SpatialReference()
srs.ImportFromESRI([prj_txt])
print('Shape prj is: %s' % prj_txt)
print('WKT is: %s' % srs.ExportToWkt())
print('Proj4 is: %s' % srs.ExportToProj4())
srs.AutoIdentifyEPSG()
print('EPSG is: %s' % srs.GetAuthorityCode(None))