How to use GDAL's ogr2ogr to create bounding box - geojson

I'm learning about GeoJSON and following Bostock's Lets Make a Map tutorial. Here he uses Natural Earth data, and converts SHP files to JSON for use in D3. The following takes only GBR and IRL data from the SHP file to create subunits.json, which I load into D3 to draw my map.
> ogr2ogr \
-f GeoJSON \
-where "ADM0_A3 IN ('GBR', 'IRL')" \
subunits.json \
ne_10m_admin_0_map_subunits.shp
ogr2ogr has many more powerful
features...The -clipdst argument, for
example, clips the shapefile to a rectangular bounding box, useful for
displaying only a small part of certain features.
I'd like to create a bounding box for displaying only a small area such as the area around Birmingham. How do I calculate the xmin ymin etc for this? This is the usage [-clipdst [xmin ymin xmax ymax]|WKT|datasource] but what exactly should it look like?

There's an example of the clipdst syntax at the bottom of the page you shared:
Example reprojecting from ETRS_1989_LAEA_52N_10E to EPSG:4326 and clipping to a bounding box
ogr2ogr -wrapdateline -t_srs EPSG:4326 -clipdst -5 40 15 55 france_4326.shp europe_laea.shp
http://www.gdal.org/ogr2ogr.html#ogr2ogr_example

Related

How to only buffer outside of the polygon and leave the shared edge un-buffered?

What I am trying to achieve is to buffer a set of connecting polygons while leaving the shared edges un-buffered.
After some searching, ArcGIS's buffer wizards + dissolve does exactly that but unfortunately without an Arcgis license I am searching for a solution that uses QGIS or other platforms such as PostGIS.
The image below shows the result using ArcGIS's buffer wizards + dissolve
Desired Result:
The main challenge is to cut the overlaps from two adjacent buffers into two disjoint polygons along a line equidistant to the unbuffered polygons.
Adding an modified image from JFK to elaborate on my desired result with an example, black shaded polygons are the original polygon, and polygon A and B are the seperate buffered polygons that has three buffered sides, and one un-buffered side (the side where A, B touches)
The doc says the wizard starts by dissolving the polygons, then a buffer is applied on the output.
You can do the same in PostGIS: st_collect aggregates the geometries together, creating a multi-polygon if geometries are disjoint. st_buffer does the buffering (in CRS unit), st_dump will explode the single multi-part buffer into individual polygons.
WITH src(geom) AS (values
('POLYGON((0 0,0 10,10 10, 10 0, 0 0))'::geometry),
('POLYGON((0 0,0 10,-10 10, -10 0, 0 0))'::geometry),
('POLYGON((20 20,20 30,30 30, 30 20, 20 20))'::geometry))
SELECT st_asText(
(st_dump(
st_buffer(
st_collect(geom),
1)
)).geom)
FROM src;
---------
POLYGON((20 19,19.8049096779839 19.0192147195968,19.6173165676349 19.0761204674887,19.4444297669804 19.1685303876975,19.2928932188135 19.2928932188135,19.1685303876975 19.4444297669804,19.0761204674887 19.6173165676349,19.0192147195968 19.8049096779839,19 20,19 30,19.0192147195968 30.1950903220161,19.0761204674887 30.3826834323651,19.1685303876975 30.5555702330196,19.2928932188135 30.7071067811865,19.4444297669804 30.8314696123025,19.6173165676349 30.9238795325113,19.8049096779839 30.9807852804032,20 31,30 31,30.1950903220161 30.9807852804032,30.3826834323651 30.9238795325113,30.5555702330196 30.8314696123025,30.7071067811865 30.7071067811865,30.8314696123025 30.5555702330196,30.9238795325113 30.3826834323651,30.9807852804032 30.1950903220161,31 30,31 20,30.9807852804032 19.8049096779839,30.9238795325113 19.6173165676349,30.8314696123025 19.4444297669804,30.7071067811865 19.2928932188135,30.5555702330196 19.1685303876975,30.3826834323651 19.0761204674887,30.1950903220161 19.0192147195968,30 19,20 19))
POLYGON((0 -1,-10 -1,-10.1950903220161 -0.980785280403231,-10.3826834323651 -0.923879532511287,-10.5555702330196 -0.831469612302547,-10.7071067811865 -0.70710678118655,-10.8314696123025 -0.555570233019605,-10.9238795325113 -0.382683432365094,-10.9807852804032 -0.195090322016134,-11 0,-11 10,-10.9807852804032 10.1950903220161,-10.9238795325113 10.3826834323651,-10.8314696123025 10.5555702330196,-10.7071067811865 10.7071067811865,-10.5555702330196 10.8314696123025,-10.3826834323651 10.9238795325113,-10.1950903220161 10.9807852804032,-10 11,0 11,10 11,10.1950903220161 10.9807852804032,10.3826834323651 10.9238795325113,10.5555702330196 10.8314696123025,10.7071067811865 10.7071067811865,10.8314696123025 10.5555702330196,10.9238795325113 10.3826834323651,10.9807852804032 10.1950903220161,11 10,11 0,10.9807852804032 -0.195090322016128,10.9238795325113 -0.38268343236509,10.8314696123025 -0.555570233019602,10.7071067811865 -0.707106781186547,10.5555702330196 -0.831469612302545,10.3826834323651 -0.923879532511287,10.1950903220161 -0.98078528040323,10 -1,0 -1))
(2 rows)

