Read shapefile attributes using talend - geospatial

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

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.

Creating a surface plot from an Unstructured grid vtk file using Vedo

I have an unstructured grid vtk file that contains three different types of cells (Tetrahedral, Wedge and Hexahedral). This file contains multiple Scalars (8 attributes such as Pressure, Temperature e.t.c.) and a Single Vector (U,V,W) and I am trying to create a surface plot from this file for a Scalar or Vector at a time using the Vedo python wrapper for vtk. The vtk file contains a scalar or vector value for each cell, including the point coordinates.
I have read the documentation over and over, with examples here https://vtkplotter.embl.es/content/vtkplotter/index.html. These are the things that I have tried with the challenge that I am having with each method:
Method 1: Loading the file as a TetMesh
vp = Plotter()
test = load('Case_60.vtk')
vp.show(test)
This method doesn't plot Scalar Values and only shows points. No Solid Surface. Tried using a cuttertool() with it , it throws an error saying non-Tetrahedral Cell Encountered.
Method 2: Using the UGrid
ug = UGrid('Case_60.vtk')
show(ug)
This method plots as surface with a solid color. Does not seem to be picking the Scalars.
What is the proper way for me to display surface plot and display the scalar value for each cell? Is Vedo able to do what I'm trying to do?
You might need to specify which array is to be used for coloring, e.g.:
from vedo import *
ug = UGrid(datadir+'limb_ugrid.vtk')
print(ug.getArrayNames())
ug.selectCellArray('chem_0')
show(ug, axes=True)
if this doesn't work for your mesh please submit an issue here.

How to modify orientation of mgh/dicom/nifti file using nibabel

I have a hard time, figuring out a proper affine transformation for 3 different views i.e. coronal, axial and saggital, each having separate issues like below:
1: Axial color map get overlapped with the saggital original view.
2: Similarly Sagittal color map gets overlapped with the axial original image.
3: And everyone has some kind of orientation issues like best visible here when the color map and original image for coronal come correct but with wrong orientation.
I am saving the original file that I am sending to the server for some kind of prediction, which generates a color map and returns that file for visualization, later I am displaying everything together.
In server after prediction, here is the code to save the file.
nifti_img = nib.MGHImage(idx, affine, header=header)
Whereas affine and header are the original affine and header extracted from the file I sent.
I need to process "idx" value that holds the raw data in Numpy array format, but not sure what exactly to be done. Need help here.
Was trying hard to solve the issue using nibabel python library, but due to very limited knowledge of mine about how these files work and about affine transformation, I am having a hard time figuring out what should I do to make them correct.
I am using AMI js with threejs support in the frontend and nibabel with python in the back end. Solution on the frontend or back end anywhere is acceptable.
Please help. Thanks in advance.
img = nib.load(img_path)
# check the orientation you wanna reorient.
# For example, the original orientation of img is RPI,
# you wanna reorient it to RAS, the second the third axes should be flipped
# ornt[P, 1] is flip of axis N, where 1 means no flip and -1 means flip.
ornt = np.array([[0, 1],
[1, -1],
[2, -1]])
img_orient = img.as_reoriented(ornt)
nib.save(img_orient, img_path)
It was simple, using numpy.moveaxis() and numpy.flip() operation on rawdata from nibabel. as below.
# Getting raw data back to process for better orienation and label mapping.
orig_img_data = nib.MGHImage(numpy_arr, affine)
nifti_img = nib.MGHImage(segmented_arr_output, affine)
# Getting original and predicted data to preprocess to original shape and view for visualisation.
orig_img = orig_img_data.get_fdata()
seg_img = nifti_img.get_fdata()
# Placing proper views in proper place and flipping it for a better visualisation as required.
# moveaxis to get original order.
orig_img_ = np.moveaxis(orig_img, -1, 0)
seg_img = np.moveaxis(seg_img, -1, 0)
# Flip axis to overcome mirror image/ flipped view.
orig_img_ = np.flip(orig_img_, 2)
seg_img = np.flip(seg_img, 2)
orig_img_data_ = nib.MGHImage(orig_img_.astype(np.uint8), np.eye(4), header)
nifti_img_ = nib.MGHImage(seg_img.astype(np.uint8), np.eye(4), header)
Note: It's very important to have same affine matrix to wrap both of these array back. A 4*4 Identity matrix is better rather than using original affine matrix as that was creating problem for me.

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'

Basic importing coordinates into R and setting projection

Ok, I am trying to upload a .csv file, get it into a spatial points data frame and set the projection system to WGS 84. I then want to determine the distance between each point This is what I have come up with but I
cluster<-read.csv(file = "cluster.csv", stringsAsFactors=FALSE)
coordinates(cluster)<- ~Latitude+Longitude
cluster<-CRS("+proj=longlat +datum=WGS84")
d<-dist2Line(cluster)
This returns an error that says
Error in .pointsToMatrix(p) :
points should be vectors of length 2, matrices with 2 columns, or inheriting from a SpatialPoints* object
But this isn't working and I will be honest that I don't fully comprehend importing and manipulating spatial data in R. Any help would be great. Thanks
I was able to determine the issue I was running into. With WGS 84, the longitude comes before the latitude. This is just backwards from how all the GPS data I download is formatted (e.g. lat-long). Hope this helps anyone else who runs into this issue!
thus the code should have been
cluster<-read.csv(file = "cluster.csv", stringsAsFactors=FALSE)
coordinates(cluster)<- ~Longitude+Latitude
cluster<-CRS("+proj=longlat +datum=WGS84")

Resources