How to measure width in KMs with geos - python

I've got a bounding box, and I want to adjust it so it's not less than, say, 2km across. I get the bounding box from google's geolocate service, and then I search for stuff that's inside that in my database, but I want to make sure I don't test against anything smaller than 1 or 2km across.. I'd like to get the area, measure the with, height, and expand pivoting on the center in case it's needed.
I already have all the SRID and stuff right.
Thanks

The answer to this depends on the SRID. You call expand which gets a unitless float as a parameter, and the units on that depend on the units with which the SRID is defined.

Related

How can I fit circles into a shape using python?

So for a project, I gotta make a web-site that fills a shape with circles that wont intersact at any point.The user is going to upload a shape, and also choose the radius of the circles, and the code is going to place as many circles(with the chosen radius) as it can into the shape.
For example, if the user uploads an 16cmx16cm square and chooses 4cm as the radius of the circles, the system is going to place as many circles with a radius of 4cm as possible into the square, and the circles wont intersact at any point.
I tried many things using python and failed eveytime. The shape can be anything, it can be completely random and no matter what the shape is, the site has to find out where to place the circles with the selected radius, place the circles, and show the final shape. I dont know if there is a way to do this without python, but I am open to every suggestion-solution.
You could try the package circle-packing. It looks like you can get the behavior you want by setting the arguments rho_max and rho_min of the class ShapeFill to the radius provided by user. I've not used it so cannot attest to its' correctness or usability. Please let us know if it works for you.
Note: The license is GPLV2 so keep in mind the implications. And don't forget to attribute.
I believe filling it with the actual possible maximum amount would be far from easy, if you actually just want fill it and don't care about the best solution then it's fairly easy.
just start to for the top left corner place a circle, if collides with another circle or the shape it, shift it to the right of an arbitrary small amount and try again. once you reached the end on the right side, move it down and to the left and start the process again.

Move ax.text a bit away from data point

I am trying to create a type of map that plots a route between points.
As such I have something that looks like the image below:
And as you can see, the bll label is very close to the data point. I would like it to be a bit further away, so you can actually see the dot.
Also, the text is just a regular ax.text plot with x and y values.
My problem is, that yes, I could just add some kind of percentage value or something. However, depending on the coordinate and x-value, this will not be the same depending on you being very far left, where x => 0, or far right where x => max value of the plot. Then you could argue I could just add 10 or 20 units, but in my case I produce different maps depending on different routes. So this means that the x-axis values are not the same. Sometimes the map is big, and sometimes it's small. So using the same value in all maps will make bll text move either very much or very little depending on size.
Also, if I am ever to zoom on the map, this would adjust the text as well if I were to use some kind of value extension, since the distance between the data point AND the text will also increase relative to the size of the zoom, like this figure:
Can this be solved in a somewhat simple manner ?
Best regards

Freeze scale transform on a parent object with animated child (MAYA MEL/Python script)

I have hierarchy of objects with animation on translation and rotation, the scale xyz are equal and static but not 1. When I freeze scale on a parent mesh it's children's animation goes wild. Is there any way to prevent this from happening?
I have found a workaround, but it's not perfect yet. Let's say we have simple setup like this:
parentObject=>childObject
I put childObject in a group "childObjectGroup"
parent childObjectGroup to the world and zero out it's transforms excluding scale.
Bake childObject's trasformations to the world so we don't need a group anymore. (found a good script for that)
Freeze scale transforms on parentObject and childObject
Reparent them back
It works for simple hierarchies like that, but not sure how to apply it for more complicated ones with deep tree and several brunches. Probably I'm missing something and there is really simple solution to that.
Any time you change the scale of a parent node, the translation for it's children is going to change - at least, if you're measuring in world space units. So, moving 10 units under a parent scaled to 0.5 will actually move 5 world space units (for example).
I'm pretty sure your rotations should be fine since scale doesn't really change how rotation around a pivot works; however, if you're rotating something from a pivot that is not in the center of the object and you have non-uniform scaling (xyz are not all equal) the rotation inside of the squashed space will feel more like an oval than a circle.
If that's not a problem, the main thing to worry about is the translation positions - you basically need to get the world space positions of each object at each key, 'fix' the scale, then go through the keys and set the world space position again (I would use the xform command for that since you can query and set position with world space values). So, the steps you outlined will probably be the best bet...
If you have non-uniform scales though, you may not actually be able to get the rotations to work out in a way that gives you the same results (just depending on positions/pivots and consecutive descendant positions/pivots). If the parent's scale isn't actually hurting anything and isn't supposed to be keyed/animated, it might be ok to just lock and hide it without any adverse effects.