How to plot correct pdf from embed image dwg

Please tell me how to plot correct pdf from embed image dwg.
I am evaluating Design Automation plot from the dwg with enbed image.
I recieved black filled dwg on below condition.
Dasign Automation API version:v2
AppPackage:PlotToPDF
DWG:rasterTest.dwg
AutoCAD version: 23.0(P.45.Z.36)
Also I tried my original plot apppackage,
and the result was the pdf filled black .
The default PlotToPDF activity is defined as follows:
_tilemode 0 -export _pdf _all result.pdf\n
This means it only plot paperspace. This drawing has no initialized paperspace layouts. You can do 2 things.
Use a different script that plots the current space.
AutoCAD PDF (General Documentation).pc3
e:\test.pdf
_N
_Y
Resave the drawing so that it has an initialized layout.

How to find sizes and shapes of Microsoft Powerpoint objects?

I have a slide with some hand-drawn circles on it. I'd like to get a list of the coordinates and radii (sizes) of them. Attached is an image and link. Anyone have an idea how?
I started looking into computer vision techniques, but it seems like there should be a much more direct way.
If you are familiar with openCV the method HoughCircles() will do the job:
http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html
Are you familiar with Matlab? imfindcircles() will do it:
http://www.mathworks.com/help/images/ref/imfindcircles.html
If this is a one time job you can post it as a job for someone else to do it for you for a small fee. Example: https://www.mturk.com/mturk/welcome
If you don't know any programming language and this is a one time job, you can do it manually. You can select each circle in photoshop, count the amount of pixels (and using the formulae of circumference = 2*pi*radius) find the radius. The center of mass of all the pixels will be the center of the circle.
It is a bit tricky to separate overlapping circles but you can do it by hand
I found a suitable method using vector graphics.
Select all the circles in powerpoint, right click and 'save as a picture'. Use .emf (windows metafile) format (this option was only available on my windows machine, not mac).
Open the emf file in inkscape, and save it to an 'svg' format, which is ascii and human readable.
Extract the information from the path commands.
E.g.: Each circle is represented as a path object, with a line:
d="m 36.527169,36.434607 c 0,-9.696733 9.075703,-17.551993 20.274845,-17.551993 11.194626,0 20.270329,7.85526 20.270329,17.551993 0,9.69264 -9.075703,17.552246 -20.270329,17.552246 -11.199142,0 -20.274845,-7.859606 -20.274845,-17.552246"
Here, the (x,y) following the 'm' character is the center of the circle, and the 12 (x,y) pairs following 'c' denote a 4-segment polybezier curve in which pairs 3,6,9,12 are the four compass points. Therefore in the above object, this is not a circle but an ellipse with axes ~ 20.27 and 17.55.

when drawing a filled circle, gnuplot gives different output between x11 terminal and latex terminal, why?

I switched from tikz to gnuplot for drawing math diagrams recently. I find them very different.
I want to draw a circle, so I created a .gpi file:
set terminal latex
set out 'gp.tex'
set xrange [-5:5]
set yrange [-5:5]
set object 1 circle at 0,0 size char 1 fillcolor rgb "black" fillstyle solid
plot NaN
set out
set terminal x11
plot NaN
And I loaded it in gnuplot.
The circle in x11 terminal is filled, as expected:
http://i.imgur.com/xDmlTa4.png
But the one compiled from gp.tex is a hollow circle:
http://i.imgur.com/7LNzvmW.png
Why? How can I produce a filled circle in the tex file as well?
The latex terminal is very old and doesn't support filled circles. You should use one of the other LaTeX-related terminals like epslatex, cairolatex or tikz which support filled circles.
See the output of the test command to see which features are supported by a terminal. For filled circles, filled polygons must be supported. For latex you get:

Remove shapes from image with X number of pixels or less

