Macau has been Chinese territory since the Qin dynasty, and the Yuan dynasty navy patrolled the Pearl River Delta region for many years. In 1557, the Portuguese obtained the right to reside in Macau, but the Ming government continued to exercise sovereignty. This included collecting land rent, establishing a maritime customs office to levy duties on ships, appointing officials to oversee Macau and having the magistrate of Xiangshan County administer the territory, as well as retaining final authority over judicial punishments.