Buffer country borders with st_buffer and a SpatialPolygonsDataFrame? - buffer

I'm using coordinate_cleaner's country test cc_coun but it's flagging entries with coordinates near to the edges country borders. To try to keep them I wanted to buffer the terrestrial area of countries, essentially expanding their borders a little so that it doesn't flag these entries.
I've tried:
require(rnaturalearth)
world <- ne_countries(scale = "large", returnclass = "sf") %>% st_buffer(dist=.001)
Using st_buffer(dist=.001) does change the geometry, but I noticed whatever I put into dist doesn't matter as it changes it to the same thing regardless (I tried .001, 0.1, 1, 2, and -1, though any minus number removes the geometry altogether).
I found that maybe I need to transform my sf file into something else and then use st_buffer so it's in meters rather than degrees(?). I saw this question/answer but I don't understand it well enough to help my situation: What unit is the `dist` argument in `st_buffer` set to by default?
In the end I'm looking to create a SpatialPolygonsDataFrame reference file to feed into cc_coun. Using the above code I followed on with:
world <- sf:::as_Spatial(world)
coun_test <- cc_coun(x = data,
lon = "Decimal_Long",
lat = "Decimal_Lat",
iso3 = "Country_code",
value = "flagged",
ref = world,
verbose = TRUE)
Which ended up flagging more entries than when I didn't use st_buffer on the reference fine.
In summary, I want to add a buffer to the edge of every country border by around 100 meters in a file I can use as a reference in this test. Is this the best way to go about it or is there a better/easier way? I'd appreciate any advice.
Thank you

Related

polyline.encode strange format

I am working with the polyline of google, I would like to give a set of coordinates and generate the correct polyline and viceversa. In particular in the end i would like to url encode the result (the polyline).
When I insert a polyline like:
code = '%28%28akntGkozv%40kcCka%40us%40y%7BDfvAm%7BBnuCj_Aus%40fzG%29%29'
I use the polyline package: https://pypi.org/project/polyline/, and first I decode the polyline in order to see the coordinates:
coordinates = polyline.decode(code)
print(coordinates)
>> [(3e-05, -0.0001), (-0.0001, -7e-05), (-0.0002, -0.0002), (45.46221, 35.36626), (45.4621, 35.36617), (45.48328, 35.39727), (45.48317, 35.39718), (45.5172, 35.39707), (45.51711, 35.39816), (45.51723, 35.39814), (45.5172, 35.38418), (45.51823, 35.3843), (45.51821, 35.38428), (45.49413, 35.37398), (45.52816, 35.37387), (45.52807, 35.32855), (45.5281, 35.32845), (45.52823, 35.32848), (45.52813, 35.32861)]
and everything here is fine, the problems comes when I try to encode the coordinates back to the polyline (which is my ultimate goal since in the end i would like to give some coordinates and obtain the corresponding polyline)
new_code = polyline.encode(coordinates)
print(new_code)
>> ERXERXakntGkozvETPkcCkaETPusETPyEWBDfvAmEWBBnuCj_AusETPfzGERYERY
Which is slightly different from the original and if put back in the url it doesnt work!
So my question here are:
what kind of encoding is new_code? I have tried to encode it in percentage url using urllib.parse.quote(new_code) but the result is exactly the same, maybe I neeed to specify some particular encoding style but i didnt found anything.
The polyline that I used is a square inside the city of Milan (so only 4 points, maximum 5, are required to identify this area), but the coordinates results from the polyline.decode gives me back a list with 19 points with coordinates that are not even close to the city of Milan. Why?
Ok so basically all of my problems came from the fact that the string i was considering: %28%28akntGkozv%40kcCka%40us%40y%7BDfvAm%7BBnuCj_Aus%40fzG%29%29
contains %28%28 and %29%29 which are not part of the polyline but are simply two (( and )) inserted by the particular url of the site I was using. A simple replace and an encode return the correct polyline:
code = '%28%28akntGkozv%40kcCka%40us%40y%7BDfvAm%7BBnuCj_Aus%40fzG%29%29'
code = code.replace('%28', '').replace('%29', '')
code = urllib.parse.unquote(code)
print(code)
>> irotG_hzv#woBmE}i#yjE`oBwkDf|ChRhMzeG}~BxcB
Which infact, if put inside the polyline.decode returns exactly the coordinates that I have used:
coordinates = polyline.decode(code)
print(coordinates)
>> [(45.46869, 9.15088), (45.48673, 9.15191), (45.4936, 9.18452), (45.47567, 9.21216), (45.45051, 9.20907), (45.44822, 9.16701), (45.46869, 9.15088)]
Which are exactly 7 (now i have changed the shape so a sixtagon instead of a square) and points exactly in the city of Milan

Creating Treemap with distinct colors using hctreemap2 in R

