Google KML file to python - kml

I have the following code to access coordinates of kml files in python
from pykml import parser
with open('test.kml', 'r') as kml_file:
root = parser.parse(kml_file).getroot()
for i in root.findall('{http://www.opengis.net/kml/2.2}Document/{http://www.opengis.net/kml/2.2}Placemark/{http://www.opengis.net/kml/2.2}Point'):
print(i.coordinates)
This one finds me all individual points in the kml file, where I marked a certain points of interest. But I also have some polygons of points that I created in google earth and this algorithm does not return them. How can I also get the polygons?
Please let me know if you have any questions.

If the KML source file has a Document with Placemarks then the following Python code will iterate over each Placemark with a Polygon geometry and dump out the coordinates.
from pykml import parser
with open('test.kml', 'r') as f:
root = parser.parse(f).getroot()
namespace = {"kml": 'http://www.opengis.net/kml/2.2'}
pms = root.xpath(".//kml:Placemark[.//kml:Polygon]", namespaces=namespace)
for p in pms:
print(p.Polygon.outerBoundaryIs.LinearRing.coordinates)
If the KML uses MultiGeometry with one or more Polygons then a small change is needed to the check inside the for loop.
for p in pms:
if getattr(p, 'MultiGeometry'):
for poly in p.MultiGeometry.Polygon:
print(poly.outerBoundaryIs.LinearRing.coordinates)
else:
print(p.Polygon.outerBoundaryIs.LinearRing.coordinates)

Related

Method for converting a skeletonized raster array to a polyline using Python 3.7

I am attempting to convert a rasterized line to a polyline. I have skeletonized the raster, but wish to export it as a shapefile (polyline feature) without resorting to ArcGIS. In ArcGIS there is a single tool 'raster to polyline' which completes this task. I've tried a few pythonic approaches, but they all seem to produce polygons rather than a single line feature as observed when running the skeletonizsation tool from skimage (below).
Any suggestions would be much appreciated.
The code I have up to the question raised above is posted below:
rasterClines = rasterpath + ClineRasterName
print(rasterClines)
raster = gdal.Open(rasterClines)
band = raster.GetRasterBand(1)
data = band.ReadAsArray()
final = morphology.skeletonize(data)
plt.figure(figsize=(15,15))
plt.imshow(final, cmap='gray')
#Method for exporting 'final' to .shp file
The plot looks correct, but I just can't find a method to export it.

Python extract part of SVG to PNG

I have been doing a ton of searching but cant quite find the answer to this one.
I have a series of relatively simple SVG images. I have drawn SVG rectangles over key areas of the images that I am interested in and would now like to extract those areas as PNG images. I have no idea the best way to approach this problem.
Idea 1) Convert the whole SVG to PNG then use say PIL to crop the image down after somehow converting the SVG rect coordinates to PNG coordinates. I am starting to work towards this method now, but I am hoping there is a better, and/or easier way to do this!
I am using Python 3.7 for this.
Edit 1:
This is a screen shot of what I am looking at. The original image is SVG, I would like to extract the areas under the green rectangles as PNG images.
Edit 2:
Working from Idea 1, I have the following code that basically sets the viewBox on the SVG image to one of the green rectangles, then sets the width and height of it. From there I am using CairoSVG to export the SVG as PNG.
import cairosvg
import xml.etree.ElementTree as ET
...
with gzip.open(fileObj.filePath,'rb') as file:
svg=file.read()
svg=svg.decode('utf-8')
svgRoot=ET.fromstring(svg)
ET.register_namespace("","http://www.w3.org/2000/svg")
ET.register_namespace('xlink', "http://www.w3.org/1999/xlink")
annots = meta['annots']
for a in annots:
r = ET.fromstring(a['g'])
vb=" ".join([r.get('x'),r.get('y'),r.get('width'),r.get('height')])
svgRoot.set("viewBox",vb)
svgRoot.set("width",'128px')
svgRoot.set("height",'128px')
svg = ET.tostring(svgRoot, encoding="unicode")
cairosvg.svg2png(svg,write_to="/home/test.png")
Unfortunately it is EXTREMELY slow! On the order of more than a minute to extract two PNGs. The SVG files are quite large (2 - 3 mb zipped) and very detailed. I am not certain how CairoSVG works, but does it render everything in the SVG even if it isnt visible before saving the visible part to PNG?
Any advise on optimising or speeding this up would be a huge help.
This worked for me in the end, though it is quite slow on larger SVG images:
import gzip
import cairosvg
import xml.etree.ElementTree as ET
...
with gzip.open(fileObj.filePath,'rb') as file:
svg=file.read()
svg=svg.decode('utf-8')
svgRoot=ET.fromstring(svg)
ET.register_namespace("","http://www.w3.org/2000/svg")
ET.register_namespace('xlink', "http://www.w3.org/1999/xlink")
annots = meta['annots']
for a in annots:
r = ET.fromstring(a['g'])
vb=" ".join([r.get('x'),r.get('y'),r.get('width'),r.get('height')])
svgRoot.set("viewBox",vb)
svgRoot.set("width",'128px')
svgRoot.set("height",'128px')
svg = ET.tostring(svgRoot, encoding="unicode")
cairosvg.svg2png(svg,write_to="/home/test.png")