Snap 3D cursor to an opaque part of plane (blender)

I have a question regarding python scripting in Blender, and I'd really appreciate it if someone could give me at least some conceptual guidelines to how I could do this:
Basically I have around 100 planes (simple primitive planes) and each of them has its own material and each material has it's own transparency map applied to it.
I need a way to snap each of those plane's respective pivots to their opaque parts. I.e. if there is a way to tell the following to blender through python language - "hey, go over every one of these planes, and do the following for each - snap a 3D cursor to an opaque part of the plane (it doesn't matter where exactly, as long as it's inside of an opaque part of the plane) and then snap plane's pivot point to the 3D cursor".
Of course I don't expect anyone to write me a full algorithm for this, I am just asking for a little help and a push in the right direction, as I do have experience with python, but not with blender :/
Any help would be appreciated.
You can find documentation on blender's python api here.
Within blender's image class you can access the pixel data at image.pixels as an array of floats, 4 floats per pixel (RGBA I think). image.size[0] is the width in pixels image.size[1] for height.
Given bpy.data.objects['Plane'].bound_box is an [8][3] array of points defining the outer extremes of the plane, you can locate a point on the plane for the pixel location to get the target point for the origin. You will also find bpy.data.objects['Plane'].matrix_world useful to translate the object coordinates to global.
bpy.context.scene.cursor_location = Vector((x,y,z)) will move the cursor to where you want.
bpy.ops.object.origin_set(type='ORIGIN_CURSOR') will set the active objects origin to the cursor. Note that this works on the active object, so you will need to alter your selection as you go.

Test if point is in some rectangle