If I have a image with, let's say squares. Is it possible to remove all shapes formed by 10 (non white) pixels or less and keep all shapes that is formed by 11 pixels or more? I want to do it programmatically or with a command line.
Thanks in advance!
Possibly an algorithm called Erosion may be useful. It works on boolean images, shrinking all areas of "true" removing one layer of their surface pixels. Apply a few times, and small areas disappear, bigger ones remain (though shrunken). De-shrink the survivors with the opposite algorithm, dilation (apply erosion to the logical complement of the image). Find a way to define a boolean images by testing if a pixel is inside an "object" however you define it, and find a way to apply the results to the original image to change the unwanted small objects to the background color.
To be more specific would require seeing examples.
Look up flood fill algorithms and alter them to count the pixels instead of filling. Then if the shape is small enough, fill it with white.
There are a couple of ways to approach this. What you are referring to is commonly called Despeckle in Document Imaging Applications. Document scanners often introduce a lot of dirt and noise into an image during scanning and so this must be removed removed to help improve OCR accuracy.
I assume you are processing B/W images here or can convert your image to B/W otherwise it becomes a lot more complex. Despeckle is done by analysing all the blobs on the page. Another way to decide on blob size is to decide on width, height and number of pixels combined.
Leptonica.com - Is an Open Source C based library that has the blob analysis functions you require. With some simple check and loops you can delete these smaller objects. Leptonica can also be compiled quite easily into a command line program. There are many example programs and that is the best way to learn Leptionica.
For testing, you may want to try ImageMagick. It has a command line option for despeckle but it has no further parameters.
http://www.imagemagick.org/script/command-line-options.php#despeckle
The other option is to look for "despeckle" algorithms in Google.
ImageMagick, starting from version 6.8.9-10, includes a -connected-components option which can be used to do what you want, however from the example provided in the official website, it is not immediately obvious how to actually obtain the original image minus the removed connected components.
I'm almost sure there is a simpler way, but I did it via a clunky script performing a series of steps:
First, I ran the command from the connected components example:
convert in.png \
-define connected-components:verbose=true \
-connected-components 8 out.png
This produces output in the following format:
Objects (id: bounding-box centroid area mean-color):
(...)
181: 9x9+1601+916 1605.2,920.2 44 gray(0)
185: 5x5+1266+923 1268.0,925.0 13 gray(0)
274: 5x5+2276+1661 2278.0,1663.0 13 gray(255)
Then, I used awk to filter only the lines containing an area (in pixels) of black components (mean-color gray(0) in my image) smaller than my threshold $min_cc_area. Note that connected-components has an option to filter components smaller than a given area, but I needed the opposite. The awk line is similar to the following:
{if ($4 < $min_cc_area && $5=="gray(0)") { print $2 }}
I then proceeded to create a command-line for ImageMagick where I drew white rectangles on top of these connected components. The -draw command expects coordinates in the form x1,y1 x2,y2, so I used awk again to compute the coordinates from the ones in the format [w]x[h]+x1+y1 given by -connected-components:
awk '{print "white fill rectangle " $3 "," $4 " " $3+$1-1 "," $4+$2-1 }'
Finally, I ran the created ImageMagick command-line to create a new image combining all the white rectangles on top of the original one.
In the end, I got the following script:
# usage: $0 infile min_cc_area outfile
infile=$1
min_cc_area=$2
outfile=$3
awk_exp="{if (\$4 < $min_cc_area && \$5==\"gray(0)\") { print \$2 }}"
draw_rects=""
draw_rects+=$(convert $infile -define connected-components:verbose=true \
-connected-components 8 null: | \
awk "$awk_exp" | tr 'x+' ' ' | \
awk '{print " rectangle " $3 "," $4 " " $3+$1-1 "," $4+$2-1 }')
convert $infile -draw "fill white $draw_rects" $outfile
Note that this solution may erase black pixels near the removed CC's, if they insersect the bounding rectangle of the removed component.
You want a connected components labeling algorithm. It will scan through the image and give every connected shape an id number, as well as assign every pixel an id number of what shape it belongs to.
After running a connected components filter, just count the pixels assigned to each object, find the objects that have less than 10 pixels, and replace the pixels in those objects with white.
If you can use openCV, this piece of code does what you want (i.e., despakle). You can play w/ parameters of Size(3,3) in the first line to get rid of bigger or smaller noisy artifacts.
Mat element = getStructuringElement(MORPH_ELLIPSE, Size(3,3));
morphologyEx(image, image, MORPH_OPEN, element);
morphologyEx(image, image, MORPH_CLOSE, element);
You just want to figure out the area of each components. So an 8-direction tracking algorithm could help. I have an API solve this problem coded in C++. If you want, send me an email.

Resources