# Count objects in polygons

I have a .txt file containing the coordinate of the two polygons in the picture below as well as coordinates for each of the lines inside the polygons.

Could anyone help to count each line inside each polygon? I know how to calculate the polygon area, and the length of the lines. But I do not know how to "assign" the different line to their respective polygons.

Im using R.

Here is an example of part of the .txt. They x,y coordinates in pixels i.e. x y x y x y x y = a polyline. Or a polygon if they form a closed circle.

This is a line:

``````1710 285
1710 286
1711 286
1725 304
1725 304
``````

This is a polygon:

``````1608 308
1605 308
1605 308
1603 308
1602 309
1601 309
1598 309
1595 310
1593 310
1591 312
1589 312
1586 313
1585 314
1583 315
1581 317
1581 319
1579 320
1579 321
1578 324
1576 327
1574 329
1573 332
1571 334
1569 335
1569 336
1568 338
1566 340
1564 343
1564 344
1563 345
1561 346
1560 348
1557 351
1557 353
1555 356
1554 359
1553 361
1552 364
1551 365
1551 366
1550 367
1549 369
1549 371
1549 373
1549 375
1549 378
1549 380
1549 381
1549 383
1550 386
1550 389
1551 393
1552 396
1552 398
1554 401
1554 403
1555 405
1556 407
1557 409
1558 412
1558 413
1559 417
1560 421
1561 423
1561 425
1562 426
1563 429
1563 430
1564 431
1564 432
1565 435
1566 437
1566 438
1567 439
1568 440
1568 442
1568 442
1569 443
1569 444
1569 445
1570 446
1570 446
1571 447
1571 447
1571 447
1571 449
1571 449
1572 450
1573 451
1573 452
1573 454
1574 456
1575 458
1576 459
1577 461
1578 463
1580 465
1583 467
1588 469
1591 471
1595 473
1597 474
1600 474
1601 475
1603 476
1607 477
1609 478
1611 478
1613 480
1616 481
1618 481
1621 483
1623 484
1626 485
1627 486
1628 486
1630 487
1631 487
1633 488
1635 488
1637 489
1639 490
1640 490
1642 490
1645 490
1648 490
1650 491
1654 491
1656 491
1658 491
1660 491
1661 491
1662 491
1665 491
1666 491
1667 491
1668 491
1669 491
1670 491
1671 491
1673 489
1676 488
1677 488
1679 487
1681 487
1682 486
1683 485
1684 485
1684 484
1686 484
1688 483
1689 483
1691 483
1693 483
1695 483
1696 483
1698 483
1700 483
1701 483
1703 483
1705 483
1708 483
1710 484
1711 484
1711 484
1713 484
1715 484
1717 484
1721 484
1723 484
1726 484
1728 484
1730 484
1732 484
1732 484
1735 483
1737 482
1738 481
1740 481
1743 481
1744 480
1745 479
1746 479
1748 478
1748 477
1752 476
1753 476
1754 475
1756 474
1759 473
1761 473
1763 473
1767 471
1769 471
1769 471
1770 470
1771 470
1771 469
1772 469
1773 468
1774 467
1775 466
1776 466
1777 465
1777 462
1777 461
1777 460
1777 459
1777 458
1777 457
1777 456
1777 455
1777 454
1777 454
1778 453
1779 452
1781 451
1784 451
1787 449
1790 448
1792 447
1796 446
1797 445
1797 444
1798 442
1799 441
1799 439
1799 438
1799 437
1799 433
1799 431
1799 427
1798 426
1797 425
1797 423
1797 422
1797 420
1797 419
1796 418
1796 416
1796 415
1797 414
1797 413
1799 412
1799 412
1801 410
1802 409
1803 408
1804 407
1806 406
1806 405
1807 404
1808 403
1808 403
1808 402
1809 402
1809 401
1809 400
1809 399
1809 397
1808 395
1807 392
1804 383
1802 380
1802 378
1800 375
1799 373
1797 370
1797 369
1795 366
1794 364
1793 363
1792 361
1791 360
1788 358
1787 356
1786 355
1784 354
1774 342
1774 341
1774 340
1774 338
1776 320
1754 298
1752 297
1749 295
1730 283
1726 283
1721 281
1710 278
1707 277
1706 277
1700 277
1697 277
1696 277
1693 278
1691 279
1689 280
1684 282
1681 283
1681 284
1680 285
1679 287
1677 287
1677 287
1676 288
1674 289
1674 290
1672 291
1670 294
1669 295
1668 295
1667 297
1667 297
1666 298
1665 301
1665 302
1664 303
1664 304
1664 306
1663 307
1662 309
1661 312
1661 312
1661 313
1660 314
1660 314
1660 315
1659 315
1659 316
1659 316
1658 317
1658 317
1657 318
1657 318
1656 318
1655 319
1655 319
1654 319
1654 319
1653 319
1650 319
1648 319
1645 317
1642 316
1640 315
1637 315
1635 314
1634 314
1633 313
1632 313
1632 313
1630 312
1629 312
1627 312
1625 311
1623 311
1621 310
1618 310
1617 310
1616 310
1613 309
1613 309
1612 309
1612 309
1611 309
1610 309
1609 309
1608 309
1606 309
1605 309
1604 309
1603 309
1603 309
1602 309
1602 308
1601 308
0
``````
• Could you please add an example of the coordinate file? Also, see: stackoverflow.com/questions/5963269/… – nico Apr 9 '15 at 10:49
• This sounds more like a question about algorithms. You may want to think about flagging it for migration to Programmers.SE. Or - this kind of question is studied in the CS area called Computational Geometry so you may want to look through the tagged questions in CS.SE. – Stephan Kolassa Apr 9 '15 at 10:56
• Ok, I have flagged it. But it seams very easy to do in R. Just count the line if its coordinates is located inside the coordinates of the polygon. – Sindre Apr 9 '15 at 11:01
• There is the over function in sp and, depending on exactly what you want to test, numerous intersect options in rgeos (eg., gIntersects, gTouches, gWithin, gCrosses). – Jeffrey Evans Apr 9 '15 at 13:20

