Why I cannot have original geometry in PyMesh after meshing? - pymesh

Sorry it maybe too easy but in PyMesh, when I define a geometry file like this [[1]: https://i.stack.imgur.com/MyuGC.png],by using the simplest example code in Tutorial as follow:
vertices = nodes # The vertex of polygon
tri = pymesh.triangle()
tri.points = vertices
tri.max_area = 0.05
tri.split_boundary = False
tri.verbosity = 0
tri.run(); # Execute triangle
mesh = tri.mesh; # output triangulation
The generated mesh is as follow which has a chamfer, but I want exact geometry.
[[2]: https://i.stack.imgur.com/kPNh1.png]
Please help!
Thank you in advance

Sorry friend according to this link https://github.com/PyMesh/PyMesh/issues/95 I should define segments.
So it solved!

Related

R raster extract polygon ID from xy coordinates of a point

I would like to extract the polygon ID in which a point is located. I have tried multiple solutions but I could not figure out how to do it. Currently my code is the following:
r1<-raster(xmn=-111.4567,
xmx=136.841,
ymn=-94.39192,
ymx=123.5581,
res=50)
r1 = rasterToPolygons(r1)
d = cbind("x" = 7.36459732, "y"= 30.4465762)
extract(r1,d)
EDIT1:
If I get the grid coordinates from the raster, polygon ID of d should be 13 (as d is between -36.45 & 13.54 in x axis and between -1.44 and 48.55)
r1<-raster(xmn=-111.4567,
xmx=136.841,
ymn=-94.39192,
ymx=123.5581,
resolution=50)
cells<-cellFromRowColCombine(r1, 1:dim(r1)[1],1:dim(r1)[2])
coord_grid<-data.frame(xyFromCell(r1,cells),id=cells)
Thanks in advance for your help.
Here the solution I found: cellFromXY(r1, d)

State Transition Diagrams with R

Does anybody know how to creat such a graph?
You can get a pretty good approximation to your graph using the igraph package.
The code below sets up the edges as a data.frame, then turns it into a graph using graph_from_data_frame. You could just plot at that point, but while it would have the same content as your graph, it would not look like your graph. So there are several steps to make it look the way that you asked.
C is the curvature of the edges. I made them all be straight (curvature = 0) except the two between nodes 0 and 3. I did not want these to be on top of each other so I gave them a small curvature.
LO is a layout to arrange the nodes in the same pattern at you did.
The edge.loop.angle parameter is so that the loop from 0 to itself would lay out nicely.
Finally, default color for the nodes is an orange-yellow, so I changed it to white like in your picture.
library(igraph)
from = c(0,0,0,0,1,2,3)
to = c(0,1,2,3,2,3,0)
weight = c(0.1,0.2,0.3,0.4, 1,1,1)
Edges = data.frame(from,to,weight)
g = graph_from_data_frame(Edges)
C = rep(0,7)
C[c(4,7)] = 0.15
LO = matrix(c(0,1,0,1,1,1,0,0), ncol=2)
plot(g, edge.label=E(g)$weight, layout=LO, edge.loop.angle=-pi/2,
vertex.color="white", edge.curved=C)

Calculating the share of isolates in R with igraph

I am currently working on an analysis of networks in R. I have run into something that does not seem right to me. First some context:
I have created a network with the igraph package with 7231 observations of 4 variables, using the following code:
MyNetwork <- data.frame(Katalog_G_2000_2018_VOLLEDIG$Zuwendungsempfänger, Katalog_G_2000_2018_VOLLEDIG$Ausführende.Stelle, Katalog_G_2000_2018_VOLLEDIG$typ, Katalog_G_2000_2018_VOLLEDIG$verbund)
Network <- graph.data.frame(MyNetwork, directed=F)
After this, I visualised the network with the following code:
plot(Network,vertex.size=6, edge.arrow.size=0.4, main= "ICT-Networks in Germany 2000-2018", vertex.label.cex = 0.8,vertex.label=NA,vertex.color = "green")
Now, I would like to find out how many vertices without edges there are in my network (i.e. the share of isolates). For this I tried using this code:
V(Network)[igraph::degree(Network, mode = "out")>0 & igraph::degree(Network, mode = "in") > 0]
length(V(Network)[igraph::degree(Network, mode = 'out')>0 & igraph::degree(Network, mode = 'in') > 0])
This is where the problem arises. Running this code, tells me that 4305/4305 vertices have edges, while the visualisation of my network clearly shows that there are vertices without edges.
Could anyone please tell me how to fix this code to find out what the share of isolates in my network is? Any other solutions to this problem (using different codes for example) would we greatly appreciated as well. If you need any additional information in order to answer my question, please let me know (unfortunately I can't share my data set at this point).
Thanks in advance.
If your main graph is all connected, you can count the vertices without edges this way.
library(igraph)
length(decompose.graph(Network,
mode = c("weak", "strong"),
max.comps = NA,
min.vertices = 0)
) - 1

QGIS Select polygons which intersect points with python

I'm very new to using QGIS, what I have is a points shapefile and a polygon shapefile. I would like to select all the polygons which have at least one point in them. The problem I'm running into is how long this takes. I have 1 million points and about 320,000 polygons, so using spatial query takes far too long. I've heard that I'd need to write a python script with spatial indexing to get a feasibly quick result, but I have no idea how to approach this. Any help would be greatly appreciated.
What I've tried to cobble together from other stack overflow questions is:
pointProvider = self.pointLayer.dataProvider()
all_point = pointProvider.getFeatures()
delta = 0.1
for point in all_point:
searchRectangle = QgsRectangle(point.x() - delta, point.y() - delta, point.x() + delta, point.y() + delta)
candidateIDs = line_index.intesects(searchRectangle)
for candidateID in candidateIDs:
candFeature == rotateProvider.getFeatures(QgsFeatureRequest(candidateID)).next()
if candFeature.geometry().contains(point):
break
This throws up a NameError: name 'self' is not defined
I found an answer over on GIS Stack Exchange, which you can find here
The code I used was:
from qgis.core import *
import processing
layer1 = processing.getObject('MyPointsLayer')
layer2 = processing.getObject('MyPolygonsLayer')
index = QgsSpatialIndex() # Spatial index
for ft in layer1.getFeatures():
index.insertFeature(ft)
selection = [] # This list stores the features which contains at least one point
for feat in layer2.getFeatures():
inGeom = feat.geometry()
idsList = index.intersects(inGeom.boundingBox())
if idsList:
selection.append(feat)
# Select all the polygon features which contains at least one point
layer2.setSelectedFeatures([k.id() for k in selection])

Using R for simple image/pattern-recognition task?

I have an image with many dots, and I would like to extract from it what is the x-y location of each dot.
I already know how to do this manually (there is a package for doing it).
However, is there some way of doing it automatically ?
(My next question will be - is there a a way, when having an image of many lines, to detect where the lines intersect/"touch each other")
Due to requests in the comments, here is an example for an image to "solve" (i.e: extract the data point locations for it)
#riddle 1 (find dots):
plot(cars, pch = 19)
#riddle 2 (find empty center circles):
plot(cars, pch = 1)
#riddle 2 (fine intersection points):
plot(cars, pch = 3)
#riddle 3 (find intersections between lines):
plot(cars, pch = 1, col = "white")
lines(stats::lowess(cars))
abline(v = c(5,10,15,20,25))
Thanks, Tal
(p.s: since I am unfamiliar with this field, I am sorry if I am using the wrong terminology or asking something too simple or complex. Is this OMR?)
The Medical Imaging Task View covers general image provessing, this may be a start.
Following up after Dirk, yes check the medical imaging task view. Also look at Rforge,
Romain Francois has an RJImage package and another image processing package was recently registered. What you are looking for are segmentation algorithms. Your dots problem is much easier than the line problem. The first can be done with an RGB or greyscale filter, just doing some sort of radius search. Detecting linear features is harder. Once you hve the features extracted you can use a sweepline algorithm to detect intersections. EBIimage may have an example for detecting cells in the vignette.
Nicholas
I think you could use package raster to extract xy coordinates from an image with specific values. Have a look at the package vignettes.
EDIT
Can you try this and tell me if it's in the ball park of what you're looking for?
I hope the code with comments is quite self-explanatory. Looking forward to your answer!
library(raster)
rst <- raster(nrows = 100, ncols = 100) #create a 100x100 raster
rst[] <- round(runif(ncell(rst))) #populate raster with values, for simplicity we round them to 0 and 1
par(mfrow=c(1,2))
plot(rst) #see what you've got so far
rst.vals <- getValues(rst) #extract values from rst object
rst.cell.vals <- which(rst.vals == 1) #see which cells are 1
coords <- xyFromCell(rst, rst.cell.vals) #get coordinates of ones
rst[rst.cell.vals] <- NA #set those raster cells that are 1 to NA (you can play with rst[!rst.cell.vals] <- NA to exclude all others)
plot(rst) #a diag plot, should have only one color

Resources