I have a large collection of rectangles, all of the same size. I am generating random points that should not fall in these rectangles, so what I wish to do is test if the generated point lies in one of the rectangles, and if it does, generate a new point.
Using R-trees seem to work, but they are really meant for rectangles and not points. I could use a modified version of a R-tree algorithm which works with points too, but I'd rather not reinvent the wheel, if there is already some better solution. I'm not very familiar with data-structures, so maybe there already exists some structure that works for my problem?
In summary, basically what I'm asking is if anyone knows of a good algorithm, that works in Python, that can be used to check if a point lies in any rectangle in a given set of rectangles.
edit: This is in 2D and the rectangles are not rotated.
This Reddit thread addresses your problem:
I have a set of rectangles, and need to determine whether a point is contained within any of them. What are some good data structures to do this, with fast lookup being important?
If your universe is integer, or if the level of precision is well known and is not too high, you can use abelsson's suggestion from the thread, using O(1) lookup using coloring:
As usual you can trade space for
time.. here is a O(1) lookup with very
low constant. init: Create a bitmap
large enough to envelop all rectangles
with sufficient precision, initialize
it to black. Color all pixels
containing any rectangle white. O(1)
lookup: is the point (x,y) white? If
so, a rectangle was hit.
I recommend you go to that post and fully read ModernRonin's answer which is the most accepted one. I pasted it here:
First, the micro problem. You have an
arbitrarily rotated rectangle, and a
point. Is the point inside the
rectangle?
There are many ways to do this. But
the best, I think, is using the 2d
vector cross product. First, make sure
the points of the rectangle are stored
in clockwise order. Then do the vector
cross product with 1) the vector
formed by the two points of the side
and 2) a vector from the first point
of the side to the test point. Check
the sign of the result - positive is
inside (to the right of) the side,
negative is outside. If it's inside
all four sides, it's inside the
rectangle. Or equivalently, if it's
outside any of the sides, it's outside
the rectangle. More explanation here.
This method will take 3 subtracts per
vector * times 2 vectors per side,
plus one cross product per side which
is three multiplies and two adds. 11
flops per side, 44 flops per
rectangle.
If you don't like the cross product,
then you could do something like:
figure out the inscribed and
circumscribed circles for each
rectangle, check if the point inside
the inscribed one. If so, it's in the
rectangle as well. If not, check if
it's outside the circumscribed
rectangle. If so, it's outside the
rectangle as well. If it falls between
the two circles, you're f****d and you
have to check it the hard way.
Finding if a point is inside a circle
in 2d takes two subtractions and two
squarings (= multiplies), and then you
compare distance squared to avoid
having to do a square root. That's 4
flops, times two circles is 8 flops -
but sometimes you still won't know.
Also this assumes that you don't pay
any CPU time to compute the
circumscribed or inscribed circles,
which may or may not be true depending
on how much pre-computation you're
willing to do on your rectangle set.
In any event, it's probably not a
great idea to test the point against
every rectangle, especially if you
have a hundred million of them.
Which brings us to the macro problem.
How to avoid testing the point against
every single rectangle in the set? In
2D, this is probably a quad-tree
problem. In 3d, what generic_handle
said - an octree. Off the top of my
head, I would probably implement it as
a B+ tree. It's tempting to use d = 5,
so that each node can have up to 4
children, since that maps so nicely
onto the quad-tree abstraction. But if
the set of rectangles is too big to
fit into main memory (not very likely
these days), then having nodes the
same size as disk blocks is probably
the way to go.
Watch out for annoying degenerate
cases, like some data set that has ten
thousand nearly identical rectangles
with centers at the same exact point.
:P
Why is this problem important? It's
useful in computer graphics, to check
if a ray intersects a polygon. I.e.,
did that sniper rifle shot you just
made hit the person you were shooting
at? It's also used in real-time map
software, like say GPS units. GPS
tells you the coordinates you're at,
but the map software has to find where
that point is in a huge amount of map
data, and do it several times per
second.
Again, credit to ModernRonin...
For rectangles that are aligned with the axes, you only need two points (four numbers) to identify the rectangle - conventionally, bottom-left and top-right corners. To establish whether a given point (Xtest, Ytest) overlaps with a rectangle (XBL, YBL, XTR, YTR) by testing both:
Xtest >= XBL && Xtest <= XTR
Ytest >= YBL && Ytest <= YTR
Clearly, for a large enough set of points to test, this could be fairly time consuming. The question, then, is how to optimize the testing.
Clearly, one optimization is to establish the minimum and maximum X and Y values for the box surrounding all the rectangles (the bounding box): a swift test on this shows whether there is any need to look further.
Xtest >= Xmin && Xtest <= Xmax
Ytest >= Ymin && Ytest <= Ymax
Depending on how much of the total surface area is covered with rectangles, you might be able to find non-overlapping sub-areas that contain rectangles, and you could then avoid searching those sub-areas that cannot contain a rectangle overlapping the point, again saving comparisons during the search at the cost of pre-computation of suitable data structures. If the set of rectangles is sparse enough, there may be no overlapping, in which case this degenerates into the brute-force search. Equally, if the set of rectangles is so dense that there are no sub-ranges in the bounding box that can be split up without breaking rectangles.
However, you could also arbitrarily break up the bounding area into, say, quarters (half in each direction). You would then use a list of boxes which would include more boxes than in the original set (two or four boxes for each box that overlapped one of the arbitrary boundaries). The advantage of this is that you could then eliminate three of the four quarters from the search, reducing the amount of searching to be done in total - at the expense of auxilliary storage.
So, there are space-time trade-offs, as ever. And pre-computation versus search trade-offs. If you are unlucky, the pre-computation achieves nothing (for example, there are two boxes only, and they don't overlap on either axis). On the other hand, it could achieve considerable search-time benefit.
I suggest you take a look at BSP trees (and possible quadtrees or octrees, links available on that page as well). They are used to partition the whole space recursively and allow you to quickly check for a point which rectangles you need to check at all.
At minimum you just have one huge partition and need to check all rectangles, at maximum your partitions get so small, that they get down to the size of single rectangles. Of course the more fine-grained the partition, the longer you need to walk down the tree in order to find the rectangles you want to check.
However, you can freely decide how many rectangles are suitable to be checked for a point and then create the corresponding structure.
Pay attention to overlapping rectangles though. As the BSP tree needs to be precomputed anyways, you may as well remove overlaps during that time, so you can get clear partitions.
Your R-tree approach is the best approach I know of (that's the approach I would choose over quadtrees, B+ trees, or BSP trees, as R-trees seem convenient to build in your case). Caveat: I'm no expert, even though I remember a few things from my senior year university class of algorithmic!
Why not try this. It seems rather light on both computation and memory.
Consider the projections of all the rectangles onto the base line of your space. Denote that set of line intervals as
{[Rl1, Rr1], [Rl2, Rr2],..., [Rln, Rrn]}, ordered by increasing left coordinates.
Now suppose your point is (x, y), start a search at the left of this set until you reach a line interval that contains the point x.
If none does, your point (x,y) is outside all rectangles.
If some do, say [Rlk, Rrk], ..., [Rlh, Rrh], (k <= h) then just check whether y is within the vertical extent of any of these rectangles.
Done.
Good luck.
John Doner

Categories

Resources