The quick-and-dirty solution, which will just test if a point is inside the bounding box of the polygon:

``````is.in.poly <- function(poly, x, y)
{
return (x <= max(poly\$x) && x >= min(poly\$y) &&
y <= max(poly\$y) && y >= min(poly\$y))
}
``````

For most situation this should really be enough if the polygons in your image are mostly separated and you are sure that all lines belong at least to one polygon. Just pass in the midpoint of the line and you're set.

Otherwise there is a `point.in.polygon` function in the `sp` package, which uses the C function InPoly by Joseph O'Rourke.

``````library(sp)
point.in.polygon(x, y, poly\$x, poly\$y)
``````
• +1 for `point.in.polygon` - Just check the endpoints of the line segments to see if either or both are in the polygons. – Mark Apr 9 '15 at 11:13
• Thats good, I will try it soon. Points in the polygon is not so interesting as the length of lines! I want to calculate the areas of the lines and relate to the polygon area. – Sindre Apr 9 '15 at 11:21
• @mark: How do you specify "either" endpoint? – nOOb Apr 9 '15 at 14:44
• @nico: I cannot use your q-&-d method, since in many picture there are a lot of polygons in close relation. – nOOb Apr 9 '15 at 14:45

So I went for the library(sp). I now made a data frame with 0 or 1 if the line was contained in the polygon. I later use this data frame to subset the correct lines for each polygon for area calculation and comparison.

``````            for (r in 1:length(LALarea.x) ) {

for (q in 1:length(LDdia.x) ) {

if ( 1:3 %in% point.in.polygon(LDdia.x[[q]], LDdia.y[[q]], LALarea.x[[r]], LALarea.y[[r]]) ) {
OL[ r , q ]  <- 1
} else OL[ r , q ] <- 0
}
}
``````