I am trying to produce an interactive treemap in R using the highcharter-package (I love the package btw).
It shall look like this (I don't even need the different levels)
https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/treemap-with-levels/
Sample code:
df <- data.frame(name = c("john", "jane", "herbert", "peter"),
bananas = c(10, 14, 6, 3))
hctreemap2(df,
group_vars = "name",
size_var = "bananas")
I don't want the boxes to have gradient colors, but distinct, say red, yellow, green and blue.
I'm getting better in understanding the highcharts-API and "translate" it to R-Code, but this really gives me a hard time.
I already found a work-arround, but I am looking for a better solution, as hc_add_series_treemap is deprecated.
p <- treemap(df,
index="name",
vSize="bananas",
type="index")
highchart() %>%
hc_add_series_treemap(p,
layoutAlgorithm = "squarified")
So thanks for your help :)
After spending an entire morning (I need this at my job) without finding a solution, coming to the end of finally posting this question here, only 10 minutes after asking it, I found a solution myself, trying something that just popped up in my head ^^
So here it goes:
hctreemap2(df,
group_vars = "name",
size_var = "bananas") %>%
hc_plotOptions(treemap = list(colorByPoint = TRUE)) %>% #allows points in the same serie to have different colors
hc_colors(c("#FFFF00", "#FF0000", "#0000FF", "#00AA00")) %>% #with this we can set the colors, note: 1st color is given to first row in the data frame (not necessarily the biggest box)
hc_colorAxis(dataClasses = color_classes(df$name)) %>% #defines acc. to which variable, a box gets a distinct color
hc_legend(enabled = FALSE) #suppresses legend
It gives this:
We need three additional functions of the highcharter-package, but it works quite well!

How to get the bounding box from a Revit Element with Revit API, then call to center of that bounding box

I am trying to rotate Revit elements about their center points. In order to do that, I need to select a Revit element and find its center point, then create a line with the coordinates at that elements center point.
My best idea to accomplish this is to wrap a Revit element in a bounding box and then find the center of that box. My problem is that I am unsure how to accomplish this.
I am using pyRevit (amazing tool) and I am stuck on how to either wrap the selected element with a bounding box or retrieve its existing bounding box.
Any help would be greatly appreciated! I am really trying to learn the Revit API and understand how everything works. I am making progress but there is a lot to unpack.
def pickobject():
from Autodesk.Revit.UI.Selection import ObjectType
#define the active Revit application and document
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
#define a transaction variable and describe the transaction
t = Transaction(doc, 'This is my new transaction')
# Begin new transaction
t.Start()
# Select an element in Revit
picked = uidoc.Selection.PickObject(ObjectType.Element, "Select something.")
### ?????????? ###
# Get bounding box of selected element.
picked_bb = BoundingBoxXYZ(picked)
# Get max and min points of bounding box.
picked_bb_max = picked_bb.Max
picked_bb_min = picked_bb.Min
# Get center point between max and min points of bounding box.
picked_bb_center = (picked_bb_max + picked_bb_min) / 2
### ?????????? ###
# Close the transaction
t.Commit()
return picked, picked_bb_center
Thanks in advance for taking a look at what I have so far. Please let me know if anything needs further clarification!
edit:
#CyrilWaechter
I think you are right. Using LocationPoint would probably make more sense. I looked through the script you linked (thank you btw!) and I tried implementing this section in my code.
transform = doc.GetElement(picked.ElementId).GetTransform()
I am passing the ElementId through this statement but I get the error, "Wall" object has no attribute 'GetTransform'. Could you please help me understand this?
edit 2:
Thanks #JeremyTammik and #CyrilWaechter, your insights helped me understand where I was going wrong. While I still feel that certain properties are ambiguous in the Revit API, I was able to get my code to execute properly. I will post the code that I was able to get working below.
The centre of the bounding box is very easy to obtain. picked is a Reference. Get the ElementId from that, open it using doc.GetElement, and retrieve the bounding box using get_BoundingBox, cf. Conduits Intersecting a Junction Box
:
Element e = Util.SelectSingleElement(
uidoc, "a junction box" );
BoundingBoxXYZ bb = e.get_BoundingBox( null );
For certain elements and certain irregular shapes, you might want to use the centroid instead of the bounding box:
Solid Centroid and Volume Calculation
GetCentroid on GitHub
Edited and preserved for posterity by The Building Coder:
Python Rotate Picked Around Bounding Box Centre
Many thanks to Christian for the interesting discussion and Cyril for the wealth of additional information he provides!
Here is how I was able to solve my problem using pyRevit. This code allows you to rotate an element about its Z axis from the center of its bounding box.
To use this code, select a single Revit element and then open the Revit Python Shell. Copy and paste the code below into the Revit Python Shell notepad and click the run button. This will rotate the element by 45 degrees because the current rotateSelectedElement() argument is 45. You may change this number to any value before running.
# Import the math module to convert user input degrees to radians.
import math
# Get a list of all user selected objects in the Revit Document.
selection = [doc.GetElement(x) for x in uidoc.Selection.GetElementIds()]
# Definitions
def rotateSelectedElement(degrees_to_rotate):
from Autodesk.Revit.UI.Selection import ObjectType
#define the active Revit application and document
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
#define a transaction variable and describe the transaction
t = Transaction(doc, 'This is my new transaction')
# Convert the user input from degrees to radians.
converted_value = float(degrees_to_rotate) * (math.pi / 180.0)
# Begin new transaction
t.Start()
# Get the first selected element from the current Revit doc.
el = selection[0].Id
# Get the element from the selected element reference
el_ID = doc.GetElement(el)
# Get the Bounding Box of the selected element.
el_bb = el_ID.get_BoundingBox(doc.ActiveView)
# Get the min and max values of the elements bounding box.
el_bb_max = el_bb.Max
el_bb_min = el_bb.Min
# Get the center of the selected elements bounding box.
el_bb_center = (el_bb_max + el_bb_min) / 2
#Create a line to use as a vector using the center location of the bounding box.
p1 = XYZ(el_bb_center[0], el_bb_center[1], 0)
p2 = XYZ(el_bb_center[0], el_bb_center[1], 1)
myLine = Line.CreateBound(p1, p2)
# Rotate the selected element.
ElementTransformUtils.RotateElement(doc, el, myLine, converted_value)
# Close the transaction
t.Commit()
# Execute
# Add the desired degrees to rotate by as an argument for rotateSelectedElement()
rotateSelectedElement(45)
edit: Made code clearer. Code now executes in Revit Python Shell without any further modifications. Refer to directions above if you have trouble!