Networkx - exporting graphml with edge labels, height and width attributes, custom images

I want to automate a network topology diagram using python. I'm new to python so please bear with me. After doing some research I found out that I can use python to create graphml files which can be read by yEd.
I'm learning how to use Networkx to create the graphml files. So far I'm able to create nodes, connect them and add labels to the nodes (these labels would be the hostnames). Now I need to know how I can add labels to the edges (these labels would be the interfaces). For example:
Topology example
If possible I would like to know how to add a custom image for every node (by default the shape is a square but I would like to use a router png file).
If it is not possible then it would be helpful to know how to edit the height and width of the shape and also disabling arrows.
I've reviewed the docs on networkx website but I haven't found how to do these changes directly to the graph object. The only way I've seen it done is when drawing the graph, for example using the following function: nx.draw_networkx_labels(G, pos, labels, font_size=15, arrows=False), but this is not what I need because this is not saved to the graphml file.
If someone can guide me through this it would be really helpful, I'm attaching my code:
import networkx as nx
import matplotlib
import matplotlib.pyplot as plt
g = nx.DiGraph()
g.add_node('Hostname_A')
g.add_node('Hostname_B')
g.add_node('Hostname_C')
g.add_node('Hostname_D')
g.add_edge('Hostname_A','Hostname_B')
g.add_edge('Hostname_A','Hostname_C')
g.add_edge('Hostname_B','Hostname_D')
g.add_edge('Hostname_B','Hostname_C')
for node in g.nodes():
g.node[node]['label'] = node
nx.readwrite.write_graphml(g, "graph.graphml")
This is the solution:
for edge in g.edges():
g.edges[edge]['source'] = 'int gi0/0/0'
g.edges[edge]['destination'] = 'int gi0/0/1'

Read shapefile attributes using talend

I am using the spatial plug-ins for TOS to perform the following task:
I have a dataset with X and Y coordinates. I have also a shapefile with multi polygons and two metadata attributes, name and Id. The idea is to look-up the names in the shapefile with the coordinates. With a point in polygon will be determined which polygon belongs a point to.
I am using the shapefile input component which points to the .shp file.
I am facing to hurdles:
I cannot retrieve the name and Id from the file. I can only see an attribute call the_geom. How can I read the metadata?
The second thing is, the file contains a multi polygon and I don't know how to iterate over it in order to perform a Contains or intersect with the points.
Any comment will be highly appreciated.
thanks for your input #chrki
I managed to solve my tasks in this way:
1) Create a generic schema under metadata:
As the .dbf file was in the same directory of the shapefile Talend automatically recognized the metadata:
2) This is the job overview:
3) I read the shape file using a sShapeFileInput component:
4) The shapefile contains multipolygons and I want to have polygons. My solution was to use a sSimplify component. I used the default settings.
5) The projection of the shapefile was "MGI / Austria Lambert" which corresponds to EPSG 31287. I want to re-project it as EPSG 4326 (GCS_WGS_1984) which is the one used by my input coordinates.
6) I read the x, y coordinates from a csv file.
7) With a s2DPointReplacer I converted the x,y coordinates as Point(x,y) (WKT)
8) Finally I created an expression in a tMap to get only the polygons and points with an intersection. I guess a "contains" would also work:
I hope this helps someone else.
Kind regards,
Paul

Make a visual representation of a graph

I have a list of edges.
(1,2),(1,3),(1,4),(1,5),(1,6),(2,4),(2,7),(3,4),(3,7),(4,5),(4,7),(5,6),(6,7)
How can I get an image of this graph?
It should be automatic, because there are over 9000(not kidding) those lists.
I have always used graphviz for this sort of stuff.
You can draw it with Python and networkx.
import networkx
import pylab
edges = [(1,2),(1,3),(1,4),(1,5),(1,6),(2,4),(2,7),(3,4),(3,7),(4,5),(4,7),(5,6),(6,7)]
G = networkx.Graph(data=edges)
networkx.draw(G)
pylab.show()
You should read pylab's documentation on how to save the graph as an image without using the GUI. You can use ast.literal_eval to parse the original lists. For example, if it stored as one graph on a line in a file, you can do:
with open('edges.txt') as f:
for line in f:
edges = list(ast.literal_eval(line))
# drawing goes here

Resources