Is there any way to resize a Surface by any natural number value in ROBLOX?

I've been working with the built-in Resize function in Roblox Studio and have been using it to expand the Top Surface of multiple Parts in order to form a wall-like structure.
The only problem that has arisen when using this method is that the surface of the wall created is not even: Some Parts are higher than others.
I later discovered that this problem is due to the fact that the built-in Resize function only takes integers as it's second parameter (or "expand-by" value). Ideally I need the Parts to have the ability expand by any Real Number.
Are there any alternatives to the built-in Resize function that allow one to resize a Surface by any Real Number?
Yes, this is possible, but it actually requires a custom function to do so. With some fairly basic math we can write a simple function to accomplish such a task:
local Resize
do
local directions = {
[Enum.NormalId.Top] = {Scale=Vector3.new(0,1,0),Position=Vector3.new(0,1,0)},
[Enum.NormalId.Bottom] = {Scale=Vector3.new(0,1,0),Position=Vector3.new(0,-1,0)},
[Enum.NormalId.Right] = {Scale=Vector3.new(1,0,0),Position=Vector3.new(1,0,0)},
[Enum.NormalId.Left] = {Scale=Vector3.new(1,0,0),Position=Vector3.new(-1,0,0)},
[Enum.NormalId.Front] = {Scale=Vector3.new(0,0,1),Position=Vector3.new(0,0,1)},
[Enum.NormalId.Back] = {Scale=Vector3.new(0,0,1),Position=Vector3.new(0,0,-1)},
}
function Resize(p, d, n, c)
local prop = c and 'Position' or 'CFrame'
p.Size = p.Size + directions[d].Scale*n
p[prop] = p[prop] + directions[d].Position*(n/2)
return p.Size, p[prop]
end
end
Resize(workspace.Part, Enum.NormalId.Bottom, 10, false) --Resize workspace.Part downards by 10 studs, ignoring collisions
If you're interested more on how and why this code works the way it does, here's a link to a pastebin that's loaded with comments, which I felt would be rather ugly for the answer here: http://pastebin.com/LYKDWZnt

Gain information about objects in an image

I have a binary image which contains several separated regions. I want to put a threshold on the Area (number of pixels) that these regions occupy, in the way that: a region would be omitted if it has fewer pixels than the threshold. I already have tried these codes (using bwconncomp):
[...]
% let's assume threshold = 50
CC = bwconncomp(my_image);
L = labelmatrix(CC);
A = cell( size(CC.PixelIdxList,1) , size(CC.PixelIdxList,2) );
A = CC.PixelIdxList;
for column = 1 : size(CC.PixelIdxList,2)
if numel(CC.PixelIdxList{column}) < 50, A{column} = 0;
end
end
But at this point I don't know how to convert cell C back to the shape of my image and then show it! Are there any tricks to do that?
Is there any easier and straighter way to gain information about objects in an image than this one I used in here?
I also need to know length and width of these objects. These objects do not necessarily have any specific geometrical shape!
Thanks
Since no one took the effort to answer my question in here, I found it somewhere else. Now I'm coppying it to here, just in case if anyone novice like me might need to know that.
In order to know length and width of objects in an image:
labeledImage = bwlabel(my_image, 8);
regioninfo = regionprops(labeledImage , 'MajorAxisLength', 'MinorAxisLength');
lengths = [regioninfo.MajorAxisLength]; %array
widths = [regioninfo.MinorAxisLength]; %array

Resources